public_suffix_service 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,14 @@
1
+ #--
2
+ # Public Suffix
3
+ #
4
+ # Domain name parser based on the Public Suffix List.
5
+ #
6
+ # Copyright (c) 2009-2011 Simone Carletti <weppos@weppos.net>
7
+ #++
8
+
9
+
10
+ warn("DEPRECATION WARNING: The PublicSuffix::RuleList object has been deprecated and will be removed in PublicSuffix 1.0. Please use PublicSuffix::List instead.")
11
+
12
+ module PublicSuffix
13
+ RuleList = List
14
+ end
@@ -1,18 +1,18 @@
1
1
  #--
2
- # Public Suffix Service
2
+ # Public Suffix
3
3
  #
4
- # Domain Name parser based on the Public Suffix List.
4
+ # Domain name parser based on the Public Suffix List.
5
5
  #
6
6
  # Copyright (c) 2009-2011 Simone Carletti <weppos@weppos.net>
7
7
  #++
8
8
 
9
9
 
10
- module PublicSuffixService
10
+ module PublicSuffix
11
11
 
12
12
  module Version
13
13
  MAJOR = 0
14
14
  MINOR = 9
15
- PATCH = 0
15
+ PATCH = 1
16
16
  BUILD = nil
17
17
 
18
18
  STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join(".")
@@ -1,130 +1,14 @@
1
1
  #--
2
- # Public Suffix Service
2
+ # Public Suffix
3
3
  #
4
- # Domain Name parser based on the Public Suffix List.
4
+ # Domain name parser based on the Public Suffix List.
5
5
  #
6
6
  # Copyright (c) 2009-2011 Simone Carletti <weppos@weppos.net>
7
7
  #++
8
8
 
9
9
 
10
- require 'public_suffix_service/domain'
11
- require 'public_suffix_service/version'
12
- require 'public_suffix_service/errors'
13
- require 'public_suffix_service/rule'
14
- require 'public_suffix_service/rule_list'
10
+ warn("DEPRECATION WARNING: The PublicSuffixService gem is now known as PublicSuffix. Please install `public_suffix` instead of `public_suffix_service`.")
15
11
 
12
+ require 'public_suffix'
16
13
 
