public_suffix 1.4.6 → 1.5.0
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.
- checksums.yaml +4 -4
- data/.ruby-gemset +1 -0
- data/.travis.yml +2 -2
- data/.yardopts +1 -2
- data/CHANGELOG.md +7 -0
- data/LICENSE.txt +1 -1
- data/README.md +65 -55
- data/Rakefile +8 -16
- data/{lib → data}/definitions.txt +1008 -64
- data/lib/public_suffix.rb +3 -10
- data/lib/public_suffix/domain.rb +15 -37
- data/lib/public_suffix/errors.rb +3 -5
- data/lib/public_suffix/list.rb +12 -13
- data/lib/public_suffix/rule.rb +61 -48
- data/lib/public_suffix/version.rb +5 -6
- data/public_suffix.gemspec +6 -6
- data/test/acceptance_test.rb +50 -14
- data/test/unit/list_test.rb +5 -5
- data/test/unit/public_suffix_test.rb +3 -3
- data/test/unit/rule_test.rb +3 -3
- metadata +18 -17
data/lib/public_suffix.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
|
1
|
+
#
|
2
2
|
# Public Suffix
|
3
3
|
#
|
4
4
|
# Domain name parser based on the Public Suffix List.
|
5
5
|
#
|
6
|
-
# Copyright (c) 2009-
|
7
|
-
|
8
|
-
|
6
|
+
# Copyright (c) 2009-2015 Simone Carletti <weppos@weppos.net>
|
7
|
+
#
|
9
8
|
|
10
9
|
require 'public_suffix/domain'
|
11
10
|
require 'public_suffix/version'
|
@@ -13,14 +12,8 @@ require 'public_suffix/errors'
|
|
13
12
|
require 'public_suffix/rule'
|
14
13
|
require 'public_suffix/list'
|
15
14
|
|
16
|
-
|
17
15
|
module PublicSuffix
|
18
16
|
|
19
|
-
NAME = "Public Suffix"
|
20
|
-
GEM = "public_suffix"
|
21
|
-
AUTHORS = ["Simone Carletti <weppos@weppos.net>"]
|
22
|
-
|
23
|
-
|
24
17
|
# Parses +domain+ and returns the
|
25
18
|
# {PublicSuffix::Domain} instance.
|
26
19
|
#
|
data/lib/public_suffix/domain.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
|
1
|
+
#
|
2
2
|
# Public Suffix
|
3
3
|
#
|
4
4
|
# Domain name parser based on the Public Suffix List.
|
5
5
|
#
|
6
|
-
# Copyright (c) 2009-
|
7
|
-
|
8
|
-
|
6
|
+
# Copyright (c) 2009-2015 Simone Carletti <weppos@weppos.net>
|
7
|
+
#
|
9
8
|
|
10
9
|
module PublicSuffix
|
11
10
|
|
@@ -33,6 +32,8 @@ module PublicSuffix
|
|
33
32
|
domain.to_s.split(".").reverse
|
34
33
|
end
|
35
34
|
|
35
|
+
attr_reader :tld, :sld, :trd
|
36
|
+
|
36
37
|
# Creates and returns a new {PublicSuffix::Domain} instance.
|
37
38
|
#
|
38
39
|
# @overload initialize(tld)
|
@@ -88,32 +89,9 @@ module PublicSuffix
|
|
88
89
|
# # => [nil, "google", "com"]
|
89
90
|
#
|
90
91
|
def to_a
|
91
|
-
[trd, sld, tld]
|
92
|
+
[@trd, @sld, @tld]
|
92
93
|
end
|
93
94
|
|
94
|
-
|
95
|
-
# Returns the Top Level Domain part, aka the extension.
|
96
|
-
#
|
97
|
-
# @return [String, nil]
|
98
|
-
def tld
|
99
|
-
@tld
|
100
|
-
end
|
101
|
-
|
102
|
-
# Returns the Second Level Domain part, aka the domain part.
|
103
|
-
#
|
104
|
-
# @return [String, nil]
|
105
|
-
def sld
|
106
|
-
@sld
|
107
|
-
end
|
108
|
-
|
109
|
-
# Returns the Third Level Domain part, aka the subdomain part.
|
110
|
-
#
|
111
|
-
# @return [String, nil]
|
112
|
-
def trd
|
113
|
-
@trd
|
114
|
-
end
|
115
|
-
|
116
|
-
|
117
95
|
# Returns the full domain name.
|
118
96
|
#
|
119
97
|
# @return [String]
|
@@ -127,7 +105,7 @@ module PublicSuffix
|
|
127
105
|
# # => "www.google.com"
|
128
106
|
#
|
129
107
|
def name
|
130
|
-
[trd, sld, tld].
|
108
|
+
[@trd, @sld, @tld].compact.join(".")
|
131
109
|
end
|
132
110
|
|
133
111
|
# Returns a domain-like representation of this object
|
@@ -164,8 +142,9 @@ module PublicSuffix
|
|
164
142
|
# @see #subdomain
|
165
143
|
#
|
166
144
|
def domain
|
167
|
-
|
168
|
-
|
145
|
+
if domain?
|
146
|
+
[@sld, @tld].join(".")
|
147
|
+
end
|
169
148
|
end
|
170
149
|
|
171
150
|
# Returns a domain-like representation of this object
|
@@ -202,8 +181,9 @@ module PublicSuffix
|
|
202
181
|
# @see #domain
|
203
182
|
#
|
204
183
|
def subdomain
|
205
|
-
|
206
|
-
|
184
|
+
if subdomain?
|
185
|
+
[@trd, @sld, @tld].join(".")
|
186
|
+
end
|
207
187
|
end
|
208
188
|
|
209
189
|
# Returns the rule matching this domain
|
@@ -216,7 +196,6 @@ module PublicSuffix
|
|
216
196
|
List.default.find(name)
|
217
197
|
end
|
218
198
|
|
219
|
-
|
220
199
|
# Checks whether <tt>self</tt> looks like a domain.
|
221
200
|
#
|
222
201
|
# This method doesn't actually validate the domain.
|
@@ -246,7 +225,7 @@ module PublicSuffix
|
|
246
225
|
# @see #subdomain?
|
247
226
|
#
|
248
227
|
def domain?
|
249
|
-
!(tld.nil? || sld.nil?)
|
228
|
+
!(@tld.nil? || @sld.nil?)
|
250
229
|
end
|
251
230
|
|
252
231
|
# Checks whether <tt>self</tt> looks like a subdomain.
|
@@ -278,7 +257,7 @@ module PublicSuffix
|
|
278
257
|
# @see #domain?
|
279
258
|
#
|
280
259
|
def subdomain?
|
281
|
-
!(tld.nil? || sld.nil? || trd.nil?)
|
260
|
+
!(@tld.nil? || @sld.nil? || @trd.nil?)
|
282
261
|
end
|
283
262
|
|
284
263
|
# Checks whether <tt>self</tt> is exclusively a domain,
|
@@ -327,7 +306,6 @@ module PublicSuffix
|
|
327
306
|
!r.nil? && r.allow?(name)
|
328
307
|
end
|
329
308
|
|
330
|
-
|
331
309
|
# Checks whether <tt>self</tt> looks like a domain and validates
|
332
310
|
# according to default {List}.
|
333
311
|
#
|
data/lib/public_suffix/errors.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
|
1
|
+
#
|
2
2
|
# Public Suffix
|
3
3
|
#
|
4
4
|
# Domain name parser based on the Public Suffix List.
|
5
5
|
#
|
6
|
-
# Copyright (c) 2009-
|
7
|
-
|
8
|
-
|
6
|
+
# Copyright (c) 2009-2015 Simone Carletti <weppos@weppos.net>
|
7
|
+
#
|
9
8
|
|
10
9
|
module PublicSuffix
|
11
10
|
|
@@ -43,7 +42,6 @@ module PublicSuffix
|
|
43
42
|
class DomainNotAllowed < DomainInvalid
|
44
43
|
end
|
45
44
|
|
46
|
-
|
47
45
|
# Backward Compatibility
|
48
46
|
#
|
49
47
|
# @deprecated Use {PublicSuffix::DomainInvalid}.
|
data/lib/public_suffix/list.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
|
1
|
+
#
|
2
2
|
# Public Suffix
|
3
3
|
#
|
4
4
|
# Domain name parser based on the Public Suffix List.
|
5
5
|
#
|
6
|
-
# Copyright (c) 2009-
|
7
|
-
|
8
|
-
|
6
|
+
# Copyright (c) 2009-2015 Simone Carletti <weppos@weppos.net>
|
7
|
+
#
|
9
8
|
|
10
9
|
module PublicSuffix
|
11
10
|
|
@@ -44,8 +43,7 @@ module PublicSuffix
|
|
44
43
|
include Enumerable
|
45
44
|
|
46
45
|
class << self
|
47
|
-
|
48
|
-
attr_accessor :default_definition
|
46
|
+
attr_writer :default_definition
|
49
47
|
end
|
50
48
|
|
51
49
|
# Gets the default rule list.
|
@@ -76,7 +74,9 @@ module PublicSuffix
|
|
76
74
|
|
77
75
|
# Enables/disables support for private (non-ICANN) domains
|
78
76
|
# Implicitly reloads the list
|
79
|
-
#
|
77
|
+
#
|
78
|
+
# @param [Boolean] value
|
79
|
+
# enable/disable support
|
80
80
|
#
|
81
81
|
# @return [PublicSuffix::List]
|
82
82
|
def self.private_domains=(value)
|
@@ -100,6 +100,8 @@ module PublicSuffix
|
|
100
100
|
self.clear.default
|
101
101
|
end
|
102
102
|
|
103
|
+
DEFAULT_DEFINITION_PATH = File.join(File.dirname(__FILE__), "..", "..", "data", "definitions.txt")
|
104
|
+
|
103
105
|
# Gets the default definition list.
|
104
106
|
# Can be any <tt>IOStream</tt> including a <tt>File</tt>
|
105
107
|
# or a simple <tt>String</tt>.
|
@@ -107,7 +109,7 @@ module PublicSuffix
|
|
107
109
|
#
|
108
110
|
# @return [File]
|
109
111
|
def self.default_definition
|
110
|
-
@default_definition || File.new(
|
112
|
+
@default_definition || File.new(DEFAULT_DEFINITION_PATH, "r:utf-8")
|
111
113
|
end
|
112
114
|
|
113
115
|
# Parse given +input+ treating the content as Public Suffix List.
|
@@ -136,7 +138,6 @@ module PublicSuffix
|
|
136
138
|
end
|
137
139
|
end
|
138
140
|
|
139
|
-
|
140
141
|
# Gets the array of rules.
|
141
142
|
#
|
142
143
|
# @return [Array<PublicSuffix::Rule::*>]
|
@@ -148,7 +149,6 @@ module PublicSuffix
|
|
148
149
|
# @return [Array]
|
149
150
|
attr_reader :indexes
|
150
151
|
|
151
|
-
|
152
152
|
# Initializes an empty {PublicSuffix::List}.
|
153
153
|
#
|
154
154
|
# @yield [self] Yields on self.
|
@@ -250,7 +250,6 @@ module PublicSuffix
|
|
250
250
|
self
|
251
251
|
end
|
252
252
|
|
253
|
-
|
254
253
|
# Returns the most appropriate rule for domain.
|
255
254
|
#
|
256
255
|
# From the Public Suffix List documentation:
|
@@ -276,7 +275,7 @@ module PublicSuffix
|
|
276
275
|
# @return [PublicSuffix::Rule::*, nil]
|
277
276
|
def find(domain)
|
278
277
|
rules = select(domain)
|
279
|
-
rules.
|
278
|
+
rules.detect { |r| r.type == :exception } ||
|
280
279
|
rules.inject { |t,r| t.length > r.length ? t : r }
|
281
280
|
end
|
282
281
|
|
@@ -290,7 +289,7 @@ module PublicSuffix
|
|
290
289
|
# @return [Array<PublicSuffix::Rule::*>]
|
291
290
|
def select(domain)
|
292
291
|
# raise DomainInvalid, "Blank domain"
|
293
|
-
return [] if domain.to_s
|
292
|
+
return [] if domain.to_s =~ /\A\s*\z/
|
294
293
|
# raise DomainInvalid, "`#{domain}' is not expected to contain a scheme"
|
295
294
|
return [] if domain.include?("://")
|
296
295
|
|
data/lib/public_suffix/rule.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
|
1
|
+
#
|
2
2
|
# Public Suffix
|
3
3
|
#
|
4
4
|
# Domain name parser based on the Public Suffix List.
|
5
5
|
#
|
6
|
-
# Copyright (c) 2009-
|
7
|
-
|
8
|
-
|
6
|
+
# Copyright (c) 2009-2015 Simone Carletti <weppos@weppos.net>
|
7
|
+
#
|
9
8
|
|
10
9
|
module PublicSuffix
|
11
10
|
|
@@ -20,37 +19,7 @@ module PublicSuffix
|
|
20
19
|
# PublicSuffix::Rule.factory("ar")
|
21
20
|
# # => #<PublicSuffix::Rule::Normal>
|
22
21
|
#
|
23
|
-
|
24
|
-
|
25
|
-
# Takes the +name+ of the rule, detects the specific rule class
|
26
|
-
# and creates a new instance of that class.
|
27
|
-
# The +name+ becomes the rule +value+.
|
28
|
-
#
|
29
|
-
# @param [String] name The rule definition.
|
30
|
-
#
|
31
|
-
# @return [PublicSuffix::Rule::*] A rule instance.
|
32
|
-
#
|
33
|
-
# @example Creates a Normal rule
|
34
|
-
# PublicSuffix::Rule.factory("ar")
|
35
|
-
# # => #<PublicSuffix::Rule::Normal>
|
36
|
-
#
|
37
|
-
# @example Creates a Wildcard rule
|
38
|
-
# PublicSuffix::Rule.factory("*.ar")
|
39
|
-
# # => #<PublicSuffix::Rule::Wildcard>
|
40
|
-
#
|
41
|
-
# @example Creates an Exception rule
|
42
|
-
# PublicSuffix::Rule.factory("!congresodelalengua3.ar")
|
43
|
-
# # => #<PublicSuffix::Rule::Exception>
|
44
|
-
#
|
45
|
-
def self.factory(name)
|
46
|
-
klass = case name.to_s[0..0]
|
47
|
-
when "*" then "wildcard"
|
48
|
-
when "!" then "exception"
|
49
|
-
else "normal"
|
50
|
-
end
|
51
|
-
const_get(klass.capitalize).new(name)
|
52
|
-
end
|
53
|
-
|
22
|
+
module Rule
|
54
23
|
|
55
24
|
#
|
56
25
|
# = Abstract rule class
|
@@ -138,7 +107,7 @@ module PublicSuffix
|
|
138
107
|
#
|
139
108
|
class Base
|
140
109
|
|
141
|
-
attr_reader :name, :value, :
|
110
|
+
attr_reader :name, :value, :labels
|
142
111
|
|
143
112
|
# Initializes a new rule with name and value.
|
144
113
|
# If value is +nil+, name also becomes the value for this rule.
|
@@ -151,10 +120,25 @@ module PublicSuffix
|
|
151
120
|
def initialize(name, value = nil)
|
152
121
|
@name = name.to_s
|
153
122
|
@value = value || @name
|
154
|
-
@type = self.class.name.split("::").last.downcase.to_sym
|
155
123
|
@labels = Domain.domain_to_labels(@value)
|
156
124
|
end
|
157
125
|
|
126
|
+
#
|
127
|
+
# The rule type name.
|
128
|
+
#
|
129
|
+
# @return [Symbol]
|
130
|
+
#
|
131
|
+
def self.type
|
132
|
+
@type ||= self.name.split("::").last.downcase.to_sym
|
133
|
+
end
|
134
|
+
|
135
|
+
#
|
136
|
+
# @see {type}
|
137
|
+
#
|
138
|
+
def type
|
139
|
+
self.class.type
|
140
|
+
end
|
141
|
+
|
158
142
|
# Checks whether this rule is equal to <tt>other</tt>.
|
159
143
|
#
|
160
144
|
# @param [PublicSuffix::Rule::*] other
|
@@ -170,7 +154,6 @@ module PublicSuffix
|
|
170
154
|
end
|
171
155
|
alias :eql? :==
|
172
156
|
|
173
|
-
|
174
157
|
# Checks if this rule matches +domain+.
|
175
158
|
#
|
176
159
|
# @param [String, #to_s] domain
|
@@ -211,7 +194,6 @@ module PublicSuffix
|
|
211
194
|
!decompose(domain).last.nil?
|
212
195
|
end
|
213
196
|
|
214
|
-
|
215
197
|
# Gets the length of this rule for comparison.
|
216
198
|
# The length usually matches the number of rule +parts+.
|
217
199
|
#
|
@@ -226,7 +208,7 @@ module PublicSuffix
|
|
226
208
|
# @raise [NotImplementedError]
|
227
209
|
# @abstract
|
228
210
|
def parts
|
229
|
-
raise
|
211
|
+
raise(NotImplementedError,"#{self.class}##{__method__} is not implemented")
|
230
212
|
end
|
231
213
|
|
232
214
|
#
|
@@ -238,20 +220,21 @@ module PublicSuffix
|
|
238
220
|
# @raise [NotImplementedError]
|
239
221
|
# @abstract
|
240
222
|
def decompose(domain)
|
241
|
-
raise
|
223
|
+
raise(NotImplementedError,"#{self.class}##{__method__} is not implemented")
|
242
224
|
end
|
243
225
|
|
244
|
-
|
245
226
|
private
|
246
227
|
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
one[ii..one.length]
|
228
|
+
def odiff(one, two)
|
229
|
+
ii = 0
|
230
|
+
|
231
|
+
while(ii < one.size && one[ii] == two[ii])
|
232
|
+
ii += 1
|
253
233
|
end
|
254
234
|
|
235
|
+
one[ii..one.length]
|
236
|
+
end
|
237
|
+
|
255
238
|
end
|
256
239
|
|
257
240
|
class Normal < Base
|
@@ -368,6 +351,36 @@ module PublicSuffix
|
|
368
351
|
|
369
352
|
end
|
370
353
|
|
354
|
+
RULES = {
|
355
|
+
'*' => Wildcard,
|
356
|
+
'!' => Exception
|
357
|
+
}
|
358
|
+
RULES.default = Normal
|
359
|
+
|
360
|
+
# Takes the +name+ of the rule, detects the specific rule class
|
361
|
+
# and creates a new instance of that class.
|
362
|
+
# The +name+ becomes the rule +value+.
|
363
|
+
#
|
364
|
+
# @param [String] name The rule definition.
|
365
|
+
#
|
366
|
+
# @return [PublicSuffix::Rule::*] A rule instance.
|
367
|
+
#
|
368
|
+
# @example Creates a Normal rule
|
369
|
+
# PublicSuffix::Rule.factory("ar")
|
370
|
+
# # => #<PublicSuffix::Rule::Normal>
|
371
|
+
#
|
372
|
+
# @example Creates a Wildcard rule
|
373
|
+
# PublicSuffix::Rule.factory("*.ar")
|
374
|
+
# # => #<PublicSuffix::Rule::Wildcard>
|
375
|
+
#
|
376
|
+
# @example Creates an Exception rule
|
377
|
+
# PublicSuffix::Rule.factory("!congresodelalengua3.ar")
|
378
|
+
# # => #<PublicSuffix::Rule::Exception>
|
379
|
+
#
|
380
|
+
def self.factory(name)
|
381
|
+
RULES[name.to_s[0,1]].new(name)
|
382
|
+
end
|
383
|
+
|
371
384
|
end
|
372
385
|
|
373
386
|
end
|
@@ -1,18 +1,17 @@
|
|
1
|
-
|
1
|
+
#
|
2
2
|
# Public Suffix
|
3
3
|
#
|
4
4
|
# Domain name parser based on the Public Suffix List.
|
5
5
|
#
|
6
|
-
# Copyright (c) 2009-
|
7
|
-
|
8
|
-
|
6
|
+
# Copyright (c) 2009-2015 Simone Carletti <weppos@weppos.net>
|
7
|
+
#
|
9
8
|
|
10
9
|
module PublicSuffix
|
11
10
|
|
12
11
|
module Version
|
13
12
|
MAJOR = 1
|
14
|
-
MINOR =
|
15
|
-
PATCH =
|
13
|
+
MINOR = 5
|
14
|
+
PATCH = 0
|
16
15
|
BUILD = nil
|
17
16
|
|
18
17
|
STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join(".")
|