Module: PublicSuffixService

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

Defined Under Namespace

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

Constant Summary collapse

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

Backward Compatibility

DomainInvalid
VERSION =
Version::STRING

Class Method Summary collapse

Class Method Details

.parse(domain) ⇒ PublicSuffixService::Domain

Parses domain and returns the Domain instance.

Parsing uses the default RuleList.

Examples:

Parse a valid domain

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

Parse a valid subdomain

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

Parse a fully qualified domain

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

Parse a fully qualified domain (subdomain)

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

Parse an invalid domain

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

Parse an URL (not supported, only domains)

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

Parameters:

  • domain (String, #to_s)

    The domain name or fully qualified domain name to parse.

Returns:

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/public_suffix_service.rb', line 64

def self.parse(domain)
  rule = RuleList.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 RuleList.

Examples:

Validate a valid domain

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

Validate a valid subdomain

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

Validate a not-assigned domain

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

Validate a not-allowed domain

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

Validate a fully qualified domain

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

Check an URL (which is not a valid domain)

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

Parameters:

  • domain (String, #to_s)

    The domain name or fully qualified domain name to validate.

Returns:

  • (Boolean)


125
126
127
128
# File 'lib/public_suffix_service.rb', line 125

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