module PublicSuffix
Constants
- AUTHORS
- GEM
- InvalidDomain
Raised when trying to parse an invalid domain. A domain is considered invalid when no rule is found in the definition list.
@example
PublicSuffix.parse("nic.test") # => PublicSuffix::DomainInvalid PublicSuffix.parse("http://www.nic.it") # => PublicSuffix::DomainInvalid
@since 0.6.0
- NAME
- RuleList
A {PublicSuffix::List} is a collection of one or more {PublicSuffix::Rule}.
Given a {PublicSuffix::List}, you can add or remove {PublicSuffix::Rule}, iterate all items in the list or search for the first rule which matches a specific domain name.
# Create a new list list = PublicSuffix::List.new # Push two rules to the list list << PublicSuffix::Rule.factory("it") list << PublicSuffix::Rule.factory("com") # Get the size of the list list.size # => 2 # Search for the rule matching given domain list.find("example.com") # => #<PublicSuffix::Rule::Normal> list.find("example.org") # => nil
You can create as many {PublicSuffix::List} you want. The {PublicSuffix::List.default} rule list is used to tokenize and validate a domain.
{PublicSuffix::List} implements
Enumerable
module.- VERSION
Public Class Methods
Parses domain
and returns the {PublicSuffix::Domain} instance.
Parsing uses the default {PublicSuffix::List}.
@param [String, to_s] domain
The domain name or fully qualified domain name to parse.
@return [PublicSuffix::Domain]
@example Parse a valid domain
PublicSuffix.parse("google.com") # => #<PublicSuffix::Domain ...>
@example Parse a valid subdomain
PublicSuffix.parse("www.google.com") # => #<PublicSuffix::Domain ...>
@example Parse a fully qualified domain
PublicSuffix.parse("google.com.") # => #<PublicSuffix::Domain ...>
@example Parse a fully qualified domain (subdomain)
PublicSuffix.parse("www.google.com.") # => #<PublicSuffix::Domain ...>
@example Parse an invalid domain
PublicSuffix.parse("x.yz") # => PublicSuffix::DomainInvalid
@example Parse an URL (not supported, only domains)
PublicSuffix.parse("http://www.google.com") # => PublicSuffix::DomainInvalid
@raise [PublicSuffix::Error]
If domain is not a valid domain.
@raise [PublicSuffix::DomainNotAllowed]
If a rule for +domain+ is found, but the rule doesn't allow +domain+.
# 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
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 {PublicSuffix::List}. @param [String, #to_s] domain The domain name or fully qualified domain name to validate. @return [Boolean] @example Validate a valid domain PublicSuffix.valid?("example.com") # => true @example Validate a valid subdomain PublicSuffix.valid?("www.example.com") # => true @example Validate a not-assigned domain PublicSuffix.valid?("example.zip") # => false @example Validate a not-allowed domain PublicSuffix.valid?("example.do") # => false PublicSuffix.valid?("www.example.do") # => true @example Validate a fully qualified domain PublicSuffix.valid?("google.com.") # => true PublicSuffix.valid?("www.google.com.") # => true @example Check an URL (which is not a valid domain) PublicSuffix.valid?("http://www.example.com") # => false
# File lib/public_suffix.rb, line 129 def self.valid?(domain) rule = List.default.find(domain) !rule.nil? && rule.allow?(domain) end