From 6e26b65cb8dc21e316de88b7d0458039ed776dfe Mon Sep 17 00:00:00 2001 From: Simone Carletti Date: Sat, 6 Feb 2016 00:56:57 +0100 Subject: [PATCH] List.parse now accepts a `private_domains: false` option This PR removes the need to set the class-level attribute that sets a global state to the list and makes it hard to parse other lists without side effects. It also closes GH-68 --- CHANGELOG.md | 2 ++ lib/public_suffix/list.rb | 30 ++++++------------------------ test/unit/public_suffix_test.rb | 29 ++++++++++++++--------------- 3 files changed, 22 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b132fde8..2024454e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ - CHANGED: Input with leading dot is invalid per PSL acceptance tests +- CHANGED: Remove private_domains class-level attribute. It is replaced by the `private_domains: false` option in the list parse method. + #### Release 1.5.3 diff --git a/lib/public_suffix/list.rb b/lib/public_suffix/list.rb index 2cfb587f..91ee5aae 100644 --- a/lib/public_suffix/list.rb +++ b/lib/public_suffix/list.rb @@ -48,8 +48,8 @@ class List # of {PublicSuffix::List.default_definition}, if required. # # @return [PublicSuffix::List] - def self.default - @default ||= parse(default_definition) + def self.default(**options) + @default ||= parse(default_definition, options) end # Sets the default rule list to +value+. @@ -62,25 +62,6 @@ def self.default=(value) @default = value end - # Shows if support for private (non-ICANN) domains is enabled or not - # - # @return [Boolean] - def self.private_domains? - @private_domains != false - end - - # Enables/disables support for private (non-ICANN) domains - # Implicitly reloads the list - # - # @param [Boolean] value - # enable/disable support - # - # @return [PublicSuffix::List] - def self.private_domains=(value) - @private_domains = !!value - self.clear - end - # Sets the default rule list to +nil+. # # @return [self] @@ -110,13 +91,14 @@ def self.default_definition # # See http://publicsuffix.org/format/ for more details about input format. # - # @param [#each_line] string The list to parse. + # @param string [#each_line] The list to parse. + # @param private_domain [Boolean] whether to ignore the private domains section. # @return [Array] - def self.parse(input) + def self.parse(input, private_domains: true) new do |list| input.each_line do |line| line.strip! - break if !private_domains? && line.include?('===BEGIN PRIVATE DOMAINS===') + break if !private_domains && line.include?('===BEGIN PRIVATE DOMAINS===') # strip blank lines if line.empty? next diff --git a/test/unit/public_suffix_test.rb b/test/unit/public_suffix_test.rb index 3c47de3c..ce95c1e3 100644 --- a/test/unit/public_suffix_test.rb +++ b/test/unit/public_suffix_test.rb @@ -2,6 +2,20 @@ class PublicSuffixTest < Minitest::Unit::TestCase + def test_private_domains_enabled_by_default + domain = PublicSuffix.parse("www.example.blogspot.com") + assert_equal "blogspot.com", domain.tld + end + + def test_private_domains_disable + PublicSuffix::List.default = PublicSuffix::List.parse(PublicSuffix::List.default_definition, private_domains: false) + domain = PublicSuffix.parse("www.example.blogspot.com") + assert_equal "com", domain.tld + ensure + PublicSuffix::List.clear + end + + def test_self_parse_a_domain_with_tld_and_sld domain = PublicSuffix.parse("example.com") assert_instance_of PublicSuffix::Domain, domain @@ -52,21 +66,6 @@ def test_self_parse_name_fqdn assert_equal "www", domain.trd end - def test_private_domains_are_enabled_by_default - domain = PublicSuffix.parse("www.example.blogspot.com") - assert_equal "blogspot.com", domain.tld - end - - def test_disable_support_for_private_domains - begin - PublicSuffix::List.private_domains = false - domain = PublicSuffix.parse("www.example.blogspot.com") - assert_equal "com", domain.tld - ensure - PublicSuffix::List.private_domains = true - end - end - def test_self_parse_with_custom_list list = PublicSuffix::List.new list << PublicSuffix::Rule.factory("test")