Class: DomainName

### Home Classes Methods

### In Files

- lib/domain_name.rb
- lib/domain_name/etld_data.rb
- lib/domain_name/punycode.rb
- lib/domain_name/version.rb

### Parent

Object

### Namespace

- MODULE DomainName::Punycode

### Methods

- ::etld_data
- ::new
- ::normalize
- #<
- #<=
- #<=>
- #==
- #>
- #>=
- #canonical?
- #canonical_tld?
- #cookie_domain?
- #inspect
- #ipaddr?
- #superdomain
- #to_s
- #to_str

### Files

- LICENSE.txt

### Class/Module Index

Quicksearch

- DomainName
- DomainName::Punycode
- DomainName::Punycode::ArgumentError
- DomainName::Punycode::BufferOverflowError
- Object

No matching classes.

# DomainName

Represents a domain name ready for extracting its registered domain and TLD.

### Constants

DOT
ETLD_DATA
ETLD_DATA_DATE
VERSION

### Attributes

domain[R]

The least "universally original" domain part of this domain name. For example, "example.co.uk" for "www.sub.example.co.uk". This may be nil if the hostname does not have one, like when it is an IP address, an effective TLD or higher itself, or of a non-canonical domain.

domain_idn[R]

The Unicode representation of the domain property.

hostname[R]

The full host name normalized, ASCII-ized and downcased using the Unicode NFC rules and the Punycode algorithm. If initialized with an IP address, the string representation of the IP address suitable for opening a connection to.

hostname_idn[R]

The Unicode representation of the hostname property.

idn[R]

The Unicode representation of the hostname property.

ipaddr[R]

Returns an IPAddr object if this is an IP address.

tld[R]

The TLD part of this domain name. For example, if the hostname is "www.sub.example.co.uk", the TLD part is "uk". This property is nil only if `ipaddr?` is true. This may be nil if the hostname does not have one, like when it is an IP address or of a non-canonical domain.

tld_idn[R]

The Unicode representation of the tld property.

uri_host[R]

Returns a host name representation suitable for use in the host name part of a URI. A host name, an IPv4 address, or a IPv6 address enclosed in square brackets.

### Public Class Methods

etld_data() click to toggle source

    # File lib/domain_name/etld_data.rb, line 7041
    def self.etld_data
      ETLD_DATA
    end

new(hostname) click to toggle source

