Class: PublicSuffix::Domain

Inherits:
Object
  • Object
show all
Defined in:
lib/public_suffix/domain.rb

Overview

Domain represents a domain name, composed by a TLD, SLD and TRD.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tld) ⇒ Domain #initialize(tld, sld) ⇒ Domain #initialize(tld, sld, trd) ⇒ Domain

Creates and returns a new PublicSuffix::Domain instance.

Examples:

Initialize with a TLD

PublicSuffix::Domain.new("com")
# => #<PublicSuffix::Domain @tld="com">

Initialize with a TLD and SLD

PublicSuffix::Domain.new("com", "example")
# => #<PublicSuffix::Domain @tld="com", @trd=nil>

Initialize with a TLD, SLD and TRD

PublicSuffix::Domain.new("com", "example", "wwww")
# => #<PublicSuffix::Domain @tld="com", @trd=nil, @sld="example">

Overloads:

  • #initialize(tld) ⇒ Domain

    Initializes with a tld.

    Parameters:

    • tld (String)

      The TLD (extension)

  • #initialize(tld, sld) ⇒ Domain

    Initializes with a tld and sld.

    Parameters:

    • tld (String)

      The TLD (extension)

    • sld (String)

      The TRD (domain)

  • #initialize(tld, sld, trd) ⇒ Domain

    Initializes with a tld, sld and trd.

    Parameters:

    • tld (String)

      The TLD (extension)

    • sld (String)

      The SLD (domain)

    • trd (String)

      The TRD (subdomain)

Yields:

  • (self)

    Yields on self.

Yield Parameters:



65
66
67
68
# File 'lib/public_suffix/domain.rb', line 65

def initialize(*args)
  @tld, @sld, @trd = args
  yield(self) if block_given?
end

Instance Attribute Details

#sldObject (readonly)

Returns the value of attribute sld.



33
34
35
# File 'lib/public_suffix/domain.rb', line 33

def sld
  @sld
end

#tldObject (readonly)

Returns the value of attribute tld.



33
34
35
# File 'lib/public_suffix/domain.rb', line 33

def tld
  @tld
end

#trdObject (readonly)

Returns the value of attribute trd.



33
34
35
# File 'lib/public_suffix/domain.rb', line 33

def trd
  @trd
end

Class Method Details

.name_to_labels(name) ⇒ Array<String>

Splits a string into the labels, that is the dot-separated parts.

The input is not validated, but it is assumed to be a valid domain name.

Examples:


name_to_labels('example.com')
# => ['example', 'com']

name_to_labels('example.co.uk')
# => ['example', 'co', 'uk']

Parameters:

  • name (String, #to_s)

    The domain name to split.

Returns:

  • (Array<String>)


28
29
30
# File 'lib/public_suffix/domain.rb', line 28

def self.name_to_labels(name)
  name.to_s.split(DOT)
end

Instance Method Details

#domainString

Returns a domain-like representation of this object if the object is a #domain?, nil otherwise.

PublicSuffix::Domain.new("com").domain
# => nil

PublicSuffix::Domain.new("com", "google").domain
# => "google.com"

PublicSuffix::Domain.new("com", "google", "www").domain
# => "www.google.com"

This method doesn’t validate the input. It handles the domain as a valid domain name and simply applies the necessary transformations.

This method returns a FQD, not just the domain part. To get the domain part, use #sld (aka second level domain).

PublicSuffix::Domain.new("com", "google", "www").domain
# => "google.com"

PublicSuffix::Domain.new("com", "google", "www").sld
# => "google"

Returns:

  • (String)

See Also:



137
138
139
# File 'lib/public_suffix/domain.rb', line 137

def domain
  [@sld, @tld].join(DOT) if domain?
end

#domain?Boolean

Checks whether self looks like a domain.

This method doesn’t actually validate the domain. It only checks whether the instance contains a value for the #tld and #sld attributes.

Examples:


PublicSuffix::Domain.new("com").domain?
# => false

PublicSuffix::Domain.new("com", "google").domain?
# => true

PublicSuffix::Domain.new("com", "google", "www").domain?
# => true

# This is an invalid domain, but returns true
# because this method doesn't validate the content.
PublicSuffix::Domain.new("com", nil).domain?
# => true

Returns:

  • (Boolean)

See Also:



198
199
200
# File 'lib/public_suffix/domain.rb', line 198

def domain?
  !(@tld.nil? || @sld.nil?)
end

#nameString

Returns the full domain name.

Examples:

Gets the domain name of a domain

PublicSuffix::Domain.new("com", "google").name
# => "google.com"

Gets the domain name of a subdomain

PublicSuffix::Domain.new("com", "google", "www").name
# => "www.google.com"

Returns:

  • (String)


105
106
107
# File 'lib/public_suffix/domain.rb', line 105

def name
  [@trd, @sld, @tld].compact.join(DOT)
end

#subdomainString

Returns a subdomain-like representation of this object if the object is a #subdomain?, nil otherwise.

PublicSuffix::Domain.new("com").subdomain
# => nil

PublicSuffix::Domain.new("com", "google").subdomain
# => nil

PublicSuffix::Domain.new("com", "google", "www").subdomain
# => "www.google.com"

This method doesn’t validate the input. It handles the domain as a valid domain name and simply applies the necessary transformations.

This method returns a FQD, not just the subdomain part. To get the subdomain part, use #trd (aka third level domain).

PublicSuffix::Domain.new("com", "google", "www").subdomain
# => "www.google.com"

PublicSuffix::Domain.new("com", "google", "www").trd
# => "www"

Returns:

  • (String)

See Also:



169
170
171
# File 'lib/public_suffix/domain.rb', line 169

def subdomain
  [@trd, @sld, @tld].join(DOT) if subdomain?
end

#subdomain?Boolean

Checks whether self looks like a subdomain.

This method doesn’t actually validate the subdomain. It only checks whether the instance contains a value for the #tld, #sld and #trd attributes. If you also want to validate the domain, use #valid_subdomain? instead.

Examples:


PublicSuffix::Domain.new("com").subdomain?
# => false

PublicSuffix::Domain.new("com", "google").subdomain?
# => false

PublicSuffix::Domain.new("com", "google", "www").subdomain?
# => true

# This is an invalid domain, but returns true
# because this method doesn't validate the content.
PublicSuffix::Domain.new("com", "example", nil).subdomain?
# => true

Returns:

  • (Boolean)

See Also:



229
230
231
# File 'lib/public_suffix/domain.rb', line 229

def subdomain?
  !(@tld.nil? || @sld.nil? || @trd.nil?)
end

#to_aArray<String, nil>

Returns an array containing the domain parts.

Examples:


PublicSuffix::Domain.new("google.com").to_a
# => [nil, "google", "com"]

PublicSuffix::Domain.new("www.google.com").to_a
# => [nil, "google", "com"]

Returns:

  • (Array<String, nil>)


89
90
91
# File 'lib/public_suffix/domain.rb', line 89

def to_a
  [@trd, @sld, @tld]
end

#to_sString

Returns a string representation of this object.

Returns:

  • (String)


73
74
75
# File 'lib/public_suffix/domain.rb', line 73

def to_s
  name
end