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, Error, InvalidDomain, Rule, RuleList

Constant Summary collapse

NAME =
"Public Suffix Service"
GEM =
"public_suffix_service"
AUTHORS =
["Simone Carletti <[email protected]>"]
VERSION =
Version::STRING

Class Method Summary collapse

Class Method Details

.parse(domain) ⇒ Object

Parses domain and returns the PubliSuffixService::Domain instance.

domain - The String domain name to parse.

Examples

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

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

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

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

Raises PublicSuffixService::Error if domain is not a valid domain

Returns the PubliSuffixService::Domain domain.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/public_suffix_service.rb', line 53

def self.parse(domain)
  rule = RuleList.default.find(domain) || raise(InvalidDomain, "`#{domain}' is not a valid domain")

  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 a valid domain name.

This method doesn’t care whether domain is a domain or subdomain. The check is performed using the default PublicSuffixService::RuleList.

domain - The String domain name to parse.

Examples

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

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

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

PublicSuffixService.valid?("x.yz")
# => false

Returns Boolean.

Returns:

  • (Boolean)


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

def self.valid?(domain)
  !RuleList.default.find(domain).nil?
end