Method: Addressable::URI#normalized_host

Defined in:
lib/addressable/uri.rb

#normalized_hostString

The host component for this URI, normalized.

Returns:

  • (String)

    The host component, normalized.



1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
# File 'lib/addressable/uri.rb', line 1126

def normalized_host
  return nil unless self.host

  @normalized_host ||= begin
    if !self.host.strip.empty?
      result = ::Addressable::IDNA.to_ascii(
        URI.unencode_component(self.host.strip.downcase)
      )
      if result =~ /[^\.]\.$/
        # Single trailing dots are unnecessary.
        result = result[0...-1]
      end
      result = Addressable::URI.normalize_component(
        result,
        NormalizeCharacterClasses::HOST
      )
      result
    else
      EMPTY_STR.dup
    end
  end
  # All normalized values should be UTF-8
  force_utf8_encoding_if_needed(@normalized_host)
  @normalized_host
end