Method: Addressable::URI.normalized_encode
- Defined in:
- lib/addressable/uri.rb
.normalized_encode(uri, return_type = String) ⇒ String, Addressable::URI
Normalizes the encoding of a URI. Characters within a hostname are not percent encoded to allow for internationalized domain names.
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 |
# File 'lib/addressable/uri.rb', line 671 def self.normalized_encode(uri, return_type=String) begin uri = uri.to_str rescue NoMethodError, TypeError raise TypeError, "Can't convert #{uri.class} into String." end if !uri.is_a? String if ![String, ::Addressable::URI].include?(return_type) raise TypeError, "Expected Class (String or Addressable::URI), " + "got #{return_type.inspect}" end uri_object = uri.kind_of?(self) ? uri : self.parse(uri) components = { :scheme => self.unencode_component(uri_object.scheme), :user => self.unencode_component(uri_object.user), :password => self.unencode_component(uri_object.password), :host => self.unencode_component(uri_object.host), :port => (uri_object.port.nil? ? nil : uri_object.port.to_s), :path => self.unencode_component(uri_object.path), :query => self.unencode_component(uri_object.query), :fragment => self.unencode_component(uri_object.fragment) } components.each do |key, value| if value != nil begin components[key] = value.to_str.unicode_normalize(:nfc) rescue ArgumentError # Likely a malformed UTF-8 character, skip unicode normalization components[key] = value.to_str end end end encoded_uri = Addressable::URI.new( :scheme => self.encode_component(components[:scheme], Addressable::URI::CharacterClassesRegexps::SCHEME), :user => self.encode_component(components[:user], Addressable::URI::CharacterClassesRegexps::UNRESERVED), :password => self.encode_component(components[:password], Addressable::URI::CharacterClassesRegexps::UNRESERVED), :host => components[:host], :port => components[:port], :path => self.encode_component(components[:path], Addressable::URI::CharacterClassesRegexps::PATH), :query => self.encode_component(components[:query], Addressable::URI::CharacterClassesRegexps::QUERY), :fragment => self.encode_component(components[:fragment], Addressable::URI::CharacterClassesRegexps::FRAGMENT) ) if return_type == String return encoded_uri.to_s elsif return_type == ::Addressable::URI return encoded_uri end end |