Parses hostname into a DomainName object. An IP address is also accepted. An IPv6 address may be enclosed in square brackets.

    # File lib/domain_name.rb, line 77
    def initialize(hostname)
      hostname.is_a?(String) or
        (hostname.respond_to?(:to_str) && (hostname = hostname.to_str).is_a?(String)) or
        raise TypeError, "#{hostname.class} is not a String"
      if hostname.start_with?(DOT)
        raise ArgumentError, "domain name must not start with a dot: #{hostname}"
      end
      case hostname
      when /\A([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\z/
        @ipaddr = IPAddr.new($1)
        @uri_host = @hostname = @ipaddr.to_s
        @domain = @tld = nil
        return
      when /\A([0-9A-Fa-f:]*:[0-9A-Fa-f:]*:[0-9A-Fa-f:]*)\z/,
        /\A\[([0-9A-Fa-f:]*:[0-9A-Fa-f:]*:[0-9A-Fa-f:]*)\]\z/
        @ipaddr = IPAddr.new($1)
        @hostname = @ipaddr.to_s
        @uri_host = "[#{@hostname}]"
        @domain = @tld = nil
        return
      end
      @ipaddr = nil
      @hostname = DomainName.normalize(hostname)
      @uri_host = @hostname
      if last_dot = @hostname.rindex(DOT)
        @tld = @hostname[(last_dot + 1)..-1]
      else
        @tld = @hostname
      end
      etld_data = DomainName.etld_data
      if @canonical_tld_p = etld_data.key?(@tld)
        subdomain = domain = nil
        parent = @hostname
        loop {
          case etld_data[parent]
          when 0
            @domain = domain if domain
            return
          when -1
            @domain = subdomain if subdomain
            return
          when 1
            @domain = parent
            return
          end
          subdomain = domain
          domain = parent
          pos = @hostname.index(DOT, -domain.length) or break
          parent = @hostname[(pos + 1)..-1]
        }
      else
        # unknown/local TLD
        if last_dot
          # fallback - accept cookies down to second level
          # cf. http://www.dkim-reputation.org/regdom-libs/
          if penultimate_dot = @hostname.rindex(DOT, last_dot - 1)
            @domain = @hostname[(penultimate_dot + 1)..-1]
          else
            @domain = @hostname
          end
        else
          # no domain part - must be a local hostname
          @domain = @tld
        end
      end
    end

normalize(domain) click to toggle source

Normalizes a domain using the Punycode algorithm as necessary. The result will be a downcased, ASCII-only string.

    # File lib/domain_name.rb, line 287
    def normalize(domain)
      DomainName::Punycode.encode_hostname(domain.chomp(DOT).to_nfc).downcase
    end

### Public Instance Methods

<(other) click to toggle source

    # File lib/domain_name.rb, line 194
    def <(other)
      case self <=> other
      when -1
        true
      when nil
        nil
      else
        false
      end
    end

<=(other) click to toggle source

    # File lib/domain_name.rb, line 216
    def <=(other)
      case self <=> other
      when -1, 0
        true
      when nil
        nil
      else
        false
      end
    end

<=>(other) click to toggle source

    # File lib/domain_name.rb, line 178
    def <=>(other)
      other = DomainName.new(other) unless DomainName === other
      othername = other.hostname
      if othername == @hostname
        0
      elsif @hostname.end_with?(othername) && @hostname[-othername.size - 1, 1] == DOT
        # The other is higher
        -1
      elsif othername.end_with?(@hostname) && othername[-@hostname.size - 1, 1] == DOT
        # The other is lower
        1
      else
        nil
      end
    end

==(other) click to toggle source

    # File lib/domain_name.rb, line 173
    def ==(other)
      other = DomainName.new(other) unless DomainName === other
      other.hostname == @hostname
    end

>(other) click to toggle source

    # File lib/domain_name.rb, line 205
    def >(other)
      case self <=> other
      when 1
        true
      when nil
        nil
      else
        false
      end
    end

>=(other) click to toggle source

    # File lib/domain_name.rb, line 227
    def >=(other)
      case self <=> other
      when 1, 0
        true
      when nil
        nil
      else
        false
      end
    end

canonical?() click to toggle source

Returns true if this domain name has a canonical registered domain.

    # File lib/domain_name.rb, line 69
    def canonical?
      @canonical_tld_p && (@domain ? true : false)
    end

canonical_tld?() click to toggle source

Returns true if this domain name has a canonical TLD.

    # File lib/domain_name.rb, line 63
    def canonical_tld?
      @canonical_tld_p
    end

cookie_domain?(domain, host_only = false) click to toggle source

Checks if the server represented by this domain is qualified to send and receive cookies with a domain attribute value of domain. A true value given as the second argument represents cookies without a domain attribute value, in which case only hostname equality is checked.

    # File lib/domain_name.rb, line 149
    def cookie_domain?(domain, host_only = false)
      # RFC 6265 #5.3
      # When the user agent "receives a cookie":
      return self == domain if host_only

      domain = DomainName.new(domain) unless DomainName === domain
      if ipaddr?
        # RFC 6265 #5.1.3
        # Do not perform subdomain matching against IP addresses.
        @hostname == domain.hostname
      else
        # RFC 6265 #4.1.1
        # Domain-value must be a subdomain.
        @domain && self <= domain && domain <= @domain ? true : false
      end
    end

inspect() click to toggle source

    # File lib/domain_name.rb, line 273
    def inspect
      str = '#<%s:%s' % [self.class.name, @hostname]
      if @ipaddr
        str << ' (ipaddr)'
      else
        str << ' domain=' << @domain if @domain
        str << ' tld=' << @tld if @tld
      end
      str << '>'
    end

ipaddr?() click to toggle source

Returns true if this is an IP address, such as "192.168.0.1" and "[::1]".

    # File lib/domain_name.rb, line 53
    def ipaddr?
      @ipaddr ? true : false
    end

superdomain() click to toggle source

Returns the superdomain of this domain name.

    # File lib/domain_name.rb, line 167
    def superdomain
      return nil if ipaddr?
      pos = @hostname.index(DOT) or return nil
      self.class.new(@hostname[(pos + 1)..-1])
    end

to_s() click to toggle source

    # File lib/domain_name.rb, line 238
    def to_s
      @hostname
    end

Also aliased as: to_str
to_str() click to toggle source
Alias for: to_s

[Validate]

Generated with the Darkfish Rdoc Generator 2.