Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for disabling private tlds

  • Loading branch information...
commit c53eec3c891f6fe29438d725d1236acbd4130476 1 parent 80d40bb
@rb2k rb2k authored
View
16 README.md
@@ -108,6 +108,22 @@ This library automatically recognizes Fully Qualified Domain Names. A FQDN is a
domain.tld
# => "com"
+## Private domains
+
+This library has support for switching off support for private (non-ICANN) domains
+
+ # Parse a domain on a private TLD
+ domain = PublicSuffix.parse("something.blogspot.com")
+ # => #<PublicSuffix::Domain>
+ domain.tld
+ # => "blogspot.com"
+ # Disable support for private TLDs
+ PublicSuffix::List.private_domains = false
+ # => #<PublicSuffix::List>
+ domain = PublicSuffix.parse("something.blogspot.com")
+ # => #<PublicSuffix::Domain>
+ domain.tld
+ # => "com"
## Feedback and bug reports
View
19 lib/public_suffix/list.rb
@@ -67,6 +67,23 @@ 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 == true || @private_domains.nil?
+ end
+
+ # Enables/disables support for private (non-ICANN) domains
+ # Implicitly reloads the list
+ # @param [Boolean] enable/disable support
+ #
+ # @return [PublicSuffix::List]
+ def self.private_domains=(value)
+ @private_domains = (value == true)
+ self.reload
+ end
+
# Sets the default rule list to +nil+.
#
# @return [self]
@@ -104,7 +121,7 @@ def self.parse(input)
new do |list|
input.each_line do |line|
line.strip!
-
+ break if !@private_domains && line.include?('===BEGIN PRIVATE DOMAINS===')
# strip blank lines
if line.empty?
next
View
2  test/unit/list_test.rb
@@ -38,7 +38,6 @@ def test_equality_with_internals
assert_equal PublicSuffix::List.new.add(rule), PublicSuffix::List.new.add(rule)
end
-
def test_add
assert_equal @list, @list.add(PublicSuffix::Rule.factory(""))
assert_equal @list, @list << PublicSuffix::Rule.factory("")
@@ -133,7 +132,6 @@ def test_self_reload
assert_equal PublicSuffix::List.new, PublicSuffix::List.default
end
-
def test_self_parse
list = PublicSuffix::List.parse(<<EOS)
// ***** BEGIN LICENSE BLOCK *****
View
15 test/unit/public_suffix_test.rb
@@ -51,6 +51,21 @@ def test_self_parse_a_fully_qualified_domain_name
assert_equal "example", domain.sld
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_a_domain_with_custom_list
list = PublicSuffix::List.new
Please sign in to comment.
Something went wrong with that request. Please try again.