Class: PublicSuffix::Domain

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

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)

    • tld (String)

      The TRD (subdomain)

Yields:

  • (self)

    Yields on self.

Yield Parameters:



67
68
69
70
# File 'lib/public_suffix/domain.rb', line 67

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

Instance Attribute Details

#sldObject (readonly)

Returns the value of attribute sld.



35
36
37
# File 'lib/public_suffix/domain.rb', line 35

def sld
  @sld
end

#tldObject (readonly)

Returns the value of attribute tld.



35
36
37
# File 'lib/public_suffix/domain.rb', line 35

def tld
  @tld
end

#trdObject (readonly)

Returns the value of attribute trd.



35
36
37
# File 'lib/public_suffix/domain.rb', line 35

def trd
  @trd
end

Class Method Details

.domain_to_labels(domain) ⇒ Array<String>

Splits a string into its possible labels as a domain in reverse order from the input string.

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

Examples:


domain_to_labels('google.com')
# => ['com', 'google']

domain_to_labels('google.co.uk')
# => ['uk', 'co', 'google']

Parameters:

  • domain (String, #to_s)

    The domain name to split.

Returns:

  • (Array<String>)


31
32
33
# File 'lib/public_suffix/domain.rb', line 31

def self.domain_to_labels(domain)
  domain.to_s.split(".").reverse
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 is an invalid domain
PublicSuffix::Domain.new("qqq", "google").domain
# => "google.qqq"

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:



144
145
146
147
148
# File 'lib/public_suffix/domain.rb', line 144

def domain
  if domain?
    [@sld, @tld].join(".")
  end
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. If you also want to validate the domain, use #valid_domain? instead.

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("qqq", "google").domain?
# => true

Returns:

  • (Boolean)

See Also:



227
228
229
# File 'lib/public_suffix/domain.rb', line 227

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

#is_a_domain?Boolean

Checks whether self is exclusively a domain, and not a subdomain.

Returns:

  • (Boolean)


267
268
269
# File 'lib/public_suffix/domain.rb', line 267

def is_a_domain?
  domain? && !subdomain?
end

#is_a_subdomain?Boolean

Checks whether self is exclusively a subdomain.

Returns:

  • (Boolean)


274
275
276
# File 'lib/public_suffix/domain.rb', line 274

def is_a_subdomain?
  subdomain?
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)


107
108
109
# File 'lib/public_suffix/domain.rb', line 107

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

#rulePublicSuffix::Rule::Base?

Returns the rule matching this domain in the default List.

Returns:



195
196
197
# File 'lib/public_suffix/domain.rb', line 195

def rule
  List.default.find(name)
end

#subdomainString

Returns a domain-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 is an invalid domain
PublicSuffix::Domain.new("qqq", "google", "www").subdomain
# => "www.google.qqq"

This method returns a FQD, not just the domain 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:



183
184
185
186
187
# File 'lib/public_suffix/domain.rb', line 183

def subdomain
  if subdomain?
    [@trd, @sld, @tld].join(".")
  end
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("qqq", "google", "www").subdomain?
# => true

Returns:

  • (Boolean)

See Also:



259
260
261
# File 'lib/public_suffix/domain.rb', line 259

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>)


91
92
93
# File 'lib/public_suffix/domain.rb', line 91

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

#to_sString

Returns a string representation of this object.

Returns:

  • (String)


75
76
77
# File 'lib/public_suffix/domain.rb', line 75

def to_s
  name
end

#valid?Boolean

Checks whether self is assigned and allowed according to default List.

This method triggers a new rule lookup in the default List, which is a quite intensive task.

Examples:

Check a valid domain

Domain.new("com", "example").valid?
# => true

Check a valid subdomain

Domain.new("com", "example", "www").valid?
# => true

Check a not-assigned domain

Domain.new("qqq", "example").valid?
# => false

Check a not-allowed domain

Domain.new("do", "example").valid?
# => false
Domain.new("do", "example", "www").valid?
# => true

Returns:

  • (Boolean)


304
305
306
307
# File 'lib/public_suffix/domain.rb', line 304

def valid?
  r = rule
  !r.nil? && r.allow?(name)
end

#valid_domain?Boolean

Checks whether self looks like a domain and validates according to default List.

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
PublicSuffix::Domain.new("qqq", "google").false?
# => true

Returns:

  • (Boolean)

See Also:



332
333
334
# File 'lib/public_suffix/domain.rb', line 332

def valid_domain?
  domain? && valid?
end

#valid_subdomain?Boolean

Checks whether self looks like a subdomain and validates according to default List.

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
PublicSuffix::Domain.new("qqq", "google", "www").subdomain?
# => false

Returns:

  • (Boolean)

See Also:



359
360
361
# File 'lib/public_suffix/domain.rb', line 359

def valid_subdomain?
  subdomain? && valid?
end