Skip to content

Commit

Permalink
List.parse now accepts a private_domains: false option
Browse files Browse the repository at this point in the history
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
  • Loading branch information
weppos committed Feb 5, 2016
1 parent cbc06f8 commit 6e26b65
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 39 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 6 additions & 24 deletions lib/public_suffix/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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+.
Expand All @@ -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]
Expand Down Expand Up @@ -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<PublicSuffix::Rule::*>]
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
Expand Down
29 changes: 14 additions & 15 deletions test/unit/public_suffix_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 6e26b65

Please sign in to comment.