Module: PublicSuffix

Defined in:
lib/public_suffix.rb,
lib/public_suffix/list.rb,
lib/public_suffix/rule.rb,
lib/public_suffix/domain.rb,
lib/public_suffix/errors.rb,
lib/public_suffix/version.rb,
lib/public_suffix/rule_list.rb

Defined Under Namespace

Modules: Version Classes: Domain, DomainInvalid, DomainNotAllowed, Error, List, Rule

Constant Summary collapse

NAME =
"Public Suffix"
GEM =
"public_suffix_service"
AUTHORS =
["Simone Carletti <[email protected]>"]
InvalidDomain =
Deprecated.

Backward Compatibility

DomainInvalid
VERSION =
Version::STRING
RuleList =
List

Class Method Summary collapse

Class Method Details

.parse(domain) ⇒ PublicSuffix::Domain

Parses domain and returns the Domain instance.

Parsing uses the default List.

Examples:

Parse a valid domain

PublicSuffix.parse("google.com")
# => #<PublicSuffix::Domain ...>

Parse a valid subdomain

PublicSuffix.parse("www.google.com")
# => #<PublicSuffix::Domain ...>

Parse a fully qualified domain

PublicSuffix.parse("google.com.")
# => #<PublicSuffix::Domain ...>

Parse a fully qualified domain (subdomain)

PublicSuffix.parse("www.google.com.")
# => #<PublicSuffix::Domain ...>

Parse an invalid domain

PublicSuffix.parse("x.yz")
# => PublicSuffix::DomainInvalid

Parse an URL (not supported, only domains)

PublicSuffix.parse("http://www.google.com")
# => PublicSuffix::DomainInvalid

Parameters:

  • domain (String, #to_s)

    The domain name or fully qualified domain name to parse.

Returns:

Raises:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/public_suffix.rb', line 68

def self.parse(domain)
  rule = List.default.find(domain)
  if rule.nil?
    raise DomainInvalid, "`#{domain}' is not a valid domain"
  end
  if !rule.allow?(domain)
    raise DomainNotAllowed, "`#{domain}' is not allowed according to Registry policy"
  end

  left, right = rule.decompose(domain)

  parts = left.split(".")
  # If we have 0 parts left, there is just a tld and no domain or subdomain
  # If we have 1 part  left, there is just a tld, domain and not subdomain
  # If we have 2 parts left, the last part is the domain, the other parts (combined) are the subdomain
  tld = right
  sld = parts.empty? ? nil : parts.pop
  trd = parts.empty? ? nil : parts.join(".")

  Domain.new(tld, sld, trd)
end

.valid?(domain) ⇒ Boolean

Checks whether domain is assigned and allowed,  without actually parsing it.

This method doesn’t care whether domain is a domain or subdomain. The validation is performed using the default List.

Examples:

Validate a valid domain

PublicSuffix.valid?("example.com")
# => true

Validate a valid subdomain

PublicSuffix.valid?("www.example.com")
# => true

Validate a not-assigned domain

PublicSuffix.valid?("example.zip")
# => false

Validate a not-allowed domain

PublicSuffix.valid?("example.do")
# => false
PublicSuffix.valid?("www.example.do")
# => true

Validate a fully qualified domain

PublicSuffix.valid?("google.com.")
# => true
PublicSuffix.valid?("www.google.com.")
# => true

Check an URL (which is not a valid domain)

PublicSuffix.valid?("http://www.example.com")
# => false

Parameters:

  • domain (String, #to_s)

    The domain name or fully qualified domain name to validate.

Returns:

  • (Boolean)


129
130
131
132
# File 'lib/public_suffix.rb', line 129

def self.valid?(domain)
  rule = List.default.find(domain)
  !rule.nil? && rule.allow?(domain)
end