Method: Addressable::URI.normalized_encode
- Defined in:
- lib/addressable/uri.rb
.normalized_encode(uri, returning = String) ⇒ String, Addressable::URI
Normalizes the encoding of a URI. Characters within a hostname are not percent encoded to allow for internationalized domain names.
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 |
# File 'lib/addressable/uri.rb', line 509 def self.normalized_encode(uri, returning=String) if !uri.respond_to?(:to_str) raise TypeError, "Can't convert #{uri.class} into String." end if ![String, ::Addressable::URI].include?(returning) raise TypeError, "Expected Class (String or Addressable::URI), " + "got #{returning.inspect}" end uri_object = uri.kind_of?(self) ? uri : self.parse(uri.to_str) 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, :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] = Addressable::IDNA.unicode_normalize_kc(value.to_str) 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::CharacterClasses::SCHEME), :user => self.encode_component(components[:user], Addressable::URI::CharacterClasses::UNRESERVED), :password => self.encode_component(components[:password], Addressable::URI::CharacterClasses::UNRESERVED), :host => components[:host], :port => components[:port], :path => self.encode_component(components[:path], Addressable::URI::CharacterClasses::PATH), :query => self.encode_component(components[:query], Addressable::URI::CharacterClasses::QUERY), :fragment => self.encode_component(components[:fragment], Addressable::URI::CharacterClasses::FRAGMENT) ) if returning == String return encoded_uri.to_s elsif returning == ::Addressable::URI return encoded_uri end end |