Skip to content
Browse files

Allow a custom List on `PublicSuffix.parse`

  • Loading branch information...
1 parent 3b71f0b commit 02d58832ddc5947d4d5f02fa99f08051c1ebe9d5 @itspriddle itspriddle committed
Showing with 14 additions and 4 deletions.
  1. +4 −4 lib/public_suffix.rb
  2. +10 −0 test/unit/public_suffix_test.rb
View
8 lib/public_suffix.rb
@@ -24,10 +24,10 @@ module PublicSuffix
# 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.
+ # @param [PublicSuffix::List] list
+ # The rule list to search, defaults to the default {PublicSuffix::List}
#
# @return [PublicSuffix::Domain]
#
@@ -61,8 +61,8 @@ module PublicSuffix
# If a rule for +domain+ is found, but the rule
# doesn't allow +domain+.
#
- def self.parse(domain)
- rule = List.default.find(domain)
+ def self.parse(domain, list = List.default)
+ rule = list.find(domain)
if rule.nil?
raise DomainInvalid, "`#{domain}' is not a valid domain"
View
10 test/unit/public_suffix_test.rb
@@ -52,6 +52,16 @@ def test_self_parse_a_fully_qualified_domain_name
assert_equal "www", domain.trd
end
+ def test_self_parse_a_domain_with_custom_list
+ list = PublicSuffix::List.new
+ list << PublicSuffix::Rule.factory("test")
+
+ domain = PublicSuffix.parse("www.example.test", list)
+ assert_equal "test", domain.tld
+ assert_equal "example", domain.sld
+ assert_equal "www", domain.trd
+ end
+
def test_self_parse_raises_with_invalid_domain
error = assert_raise(PublicSuffix::DomainInvalid) { PublicSuffix.parse("example.zip") }
assert_match %r{example\.zip}, error.message

0 comments on commit 02d5883

Please sign in to comment.
Something went wrong with that request. Please try again.