class Addressable::URI

def self.normalize_component(component, character_class=

Returns:
  • (String) - The normalized component.

Parameters:
  • character_class (String, Regexp) --
  • component (String, #to_str) -- The URI component to encode.
def self.normalize_component(component, character_class=
    CharacterClasses::RESERVED + CharacterClasses::UNRESERVED)
  return nil if component.nil?
  begin
    component = component.to_str
  rescue NoMethodError, TypeError
    raise TypeError, "Can't convert #{component.class} into String."
  end if !component.is_a? String
  if ![String, Regexp].include?(character_class.class)
    raise TypeError,
      "Expected String or Regexp, got #{character_class.inspect}"
  end
  if character_class.kind_of?(String)
    character_class = /[^#{character_class}]/
  end
  if component.respond_to?(:force_encoding)
    # We can't perform regexps on invalid UTF sequences, but
    # here we need to, so switch to ASCII.
    component = component.dup
    component.force_encoding(Encoding::ASCII_8BIT)
  end
  unencoded = self.unencode_component(component)
  begin
    encoded = self.encode_component(
      Addressable::IDNA.unicode_normalize_kc(unencoded),
      character_class
    )
  rescue ArgumentError
    encoded = self.encode_component(unencoded)
  end
  return encoded
end