Module: PublicSuffix
- Defined in:
- lib/public_suffix/list.rb,
lib/public_suffix.rb,
lib/public_suffix/rule.rb,
lib/public_suffix/domain.rb,
lib/public_suffix/errors.rb,
lib/public_suffix/version.rb
Overview
Public Suffix
Domain name parser based on the Public Suffix List.
Copyright © 2009-2015 Simone Carletti <[email protected]>
Defined Under Namespace
Modules: Rule, Version Classes: Domain, DomainInvalid, DomainNotAllowed, Error, List
Constant Summary collapse
- InvalidDomain =
Deprecated.
Use DomainInvalid.
Backward Compatibility
DomainInvalid
- VERSION =
Version::STRING
Class Method Summary collapse
-
.parse(domain, list = List.default) ⇒ PublicSuffix::Domain
Parses
domain
and returns the Domain instance. -
.valid?(domain) ⇒ Boolean
Checks whether
domain
is assigned and allowed, without actually parsing it.
Class Method Details
.parse(domain, list = List.default) ⇒ PublicSuffix::Domain
Parses domain
and returns the Domain instance.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/public_suffix.rb', line 57 def self.parse(domain, list = List.default) domain = domain.to_s.downcase rule = list.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.
120 121 122 123 124 |
# File 'lib/public_suffix.rb', line 120 def self.valid?(domain) domain = domain.to_s.downcase rule = List.default.find(domain) !rule.nil? && rule.allow?(domain) end |