class Addressable::URI
def self.encode_component(component, character_class=
-
(String)
- The encoded component.
Parameters:
-
upcase_encoded
(Regexp
) -- -
character_class
(String, Regexp
) -- -
component
(String, #to_str
) -- The URI component to encode.
def self.encode_component(component, character_class= CharacterClasses::RESERVED + CharacterClasses::UNRESERVED, upcase_encoded='') return nil if component.nil? begin if component.kind_of?(Symbol) || component.kind_of?(Numeric) || component.kind_of?(TrueClass) || component.kind_of?(FalseClass) component = component.to_s else component = component.to_str end rescue TypeError, NoMethodError 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 # 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) # Avoiding gsub! because there are edge cases with frozen strings component = component.gsub(character_class) do |sequence| SEQUENCE_UPCASED_PERCENT_ENCODING_TABLE[sequence] end if upcase_encoded.length > 0 upcase_encoded_chars = upcase_encoded.chars.map do |char| SEQUENCE_ENCODING_TABLE[char] end component = component.gsub(/%(#{upcase_encoded_chars.join('|')})/, &:upcase) end return component end