Skip to content

Commit

Permalink
Merge 7eaa5f8 into b46f622
Browse files Browse the repository at this point in the history
  • Loading branch information
dlovellrw committed May 3, 2016
2 parents b46f622 + 7eaa5f8 commit b6fcb1f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
32 changes: 29 additions & 3 deletions lib/public_suffix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ module PublicSuffix
# PublicSuffix.parse("http://www.google.com")
# # => PublicSuffix::DomainInvalid
#
#
# @param [String, #to_s] name The domain name or fully qualified domain name to parse.
# @param [PublicSuffix::List] list The rule list to search, defaults to the default {PublicSuffix::List}
# @param [Boolean] ignore_private
# @return [PublicSuffix::Domain]
#
# @raise [PublicSuffix::Error]
# @raise [PublicSuffix::DomainInvalid]
# If domain is not a valid domain.
# @raise [PublicSuffix::DomainNotAllowed]
# If a rule for +domain+ is found, but the rule doesn't allow +domain+.
Expand All @@ -77,7 +76,34 @@ def self.parse(name, list: List.default, default_rule: nil, ignore_private: fals
decompose(what, rule)
end

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

# Find the registered part of the domain
# "The registered or registrable domain is the public suffix plus one additional label."
# https://publicsuffix.org/list/
#
# @param [String, #to_s] domain
# The domain name or fully qualified domain name to parse.
# @param [PublicSuffix::List] list
# The rule list to search, defaults to the default {PublicSuffix::List}
#
# @return [String]
#
# @raise [PublicSuffix::DomainInvalid]
# If domain does not end with a public suffix according to the rule list
#
def self.registered_domain(domain, list = List.default)
domain = domain.to_s.downcase
rule = list.find(domain)

if rule.nil? || rule.kind_of?(PublicSuffix::Rule::Wildcard)
raise DomainInvalid, "'#{domain}' is not a valid domain"
end

rule.registered_domain(domain)
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}.
Expand Down
21 changes: 21 additions & 0 deletions lib/public_suffix/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,27 @@ def decompose(*)
raise NotImplementedError
end

#
# @param [String, #to_s] domain
# The domain string from which to extract the registered part
# "- The public suffix is the set of labels from the domain which match the labels of
# the prevailing rule, using the matching algorithm above.
# - The registered or registrable domain is the public suffix plus one additional label."
# https://publicsuffix.org/list/
#
# @return [String] registered part of domain
#
def registered_domain(domain)
reg_dom_len = parts.length
whole_parts = domain.split('.')
if (reg_dom_len < whole_parts.length)
registered_parts = whole_parts.pop(reg_dom_len + 1)
else
registered_parts = whole_parts
end
registered_parts.join('.')
end

end

# Normal represents a standard rule (e.g. com).
Expand Down
14 changes: 14 additions & 0 deletions test/unit/public_suffix_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,18 @@ def test_normalize_leading_dot
end
end

def test_registered_domain_invalid
assert_throws(:dom_invalid) do
begin
PublicSuffix.registered_domain('dom.this.fake')
rescue PublicSuffix::DomainInvalid
throw :dom_invalid
end
end
end

def test_registered_domain
reg_part = PublicSuffix.registered_domain('www.example.com')
assert_match 'example.com', reg_part
end
end

0 comments on commit b6fcb1f

Please sign in to comment.