public_suffix_service 0.9.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +0 -1
- data/.travis.yml +11 -0
- data/.yardopts +1 -1
- data/CHANGELOG.md +11 -0
- data/Gemfile.lock +6 -4
- data/README.md +8 -135
- data/Rakefile +23 -31
- data/lib/public_suffix.rb +134 -0
- data/lib/{public_suffix_service → public_suffix}/definitions.txt +282 -1
- data/lib/{public_suffix_service → public_suffix}/domain.rb +50 -50
- data/lib/{public_suffix_service → public_suffix}/errors.rb +12 -12
- data/lib/{public_suffix_service/rule_list.rb → public_suffix/list.rb} +54 -55
- data/lib/{public_suffix_service → public_suffix}/rule.rb +29 -29
- data/lib/public_suffix/rule_list.rb +14 -0
- data/lib/{public_suffix_service → public_suffix}/version.rb +4 -4
- data/lib/public_suffix_service.rb +5 -121
- data/public_suffix_service.gemspec +18 -15
- data/test/acceptance_test.rb +3 -3
- data/test/test_helper.rb +2 -6
- data/test/{public_suffix_service → unit}/domain_test.rb +11 -11
- data/test/unit/errors_test.rb +23 -0
- data/test/unit/list_test.rb +193 -0
- data/test/unit/public_suffix_test.rb +85 -0
- data/test/{public_suffix_service → unit}/rule_test.rb +22 -22
- metadata +66 -59
- data/test/public_suffix_service/errors_test.rb +0 -23
- data/test/public_suffix_service/rule_list_test.rb +0 -193
- data/test/public_suffix_service_test.rb +0 -85
@@ -1,13 +1,13 @@
|
|
1
1
|
#--
|
2
|
-
# Public Suffix
|
2
|
+
# Public Suffix
|
3
3
|
#
|
4
|
-
# Domain
|
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
|
10
|
+
module PublicSuffix
|
11
11
|
|
12
12
|
class Error < StandardError
|
13
13
|
end
|
@@ -18,11 +18,11 @@ module PublicSuffixService
|
|
18
18
|
#
|
19
19
|
# @example
|
20
20
|
#
|
21
|
-
#
|
22
|
-
# # =>
|
21
|
+
# PublicSuffix.parse("nic.test")
|
22
|
+
# # => PublicSuffix::DomainInvalid
|
23
23
|
#
|
24
|
-
#
|
25
|
-
# # =>
|
24
|
+
# PublicSuffix.parse("http://www.nic.it")
|
25
|
+
# # => PublicSuffix::DomainInvalid
|
26
26
|
#
|
27
27
|
# @since 0.6.0
|
28
28
|
#
|
@@ -36,11 +36,11 @@ module PublicSuffixService
|
|
36
36
|
#
|
37
37
|
# @example
|
38
38
|
#
|
39
|
-
#
|
40
|
-
# # =>
|
39
|
+
# PublicSuffix.parse("nic.do")
|
40
|
+
# # => PublicSuffix::DomainNotAllowed
|
41
41
|
#
|
42
|
-
#
|
43
|
-
# # =>
|
42
|
+
# PublicSuffix.parse("www.nic.do")
|
43
|
+
# # => PublicSuffix::Domain
|
44
44
|
#
|
45
45
|
# @since 0.6.0
|
46
46
|
#
|
@@ -50,7 +50,7 @@ module PublicSuffixService
|
|
50
50
|
|
51
51
|
# Backward Compatibility
|
52
52
|
#
|
53
|
-
# @deprecated Use {
|
53
|
+
# @deprecated Use {PublicSuffix::DomainInvalid}.
|
54
54
|
#
|
55
55
|
InvalidDomain = DomainInvalid
|
56
56
|
|
@@ -1,28 +1,28 @@
|
|
1
1
|
#--
|
2
|
-
# Public Suffix
|
2
|
+
# Public Suffix
|
3
3
|
#
|
4
|
-
# Domain
|
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
|
10
|
+
module PublicSuffix
|
11
11
|
|
12
|
-
# A {
|
13
|
-
# or more {
|
12
|
+
# A {PublicSuffix::List} is a collection of one
|
13
|
+
# or more {PublicSuffix::Rule}.
|
14
14
|
#
|
15
|
-
# Given a {
|
16
|
-
# you can add or remove {
|
15
|
+
# Given a {PublicSuffix::List},
|
16
|
+
# you can add or remove {PublicSuffix::Rule},
|
17
17
|
# iterate all items in the list or search for the first rule
|
18
18
|
# which matches a specific domain name.
|
19
19
|
#
|
20
20
|
# # Create a new list
|
21
|
-
# list =
|
21
|
+
# list = PublicSuffix::List.new
|
22
22
|
#
|
23
23
|
# # Push two rules to the list
|
24
|
-
# list <<
|
25
|
-
# list <<
|
24
|
+
# list << PublicSuffix::Rule.factory("it")
|
25
|
+
# list << PublicSuffix::Rule.factory("com")
|
26
26
|
#
|
27
27
|
# # Get the size of the list
|
28
28
|
# list.size
|
@@ -30,52 +30,52 @@ module PublicSuffixService
|
|
30
30
|
#
|
31
31
|
# # Search for the rule matching given domain
|
32
32
|
# list.find("example.com")
|
33
|
-
# # => #<
|
33
|
+
# # => #<PublicSuffix::Rule::Normal>
|
34
34
|
# list.find("example.org")
|
35
35
|
# # => nil
|
36
36
|
#
|
37
|
-
# You can create as many {
|
38
|
-
# The {
|
37
|
+
# You can create as many {PublicSuffix::List} you want.
|
38
|
+
# The {PublicSuffix::List.default} rule list is used
|
39
39
|
# to tokenize and validate a domain.
|
40
40
|
#
|
41
|
-
# {
|
41
|
+
# {PublicSuffix::List} implements +Enumerable+ module.
|
42
42
|
#
|
43
|
-
class
|
43
|
+
class List
|
44
44
|
include Enumerable
|
45
45
|
|
46
|
-
# Gets the
|
46
|
+
# Gets the array of rules.
|
47
47
|
#
|
48
|
-
# @return [Array<
|
49
|
-
attr_reader :
|
48
|
+
# @return [Array<PublicSuffix::Rule::*>]
|
49
|
+
attr_reader :rules
|
50
50
|
|
51
51
|
# Gets the naive index, a hash that with the keys being the first label of
|
52
|
-
# every rule pointing to an array of integers (indexes of the rules in @
|
52
|
+
# every rule pointing to an array of integers (indexes of the rules in @rules).
|
53
53
|
#
|
54
54
|
# @return [Array]
|
55
55
|
attr_reader :indexes
|
56
56
|
|
57
57
|
|
58
|
-
# Initializes an empty {
|
58
|
+
# Initializes an empty {PublicSuffix::List}.
|
59
59
|
#
|
60
60
|
# @yield [self] Yields on self.
|
61
|
-
# @yieldparam [
|
61
|
+
# @yieldparam [PublicSuffix::List] self The newly created instance.
|
62
62
|
#
|
63
63
|
def initialize(&block)
|
64
|
-
@
|
64
|
+
@rules = []
|
65
65
|
@indexes = {}
|
66
66
|
yield(self) if block_given?
|
67
67
|
create_index!
|
68
68
|
end
|
69
69
|
|
70
|
-
# Creates a naive index for +@
|
71
|
-
# us where the elements of +@
|
72
|
-
# {
|
70
|
+
# Creates a naive index for +@rules+. Just a hash that will tell
|
71
|
+
# us where the elements of +@rules+ are relative to its first
|
72
|
+
# {PublicSuffix::Rule::Base#labels} element.
|
73
73
|
#
|
74
|
-
# For instance if @
|
74
|
+
# For instance if @rules[5] and @rules[4] are the only elements of the list
|
75
75
|
# where Rule#labels.first is 'us' @indexes['us'] #=> [5,4], that way in
|
76
76
|
# select we can avoid mapping every single rule against the candidate domain.
|
77
77
|
def create_index!
|
78
|
-
@
|
78
|
+
@rules.map { |l| l.labels.first }.each_with_index do |elm, inx|
|
79
79
|
if !@indexes.has_key?(elm)
|
80
80
|
@indexes[elm] = [inx]
|
81
81
|
else
|
@@ -86,38 +86,37 @@ module PublicSuffixService
|
|
86
86
|
|
87
87
|
# Checks whether two lists are equal.
|
88
88
|
#
|
89
|
-
#
|
90
|
-
# {
|
91
|
-
# in list <tt>one</tt> is available in list <tt>two</tt>,
|
92
|
-
# in the same order.
|
89
|
+
# List <tt>one</tt> is equal to <tt>two</tt>, if <tt>two</tt> is an instance of
|
90
|
+
# {PublicSuffix::List} and each +PublicSuffix::Rule::*+
|
91
|
+
# in list <tt>one</tt> is available in list <tt>two</tt>, in the same order.
|
93
92
|
#
|
94
|
-
# @param [
|
95
|
-
# The
|
93
|
+
# @param [PublicSuffix::List] other
|
94
|
+
# The List to compare.
|
96
95
|
#
|
97
96
|
# @return [Boolean]
|
98
97
|
def ==(other)
|
99
|
-
return false unless other.is_a?(
|
98
|
+
return false unless other.is_a?(List)
|
100
99
|
self.equal?(other) ||
|
101
|
-
self.
|
100
|
+
self.rules == other.rules
|
102
101
|
end
|
103
102
|
alias :eql? :==
|
104
103
|
|
105
104
|
# Iterates each rule in the list.
|
106
105
|
def each(*args, &block)
|
107
|
-
@
|
106
|
+
@rules.each(*args, &block)
|
108
107
|
end
|
109
108
|
|
110
109
|
# Gets the list as array.
|
111
110
|
#
|
112
|
-
# @return [Array<
|
111
|
+
# @return [Array<PublicSuffix::Rule::*>]
|
113
112
|
def to_a
|
114
|
-
@
|
113
|
+
@rules
|
115
114
|
end
|
116
115
|
|
117
116
|
# Adds the given object to the list
|
118
117
|
# and optionally refreshes the rule index.
|
119
118
|
#
|
120
|
-
# @param [
|
119
|
+
# @param [PublicSuffix::Rule::*] rule
|
121
120
|
# The rule to add to the list.
|
122
121
|
# @param [Boolean] index
|
123
122
|
# Set to true to recreate the rule index
|
@@ -128,7 +127,7 @@ module PublicSuffixService
|
|
128
127
|
# @see #create_index!
|
129
128
|
#
|
130
129
|
def add(rule, index = true)
|
131
|
-
@
|
130
|
+
@rules << rule
|
132
131
|
create_index! if index == true
|
133
132
|
self
|
134
133
|
end
|
@@ -138,7 +137,7 @@ module PublicSuffixService
|
|
138
137
|
#
|
139
138
|
# @return [Integer]
|
140
139
|
def size
|
141
|
-
@
|
140
|
+
@rules.size
|
142
141
|
end
|
143
142
|
alias length size
|
144
143
|
|
@@ -146,14 +145,14 @@ module PublicSuffixService
|
|
146
145
|
#
|
147
146
|
# @return [Boolean]
|
148
147
|
def empty?
|
149
|
-
@
|
148
|
+
@rules.empty?
|
150
149
|
end
|
151
150
|
|
152
151
|
# Removes all elements.
|
153
152
|
#
|
154
153
|
# @return [self]
|
155
154
|
def clear
|
156
|
-
@
|
155
|
+
@rules.clear
|
157
156
|
self
|
158
157
|
end
|
159
158
|
|
@@ -180,7 +179,7 @@ module PublicSuffixService
|
|
180
179
|
#
|
181
180
|
# @param [String, #to_s] domain The domain name.
|
182
181
|
#
|
183
|
-
# @return [
|
182
|
+
# @return [PublicSuffix::Rule::*, nil]
|
184
183
|
def find(domain)
|
185
184
|
rules = select(domain)
|
186
185
|
rules.select { |r| r.type == :exception }.first ||
|
@@ -190,14 +189,14 @@ module PublicSuffixService
|
|
190
189
|
# Selects all the rules matching given domain.
|
191
190
|
#
|
192
191
|
# Will use +@indexes+ to try only the rules that share the same first label,
|
193
|
-
# that will speed up things when using +
|
192
|
+
# that will speed up things when using +List.find('foo')+ a lot.
|
194
193
|
#
|
195
194
|
# @param [String, #to_s] domain The domain name.
|
196
195
|
#
|
197
|
-
# @return [Array<
|
196
|
+
# @return [Array<PublicSuffix::Rule::*>]
|
198
197
|
def select(domain)
|
199
198
|
indices = (@indexes[Domain.domain_to_labels(domain).first] || [])
|
200
|
-
@
|
199
|
+
@rules.values_at(*indices).select { |rule| rule.match?(domain) }
|
201
200
|
end
|
202
201
|
|
203
202
|
|
@@ -206,20 +205,20 @@ module PublicSuffixService
|
|
206
205
|
class << self
|
207
206
|
|
208
207
|
# Gets the default rule list.
|
209
|
-
# Initializes a new {
|
210
|
-
# of {
|
208
|
+
# Initializes a new {PublicSuffix::List} parsing the content
|
209
|
+
# of {PublicSuffix::List.default_definition}, if required.
|
211
210
|
#
|
212
|
-
# @return [
|
211
|
+
# @return [PublicSuffix::List]
|
213
212
|
def default
|
214
213
|
@@default ||= parse(default_definition)
|
215
214
|
end
|
216
215
|
|
217
216
|
# Sets the default rule list to +value+.
|
218
217
|
#
|
219
|
-
# @param [
|
218
|
+
# @param [PublicSuffix::List] value
|
220
219
|
# The new rule list.
|
221
220
|
#
|
222
|
-
# @return [
|
221
|
+
# @return [PublicSuffix::List]
|
223
222
|
def default=(value)
|
224
223
|
@@default = value
|
225
224
|
end
|
@@ -233,9 +232,9 @@ module PublicSuffixService
|
|
233
232
|
end
|
234
233
|
|
235
234
|
# Resets the default rule list and reinitialize it
|
236
|
-
# parsing the content of {
|
235
|
+
# parsing the content of {PublicSuffix::List.default_definition}.
|
237
236
|
#
|
238
|
-
# @return [
|
237
|
+
# @return [PublicSuffix::List]
|
239
238
|
def reload
|
240
239
|
self.clear.default
|
241
240
|
end
|
@@ -257,7 +256,7 @@ module PublicSuffixService
|
|
257
256
|
#
|
258
257
|
# @param [String] input The rule list to parse.
|
259
258
|
#
|
260
|
-
# @return [Array<
|
259
|
+
# @return [Array<PublicSuffix::Rule::*>]
|
261
260
|
def parse(input)
|
262
261
|
new do |list|
|
263
262
|
input.each_line do |line|
|
@@ -1,24 +1,24 @@
|
|
1
1
|
#--
|
2
|
-
# Public Suffix
|
2
|
+
# Public Suffix
|
3
3
|
#
|
4
|
-
# Domain
|
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
|
10
|
+
module PublicSuffix
|
11
11
|
|
12
12
|
# A Rule is a special object which holds a single definition
|
13
13
|
# of the Public Suffix List.
|
14
14
|
#
|
15
15
|
# There are 3 types of ruleas, each one represented by a specific
|
16
|
-
# subclass within the +
|
16
|
+
# subclass within the +PublicSuffix::Rule+ namespace.
|
17
17
|
#
|
18
|
-
# To create a new Rule, use the {
|
18
|
+
# To create a new Rule, use the {PublicSuffix::Rule#factory} method.
|
19
19
|
#
|
20
|
-
#
|
21
|
-
# # => #<
|
20
|
+
# PublicSuffix::Rule.factory("ar")
|
21
|
+
# # => #<PublicSuffix::Rule::Normal>
|
22
22
|
#
|
23
23
|
class Rule
|
24
24
|
|
@@ -28,19 +28,19 @@ module PublicSuffixService
|
|
28
28
|
#
|
29
29
|
# @param [String] name The rule definition.
|
30
30
|
#
|
31
|
-
# @return [
|
31
|
+
# @return [PublicSuffix::Rule::*] A rule instance.
|
32
32
|
#
|
33
33
|
# @example Creates a Normal rule
|
34
|
-
#
|
35
|
-
# # => #<
|
34
|
+
# PublicSuffix::Rule.factory("ar")
|
35
|
+
# # => #<PublicSuffix::Rule::Normal>
|
36
36
|
#
|
37
37
|
# @example Creates a Wildcard rule
|
38
|
-
#
|
39
|
-
# # => #<
|
38
|
+
# PublicSuffix::Rule.factory("*.ar")
|
39
|
+
# # => #<PublicSuffix::Rule::Wildcard>
|
40
40
|
#
|
41
41
|
# @example Creates an Exception rule
|
42
|
-
#
|
43
|
-
# # => #<
|
42
|
+
# PublicSuffix::Rule.factory("!congresodelalengua3.ar")
|
43
|
+
# # => #<PublicSuffix::Rule::Exception>
|
44
44
|
#
|
45
45
|
def self.factory(name)
|
46
46
|
klass = case name.to_s[0..0]
|
@@ -63,9 +63,9 @@ module PublicSuffixService
|
|
63
63
|
# of this class is to expose a common interface
|
64
64
|
# for all the available subclasses.
|
65
65
|
#
|
66
|
-
# * {
|
67
|
-
# * {
|
68
|
-
# * {
|
66
|
+
# * {PublicSuffix::Rule::Normal}
|
67
|
+
# * {PublicSuffix::Rule::Exception}
|
68
|
+
# * {PublicSuffix::Rule::Wildcard}
|
69
69
|
#
|
70
70
|
# == Properties
|
71
71
|
#
|
@@ -80,8 +80,8 @@ module PublicSuffixService
|
|
80
80
|
#
|
81
81
|
# Here's an example
|
82
82
|
#
|
83
|
-
#
|
84
|
-
# #<
|
83
|
+
# PublicSuffix::Rule.factory("*.google.com")
|
84
|
+
# #<PublicSuffix::Rule::Wildcard:0x1015c14b0
|
85
85
|
# @labels=["com", "google"],
|
86
86
|
# @name="*.google.com",
|
87
87
|
# @type=:wildcard,
|
@@ -91,13 +91,13 @@ module PublicSuffixService
|
|
91
91
|
# == Rule Creation
|
92
92
|
#
|
93
93
|
# The best way to create a new rule is passing the rule name
|
94
|
-
# to the <tt>
|
94
|
+
# to the <tt>PublicSuffix::Rule.factory</tt> method.
|
95
95
|
#
|
96
|
-
#
|
97
|
-
# # =>
|
96
|
+
# PublicSuffix::Rule.factory("com")
|
97
|
+
# # => PublicSuffix::Rule::Normal
|
98
98
|
#
|
99
|
-
#
|
100
|
-
# # =>
|
99
|
+
# PublicSuffix::Rule.factory("*.com")
|
100
|
+
# # => PublicSuffix::Rule::Wildcard
|
101
101
|
#
|
102
102
|
# This method will detect the rule type and create an instance
|
103
103
|
# from the proper rule class.
|
@@ -112,7 +112,7 @@ module PublicSuffixService
|
|
112
112
|
# can be handled by the current rule.
|
113
113
|
# You can use the <tt>#match?</tt> method.
|
114
114
|
#
|
115
|
-
# rule =
|
115
|
+
# rule = PublicSuffix::Rule.factory("com")
|
116
116
|
#
|
117
117
|
# rule.match?("google.com")
|
118
118
|
# # => true
|
@@ -126,7 +126,7 @@ module PublicSuffixService
|
|
126
126
|
#
|
127
127
|
# When you have the right rule, you can use it to tokenize the domain name.
|
128
128
|
#
|
129
|
-
# rule =
|
129
|
+
# rule = PublicSuffix::Rule.factory("com")
|
130
130
|
#
|
131
131
|
# rule.decompose("google.com")
|
132
132
|
# # => ["google", "com"]
|
@@ -157,7 +157,7 @@ module PublicSuffixService
|
|
157
157
|
|
158
158
|
# Checks whether this rule is equal to <tt>other</tt>.
|
159
159
|
#
|
160
|
-
# @param [
|
160
|
+
# @param [PublicSuffix::Rule::*] other
|
161
161
|
# The rule to compare.
|
162
162
|
#
|
163
163
|
# @return [Boolean]
|
@@ -180,7 +180,7 @@ module PublicSuffixService
|
|
180
180
|
#
|
181
181
|
# @example
|
182
182
|
# rule = Rule.factory("com")
|
183
|
-
# # #<
|
183
|
+
# # #<PublicSuffix::Rule::Normal>
|
184
184
|
# rule.match?("example.com")
|
185
185
|
# # => true
|
186
186
|
# rule.match?("example.net")
|
@@ -201,7 +201,7 @@ module PublicSuffixService
|
|
201
201
|
#
|
202
202
|
# @example
|
203
203
|
# rule = Rule.factory("*.do")
|
204
|
-
# # => #<
|
204
|
+
# # => #<PublicSuffix::Rule::Wildcard>
|
205
205
|
# rule.allow?("example.do")
|
206
206
|
# # => false
|
207
207
|
# rule.allow?("www.example.do")
|