17
- module PublicSuffixService
18
-
19
- NAME = "Public Suffix Service"
20
- GEM = "public_suffix_service"
21
- AUTHORS = ["Simone Carletti <weppos@weppos.net>"]
22
-
23
-
24
- # Parses +domain+ and returns the
25
- # {PublicSuffixService::Domain} instance.
26
- #
27
- # Parsing uses the default {PublicSuffixService::RuleList}.
28
- #
29
- # @param [String, #to_s] domain
30
- # The domain name or fully qualified domain name to parse.
31
- #
32
- # @return [PublicSuffixService::Domain]
33
- #
34
- # @example Parse a valid domain
35
- # PublicSuffixService.parse("google.com")
36
- # # => #<PubliSuffixService::Domain ...>
37
- #
38
- # @example Parse a valid subdomain
39
- # PublicSuffixService.parse("www.google.com")
40
- # # => #<PubliSuffixService::Domain ...>
41
- #
42
- # @example Parse a fully qualified domain
43
- # PublicSuffixService.parse("google.com.")
44
- # # => #<PubliSuffixService::Domain ...>
45
- #
46
- # @example Parse a fully qualified domain (subdomain)
47
- # PublicSuffixService.parse("www.google.com.")
48
- # # => #<PubliSuffixService::Domain ...>
49
- #
50
- # @example Parse an invalid domain
51
- # PublicSuffixService.parse("x.yz")
52
- # # => PublicSuffixService::DomainInvalid
53
- #
54
- # @example Parse an URL (not supported, only domains)
55
- # PublicSuffixService.parse("http://www.google.com")
56
- # # => PublicSuffixService::DomainInvalid
57
- #
58
- # @raise [PublicSuffixService::Error]
59
- # If domain is not a valid domain.
60
- # @raise [PublicSuffixService::DomainNotAllowed]
61
- # If a rule for +domain+ is found, but the rule
62
- # doesn't allow +domain+.
63
- #
64
- def self.parse(domain)
65
- rule = RuleList.default.find(domain)
66
- if rule.nil?
67
- raise DomainInvalid, "`#{domain}' is not a valid domain"
68
- end
69
- if !rule.allow?(domain)
70
- raise DomainNotAllowed, "`#{domain}' is not allowed according to Registry policy"
71
- end
72
-
73
- left, right = rule.decompose(domain)
74
-
75
- parts = left.split(".")
76
- # If we have 0 parts left, there is just a tld and no domain or subdomain
77
- # If we have 1 part left, there is just a tld, domain and not subdomain
78
- # If we have 2 parts left, the last part is the domain, the other parts (combined) are the subdomain
79
- tld = right
80
- sld = parts.empty? ? nil : parts.pop
81
- trd = parts.empty? ? nil : parts.join(".")
82
-
83
- Domain.new(tld, sld, trd)
84
- end
85
-
86
- # Checks whether +domain+ is assigned and allowed,
87
- # without actually parsing it.
88
- #
89
- # This method doesn't care whether domain is a domain or subdomain.
90
- # The validation is performed using the default {PublicSuffixService::RuleList}.
91
- #
92
- # @param [String, #to_s] domain
93
- # The domain name or fully qualified domain name to validate.
94
- #
95
- # @return [Boolean]
96
- #
97
- # @example Validate a valid domain
98
- # PublicSuffixService.valid?("example.com")
99
- # # => true
100
- #
101
- # @example Validate a valid subdomain
102
- # PublicSuffixService.valid?("www.example.com")
103
- # # => true
104
- #
105
- # @example Validate a not-assigned domain
106
- # PublicSuffixService.valid?("example.zip")
107
- # # => false
108
- #
109
- # @example Validate a not-allowed domain
110
- # PublicSuffixService.valid?("example.do")
111
- # # => false
112
- # PublicSuffixService.valid?("www.example.do")
113
- # # => true
114
- #
115
- # @example Validate a fully qualified domain
116
- # PublicSuffixService.valid?("google.com.")
117
- # # => true
118
- # PublicSuffixService.valid?("www.google.com.")
119
- # # => true
120
- #
121
- # @example Check an URL (which is not a valid domain)
122
- # PublicSuffixService.valid?("http://www.example.com")
123
- # # => false
124
- #
125
- def self.valid?(domain)
126
- rule = RuleList.default.find(domain)
127
- !rule.nil? && rule.allow?(domain)
128
- end
129
-
130
- end
14
+ PublicSuffixService = PublicSuffix
@@ -1,34 +1,37 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  Gem::Specification.new do |s|
4
- s.name = %q{public_suffix_service}
5
- s.version = "0.8.3"
4
+ s.name = "public_suffix_service"
5
+ s.version = "0.9.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
- s.authors = [%q{Simone Carletti}]
9
- s.date = %q{2011-06-17}
10
- s.description = %q{PublicSuffixService can parse and decompose a domain name into top level domain, domain and subdomains.}
11
- s.email = %q{weppos@weppos.net}
12
- s.files = [%q{.gemtest}, %q{.gitignore}, %q{.yardopts}, %q{CHANGELOG.md}, %q{Gemfile}, %q{Gemfile.lock}, %q{LICENSE}, %q{README.md}, %q{Rakefile}, %q{lib/public_suffix_service.rb}, %q{lib/public_suffix_service/definitions.txt}, %q{lib/public_suffix_service/domain.rb}, %q{lib/public_suffix_service/errors.rb}, %q{lib/public_suffix_service/rule.rb}, %q{lib/public_suffix_service/rule_list.rb}, %q{lib/public_suffix_service/version.rb}, %q{public_suffix_service.gemspec}, %q{test/acceptance_test.rb}, %q{test/public_suffix_service/domain_test.rb}, %q{test/public_suffix_service/errors_test.rb}, %q{test/public_suffix_service/rule_list_test.rb}, %q{test/public_suffix_service/rule_test.rb}, %q{test/public_suffix_service_test.rb}, %q{test/test_helper.rb}]
13
- s.homepage = %q{http://www.simonecarletti.com/code/public_suffix_service}
14
- s.require_paths = [%q{lib}]
8
+ s.authors = ["Simone Carletti"]
9
+ s.date = "2011-12-24"
10
+ s.description = "PublicSuffixService gem is now known as PublicSuffix."
11
+ s.email = "weppos@weppos.net"
12
+ s.files = [".gemtest", ".gitignore", ".travis.yml", ".yardopts", "CHANGELOG.md", "Gemfile", "Gemfile.lock", "LICENSE", "README.md", "Rakefile", "lib/public_suffix.rb", "lib/public_suffix/definitions.txt", "lib/public_suffix/domain.rb", "lib/public_suffix/errors.rb", "lib/public_suffix/list.rb", "lib/public_suffix/rule.rb", "lib/public_suffix/rule_list.rb", "lib/public_suffix/version.rb", "lib/public_suffix_service.rb", "public_suffix_service.gemspec", "test/acceptance_test.rb", "test/test_helper.rb", "test/unit/domain_test.rb", "test/unit/errors_test.rb", "test/unit/list_test.rb", "test/unit/public_suffix_test.rb", "test/unit/rule_test.rb"]
13
+ s.homepage = "http://www.simonecarletti.com/code/public_suffix"
14
+ s.require_paths = ["lib"]
15
15
  s.required_ruby_version = Gem::Requirement.new(">= 1.8.7")
16
- s.rubygems_version = %q{1.8.3}
17
- s.summary = %q{Domain name parser based in the Public Suffix List.}
18
- s.test_files = [%q{test/acceptance_test.rb}, %q{test/public_suffix_service/domain_test.rb}, %q{test/public_suffix_service/errors_test.rb}, %q{test/public_suffix_service/rule_list_test.rb}, %q{test/public_suffix_service/rule_test.rb}, %q{test/public_suffix_service_test.rb}, %q{test/test_helper.rb}]
16
+ s.rubygems_version = "1.8.11"
17
+ s.summary = "Domain name parser based in the Public Suffix List."
18
+ s.test_files = ["test/acceptance_test.rb", "test/test_helper.rb", "test/unit/domain_test.rb", "test/unit/errors_test.rb", "test/unit/list_test.rb", "test/unit/public_suffix_test.rb", "test/unit/rule_test.rb"]
19
19
 
20
20
  if s.respond_to? :specification_version then
21
21
  s.specification_version = 3
22
22
 
23
23
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
24
- s.add_development_dependency(%q<rr>, [">= 0"])
24
+ s.add_development_dependency(%q<rake>, [">= 0"])
25
+ s.add_development_dependency(%q<mocha>, [">= 0"])
25
26
  s.add_development_dependency(%q<yard>, [">= 0"])
26
27
  else
27
- s.add_dependency(%q<rr>, [">= 0"])
28
+ s.add_dependency(%q<rake>, [">= 0"])
29
+ s.add_dependency(%q<mocha>, [">= 0"])
28
30
  s.add_dependency(%q<yard>, [">= 0"])
29
31
  end
30
32
  else
31
- s.add_dependency(%q<rr>, [">= 0"])
33
+ s.add_dependency(%q<rake>, [">= 0"])
34
+ s.add_dependency(%q<mocha>, [">= 0"])
32
35
  s.add_dependency(%q<yard>, [">= 0"])
33
36
  end
34
37
  end
@@ -15,7 +15,7 @@ class AcceptanceTest < Test::Unit::TestCase
15
15
 
16
16
  def test_valid
17
17
  ValidCases.each do |name, results|
18
- domain = PublicSuffixService.parse(name)
18
+ domain = PublicSuffix.parse(name)
19
19
  trd, sld, tld = results
20
20
  assert_equal tld, domain.tld, "Invalid tld for '#{name}'"
21
21
  assert_equal sld, domain.sld, "Invalid sld for '#{name}'"
@@ -24,12 +24,12 @@ class AcceptanceTest < Test::Unit::TestCase
24
24
  end
25
25
 
26
26
  InvalidCases = {
27
- "nic.ke" => PublicSuffixService::DomainNotAllowed,
27
+ "nic.ke" => PublicSuffix::DomainNotAllowed,
28
28
  }
29
29
 
30
30
  def test_invalid
31
31
  InvalidCases.each do |name, error|
32
- assert_raise(error) { PublicSuffixService.parse(name) }
32
+ assert_raise(error) { PublicSuffix.parse(name) }
33
33
  end
34
34
  end
35
35
 
@@ -1,10 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
- require 'rr'
3
+ require 'mocha'
4
4
 
5
5
  $:.unshift File.expand_path('../../lib', __FILE__)
6
- require 'public_suffix_service'
7
-
8
- class Test::Unit::TestCase
9
- include RR::Adapters::TestUnit
10
- end
6
+ require 'public_suffix'
@@ -1,31 +1,31 @@
1
1
  require 'test_helper'
2
2
 
3
- class PublicSuffixService::DomainTest < Test::Unit::TestCase
3
+ class PublicSuffix::DomainTest < Test::Unit::TestCase
4
4
 
5
5
  def setup
6
- @klass = PublicSuffixService::Domain
6
+ @klass = PublicSuffix::Domain
7
7
  end
8
8
 
9
9
  # Tokenizes given input into labels.
10
10
  def test_self_domain_to_labels
11
11
  assert_equal %w( com live spaces someone ),
12
- PublicSuffixService::Domain.domain_to_labels("someone.spaces.live.com")
12
+ PublicSuffix::Domain.domain_to_labels("someone.spaces.live.com")
13
13
  assert_equal %w( com zoho wiki leontina23samiko ),
14
- PublicSuffixService::Domain.domain_to_labels("leontina23samiko.wiki.zoho.com")
14
+ PublicSuffix::Domain.domain_to_labels("leontina23samiko.wiki.zoho.com")
15
15
  end
16
16
 
17
17
  # Converts input into String.
18
18
  def test_self_domain_to_labels_converts_input_to_string
19
19
  assert_equal %w( com live spaces someone ),
20
- PublicSuffixService::Domain.domain_to_labels(:"someone.spaces.live.com")
20
+ PublicSuffix::Domain.domain_to_labels(:"someone.spaces.live.com")
21
21
  end
22
22
 
23
23
  # Ignores trailing .
24
24
  def test_self_domain_to_labels_ignores_trailing_dot
25
25
  assert_equal %w( com live spaces someone ),
26
- PublicSuffixService::Domain.domain_to_labels("someone.spaces.live.com")
26
+ PublicSuffix::Domain.domain_to_labels("someone.spaces.live.com")
27
27
  assert_equal %w( com live spaces someone ),
28
- PublicSuffixService::Domain.domain_to_labels(:"someone.spaces.live.com")
28
+ PublicSuffix::Domain.domain_to_labels(:"someone.spaces.live.com")
29
29
  end
30
30
 
31
31
 
@@ -72,7 +72,7 @@ class PublicSuffixService::DomainTest < Test::Unit::TestCase
72
72
  assert_equal "google", @klass.new("com", "google", "www").sld
73
73
  end
74
74
 
75
- def test_tld
75
+ def test_trd
76
76
  assert_equal "www", @klass.new("com", "google", "www").trd
77
77
  end
78
78
 
@@ -103,9 +103,9 @@ class PublicSuffixService::DomainTest < Test::Unit::TestCase
103
103
 
104
104
  def test_rule
105
105
  assert_equal nil, @klass.new("zip").rule
106
- assert_equal PublicSuffixService::Rule.factory("com"), @klass.new("com").rule
107
- assert_equal PublicSuffixService::Rule.factory("com"), @klass.new("com", "google").rule
108
- assert_equal PublicSuffixService::Rule.factory("com"), @klass.new("com", "google", "www").rule
106
+ assert_equal PublicSuffix::Rule.factory("com"), @klass.new("com").rule
107
+ assert_equal PublicSuffix::Rule.factory("com"), @klass.new("com", "google").rule
108
+ assert_equal PublicSuffix::Rule.factory("com"), @klass.new("com", "google", "www").rule
109
109
  end
110
110
 
111
111
 
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+
3
+ class ErrorsTest < Test::Unit::TestCase
4
+
5
+ # Inherits from StandardError
6
+ def test_error_inheritance
7
+ assert_kind_of StandardError,
8
+ PublicSuffix::Error.new
9
+ end
10
+
11
+ # Inherits from PublicSuffix::Error
12
+ def test_domain_invalid_inheritance
13
+ assert_kind_of PublicSuffix::Error,
14
+ PublicSuffix::DomainInvalid.new
15
+ end
16
+
17
+ # Inherits from PublicSuffix::DomainInvalid
18
+ def test_domain_not_allowed_inheritance
19
+ assert_kind_of PublicSuffix::DomainInvalid,
20
+ PublicSuffix::DomainNotAllowed.new
21
+ end
22
+
23
+ end
@@ -0,0 +1,193 @@
1
+ require 'test_helper'
2
+
3
+ class PublicSuffix::ListTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @list = PublicSuffix::List.new
7
+ end
8
+
9
+ def teardown
10
+ PublicSuffix::List.clear
11
+ end
12
+
13
+
14
+ def test_initialize
15
+ assert_instance_of PublicSuffix::List, @list
16
+ assert_equal 0, @list.length
17
+ end
18
+
19
+ def test_initialize_create_index_when_empty
20
+ assert_equal({}, @list.indexes)
21
+ end
22
+
23
+ def test_indexes
24
+ @list = PublicSuffix::List.parse(<<EOS)
25
+ // com : http://en.wikipedia.org/wiki/.com
26
+ com
27
+
28
+ // uk : http://en.wikipedia.org/wiki/.uk
29
+ *.uk
30
+ *.sch.uk
31
+ !bl.uk
32
+ !british-library.uk
33
+ EOS
34
+
35
+ assert !@list.indexes.empty?
36
+ assert_equal [1,2,3,4], @list.indexes.delete('uk')
37
+ assert_equal [0], @list.indexes.delete('com')
38
+ assert @list.indexes.empty?
39
+ end
40
+
41
+
42
+ def test_equality_with_self
43
+ list = PublicSuffix::List.new
44
+ assert_equal list, list
45
+ end
46
+
47
+ def test_equality_with_internals
48
+ rule = PublicSuffix::Rule.factory("com")
49
+ assert_equal PublicSuffix::List.new.add(rule), PublicSuffix::List.new.add(rule)
50
+ end
51
+
52
+
53
+ def test_add
54
+ assert_equal @list, @list.add(PublicSuffix::Rule.factory(""))
55
+ assert_equal @list, @list << PublicSuffix::Rule.factory("")
56
+ assert_equal 2, @list.length
57
+ end
58
+
59
+ def test_add_should_recreate_index
60
+ @list = PublicSuffix::List.parse("com")
61
+ assert_equal PublicSuffix::Rule.factory("com"), @list.find("google.com")
62
+ assert_equal nil, @list.find("google.net")
63
+
64
+ @list << PublicSuffix::Rule.factory("net")
65
+ assert_equal PublicSuffix::Rule.factory("com"), @list.find("google.com")
66
+ assert_equal PublicSuffix::Rule.factory("net"), @list.find("google.net")
67
+ end
68
+
69
+ def test_empty?
70
+ assert @list.empty?
71
+ @list.add(PublicSuffix::Rule.factory(""))
72
+ assert !@list.empty?
73
+ end
74
+
75
+ def test_size
76
+ assert_equal 0, @list.length
77
+ assert_equal @list, @list.add(PublicSuffix::Rule.factory(""))
78
+ assert_equal 1, @list.length
79
+ end
80
+
81
+ def test_clear
82
+ assert_equal 0, @list.length
83
+ assert_equal @list, @list.add(PublicSuffix::Rule.factory(""))
84
+ assert_equal 1, @list.length
85
+ assert_equal @list, @list.clear
86
+ assert_equal 0, @list.length
87
+ end
88
+
89
+
90
+ def test_find
91
+ @list = PublicSuffix::List.parse(<<EOS)
92
+ // com : http://en.wikipedia.org/wiki/.com
93
+ com
94
+
95
+ // uk : http://en.wikipedia.org/wiki/.uk
96
+ *.uk
97
+ *.sch.uk
98
+ !bl.uk
99
+ !british-library.uk
100
+ EOS
101
+ assert_equal PublicSuffix::Rule.factory("com"), @list.find("google.com")
102
+ assert_equal PublicSuffix::Rule.factory("com"), @list.find("foo.google.com")
103
+ assert_equal PublicSuffix::Rule.factory("*.uk"), @list.find("google.uk")
104
+ assert_equal PublicSuffix::Rule.factory("*.uk"), @list.find("google.co.uk")
105
+ assert_equal PublicSuffix::Rule.factory("*.uk"), @list.find("foo.google.co.uk")
106
+ assert_equal PublicSuffix::Rule.factory("!british-library.uk"), @list.find("british-library.uk")
107
+ assert_equal PublicSuffix::Rule.factory("!british-library.uk"), @list.find("foo.british-library.uk")
108
+ end
109
+
110
+ def test_select
111
+ @list = PublicSuffix::List.parse(<<EOS)
112
+ // com : http://en.wikipedia.org/wiki/.com
113
+ com
114
+
115
+ // uk : http://en.wikipedia.org/wiki/.uk
116
+ *.uk
117
+ *.sch.uk
118
+ !bl.uk
119
+ !british-library.uk
120
+ EOS
121
+ assert_equal 2, @list.select("british-library.uk").size
122
+ end
123
+
124
+
125
+ def test_self_default_getter
126
+ assert_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
127
+ PublicSuffix::List.default
128
+ assert_not_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
129
+ end
130
+
131
+ def test_self_default_setter
132
+ PublicSuffix::List.default
133
+ assert_not_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
134
+ PublicSuffix::List.default = nil
135
+ assert_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
136
+ end
137
+
138
+ def test_self_clear
139
+ PublicSuffix::List.default
140
+ assert_not_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
141
+ PublicSuffix::List.clear
142
+ assert_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
143
+ end
144
+
145
+ def test_self_reload
146
+ PublicSuffix::List.default
147
+ PublicSuffix::List.expects(:default_definition).returns("")
148
+
149
+ PublicSuffix::List.reload
150
+ assert_equal PublicSuffix::List.new, PublicSuffix::List.default
151
+ end
152
+
153
+
154
+ def test_self_parse
155
+ list = PublicSuffix::List.parse(<<EOS)
156
+ // ***** BEGIN LICENSE BLOCK *****
157
+ // Version: MPL 1.1/GPL 2.0/LGPL 2.1
158
+ //
159
+ // ***** END LICENSE BLOCK *****
160
+
161
+ // ac : http://en.wikipedia.org/wiki/.ac
162
+ ac
163
+ com.ac
164
+
165
+ // ad : http://en.wikipedia.org/wiki/.ad
166
+ ad
167
+
168
+ // ar : http://en.wikipedia.org/wiki/.ar
169
+ *.ar
170
+ !congresodelalengua3.ar
171
+ EOS
172
+
173
+ assert_instance_of PublicSuffix::List, list
174
+ assert_equal 5, list.length
175
+ assert_equal %w(ac com.ac ad *.ar !congresodelalengua3.ar).map { |name| PublicSuffix::Rule.factory(name) }, list.to_a
176
+ end
177
+
178
+ def test_self_parse_should_create_cache
179
+ list = PublicSuffix::List.parse(<<EOS)
180
+ // com : http://en.wikipedia.org/wiki/.com
181
+ com
182
+
183
+ // uk : http://en.wikipedia.org/wiki/.uk
184
+ *.uk
185
+ *.sch.uk
186
+ !bl.uk
187
+ !british-library.uk
188
+ EOS
189
+
190
+ assert_equal PublicSuffix::Rule.factory("com"), list.find("google.com")
191
+ end
192
+
193
+ end