public_suffix_service 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,12 +1,21 @@
1
1
  = Changelog
2
2
 
3
+ == Release 0.7.0
4
+
5
+ * CHANGED: Using YARD to document the code instead of RDoc.
6
+
7
+ * FIXED: RuleList cache is not recreated when a new rule is appended to the list (#6)
8
+
9
+ * FIXED: PublicSuffixService.valid? should return false if the domain is not defined or not allowed (#4, #5)
10
+
11
+
3
12
  == Release 0.6.0
4
13
 
5
- * ADDED: PublicSuffixService.parse raises DomainNotAllowed when trying to parse a domain name
6
- which exists, but is not allowed by the current definition list (#3)
14
+ * NEW: PublicSuffixService.parse raises DomainNotAllowed when trying to parse a domain name
15
+ which exists, but is not allowed by the current definition list (#3)
7
16
 
8
- PublicSuffixService.parse("nic.do")
9
- # => PublicSuffixService::DomainNotAllowed
17
+ PublicSuffixService.parse("nic.do")
18
+ # => PublicSuffixService::DomainNotAllowed
10
19
 
11
20
  * CHANGED: Renamed PublicSuffixService::InvalidDomain to PublicSuffixService::DomainInvalid
12
21
 
@@ -51,11 +60,11 @@
51
60
 
52
61
  == Release 0.2.0
53
62
 
54
- * ADDED: DomainName#valid?
63
+ * NEW: DomainName#valid?
55
64
 
56
- * ADDED: DomainName#parse and DomainName#parse!
65
+ * NEW: DomainName#parse and DomainName#parse!
57
66
 
58
- * ADDED: DomainName#valid_domain? and DomainName#valid_subdomain?
67
+ * NEW: DomainName#valid_domain? and DomainName#valid_subdomain?
59
68
 
60
69
  * CHANGED: Make sure RuleList lookup is only performed once.
61
70
 
data/Rakefile CHANGED
@@ -28,13 +28,6 @@ Rake::TestTask.new do |t|
28
28
  t.verbose = true
29
29
  end
30
30
 
31
- # Generate documentation
32
- Rake::RDocTask.new do |rd|
33
- rd.main = "README.rdoc"
34
- rd.rdoc_files.include("*.rdoc", "lib/**/*.rb")
35
- rd.rdoc_dir = "rdoc"
36
- end
37
-
38
31
  # Run test by default.
39
32
  task :default => ["test"]
40
33
 
@@ -73,7 +66,7 @@ spec = Gem::Specification.new do |s|
73
66
  # s.add_dependency("some_other_gem", "~> 0.1.0")
74
67
 
75
68
  # If your tests use any gems, include them here
76
- s.add_development_dependency("mocha")
69
+ s.add_development_dependency("rr")
77
70
  end
78
71
 
79
72
  # This task actually builds the gem. We also regenerate a static
@@ -96,25 +89,67 @@ task :clean => [:clobber] do
96
89
  end
97
90
 
98
91
  desc "Remove any generated file"
99
- task :clobber => [:clobber_rdoc, :clobber_rcov, :clobber_package]
92
+ task :clobber => [:clobber_rdoc, :clobber_package]
100
93
 
101
94
  desc "Package the library and generates the gemspec"
102
95
  task :package => [:gemspec]
103
96
 
97
+
98
+ desc "Generate RDoc documentation"
99
+ Rake::RDocTask.new do |r|
100
+ r.main = "README.rdoc"
101
+ r.rdoc_files.include("*.rdoc", "lib/**/*.rb")
102
+ r.rdoc_dir = "rdoc"
103
+ end
104
+
105
+ namespace :rdoc do
106
+ desc "Publish RDoc documentation to the site"
107
+ task :publish => [:clobber_rdoc, :rdoc] do
108
+ ENV["username"] || raise(ArgumentError, "Missing ssh username")
109
+ sh "rsync -avz --delete rdoc/ #{ENV["username"]}@code:/var/www/apps/code/#{PKG_NAME}/rdoc"
110
+ end
111
+ end
112
+
113
+
114
+ begin
115
+ require "yard"
116
+ require "yard/rake/yardoc_task"
117
+
118
+ YARD::Rake::YardocTask.new(:yardoc)
119
+
120
+ namespace :yardoc do
121
+ desc "Publish YARD documentation to the site"
122
+ task :publish => ["yardoc:clobber", "yardoc"] do
123
+ ENV["username"] || raise(ArgumentError, "Missing ssh username")
124
+ sh "rsync -avz --delete yardoc/ #{ENV["username"]}@code:/var/www/apps/code/#{PKG_NAME}/api"
125
+ end
126
+
127
+ desc "Remove YARD products"
128
+ task :clobber do
129
+ rm_r "yardoc" rescue nil
130
+ end
131
+ end
132
+
133
+ task :clobber => "yardoc:clobber"
134
+ rescue LoadError
135
+ puts "YARD is not available"
136
+ end
137
+
138
+
104
139
  begin
105
140
  require "rcov/rcovtask"
106
141
 
107
142
  desc "Create a code coverage report"
108
143
  Rcov::RcovTask.new do |t|
109
144
  t.test_files = FileList["test/**/*_test.rb"]
110
- t.ruby_opts << "-Itest -x mocha,rcov,Rakefile"
145
+ t.ruby_opts << "-Itest -x rr,rcov,Rakefile"
111
146
  t.verbose = true
112
147
  end
113
148
  rescue LoadError
114
- task :clobber_rcov
115
149
  puts "RCov is not available"
116
150
  end
117
151
 
152
+
118
153
  desc "Open an irb session preloaded with this library"
119
154
  task :console do
120
155
  sh "irb -rubygems -I lib -r public_suffix_service.rb"
@@ -131,11 +166,6 @@ rescue LoadError
131
166
  puts "CodeStatistics (Rails) is not available"
132
167
  end
133
168
 
134
- desc "Publish documentation to the site"
135
- task :publish_rdoc => [:clobber_rdoc, :rdoc] do
136
- ENV["username"] || raise(ArgumentError, "Missing ssh username")
137
- sh "rsync -avz --delete rdoc/ #{ENV["username"]}@code:/var/www/apps/code/#{PKG_NAME}/api"
138
- end
139
169
 
140
170
  desc <<-DESC
141
171
  Downloads the Public Suffix List file from the repository \
@@ -28,35 +28,48 @@ module PublicSuffixService
28
28
  AUTHORS = ["Simone Carletti <weppos@weppos.net>"]
29
29
 
30
30
 
31
- # Parses <tt>domain</tt> and returns the
32
- # PubliSuffixService::Domain instance.
31
+ # Parses +domain+ and returns the
32
+ # {PublicSuffixService::Domain} instance.
33
33
  #
34
- # domain - The String domain name to parse.
34
+ # Parsing uses the default {PublicSuffixService::RuleList}.
35
35
  #
36
- # Examples
36
+ # @param [String, #to_s] domain
37
+ # The domain name to parse.
37
38
  #
39
+ # @return [PublicSuffixService::Domain]
40
+ #
41
+ # @example Parse a valid domain
38
42
  # PublicSuffixService.parse("google.com")
39
43
  # # => #<PubliSuffixService::Domain ...>
40
44
  #
45
+ # @example Parse a valid subdomain
41
46
  # PublicSuffixService.parse("www.google.com")
42
47
  # # => #<PubliSuffixService::Domain ...>
43
- #
44
- # PublicSuffixService.parse("http://www.google.com")
45
- # # => PublicSuffixService::DomainInvalid
46
- #
48
+ #
49
+ # @example Parse an invalid domain
47
50
  # PublicSuffixService.parse("x.yz")
48
51
  # # => PublicSuffixService::DomainInvalid
49
52
  #
50
- # Raises PublicSuffixService::Error if domain is not a valid domain
53
+ # @example Parse an URL (not supported, only domains)
54
+ # PublicSuffixService.parse("http://www.google.com")
55
+ # # => PublicSuffixService::DomainInvalid
56
+ #
57
+ # @raise [PublicSuffixService::Error]
58
+ # If domain is not a valid domain.
59
+ # @raise [PublicSuffixService::DomainNotAllowed]
60
+ # If a rule for +domain+ is found, but the rule
61
+ # doesn't allow +domain+.
51
62
  #
52
- # Returns the PubliSuffixService::Domain domain.
53
63
  def self.parse(domain)
54
- rule = RuleList.default.find(domain) || raise(DomainInvalid, "`#{domain}' is not a valid domain")
64
+ rule = RuleList.default.find(domain)
65
+ if rule.nil?
66
+ raise(DomainInvalid, "`#{domain}' is not a valid domain")
67
+ end
68
+ if !rule.allow?(domain)
69
+ raise DomainNotAllowed, "`#{domain}' is not allowed according to Registry policy"
70
+ end
55
71
 
56
72
  left, right = rule.decompose(domain)
57
- if right.nil?
58
- raise DomainNotAllowed, "Rule `#{rule.name}' doesn't allow `#{domain}'"
59
- end
60
73
 
61
74
  parts = left.split(".")
62
75
  # If we have 0 parts left, there is just a tld and no domain or subdomain
@@ -69,30 +82,42 @@ module PublicSuffixService
69
82
  Domain.new(tld, sld, trd)
70
83
  end
71
84
 
72
- # Checks whether <tt>domain</tt> is a valid domain name.
85
+ # Checks whether +domain+ is assigned and allowed,
86
+ # without actually parsing it.
73
87
  #
74
88
  # This method doesn't care whether domain is a domain or subdomain.
75
- # The check is performed using the default PublicSuffixService::RuleList.
89
+ # The check is performed using the default {PublicSuffixService::RuleList}.
76
90
  #
77
- # domain - The String domain name to parse.
91
+ # @param [String, #to_s] domain
92
+ # The domain name to check.
78
93
  #
79
- # Examples
94
+ # @return [Boolean]
80
95
  #
81
- # PublicSuffixService.valid?("google.com")
96
+ # @example Check a valid domain
97
+ # PublicSuffixService.valid?("example.com")
82
98
  # # => true
83
- #
84
- # PublicSuffixService.valid?("www.google.com")
99
+ #
100
+ # @example Check a valid subdomain
101
+ # PublicSuffixService.valid?("www.example.com")
85
102
  # # => true
86
- #
87
- # PublicSuffixService.valid?("http://www.google.com")
103
+ #
104
+ # @example Check a not-assigned domain
105
+ # PublicSuffixService.valid?("example.zip")
88
106
  # # => false
89
- #
90
- # PublicSuffixService.valid?("x.yz")
107
+ #
108
+ # @example Check a not-allowed domain
109
+ # PublicSuffixService.valid?("example.do")
110
+ # # => false
111
+ # PublicSuffixService.valid?("www.example.do")
112
+ # # => true
113
+ #
114
+ # @example Check an URL (which is not a valid domain)
115
+ # PublicSuffixService.valid?("http://www.example.com")
91
116
  # # => false
92
117
  #
93
- # Returns Boolean.
94
118
  def self.valid?(domain)
95
- !RuleList.default.find(domain).nil?
119
+ rule = RuleList.default.find(domain)
120
+ !rule.nil? && rule.allow?(domain)
96
121
  end
97
122
 
98
123
  end
@@ -18,79 +18,127 @@ module PublicSuffixService
18
18
 
19
19
  class Domain
20
20
 
21
- # Splits a string into its possible labels as a domain in reverse order
22
- # from the input string.
21
+ # Splits a string into its possible labels
22
+ # as a domain in reverse order from the input string.
23
+ #
23
24
  # The input is not validated, but it is assumed to be a valid domain.
24
25
  #
25
- # domain - The String domain to be split.
26
+ # @param [String, #to_s] domain
27
+ # The domain name to split.
28
+ #
29
+ # @return [Array<String>]
26
30
  #
27
- # Examples
31
+ # @example
28
32
  #
29
- # domain_to_labels('google.com.uk')
30
- # # => ['uk', 'com', 'google']
33
+ # domain_to_labels('google.com')
34
+ # # => ['com', 'google']
35
+ #
36
+ # domain_to_labels('google.co.uk')
37
+ # # => ['uk', 'co', 'google']
31
38
  #
32
- # Returns an Array of String representing the possible labels.
33
39
  def self.domain_to_labels(domain)
34
40
  domain.to_s.split(".").reverse
35
41
  end
36
42
 
43
+ # Creates and returns a new {PublicSuffixService::Domain} instance.
44
+ #
45
+ # @overload initialize(tld)
46
+ # Initializes with a +tld+.
47
+ # @param [String] tld The TLD (extension)
48
+ # @overload initialize(tld, sld)
49
+ # Initializes with a +tld+ and +sld+.
50
+ # @param [String] tld The TLD (extension)
51
+ # @param [String] sld The TRD (domain)
52
+ # @overload initialize(tld, sld, trd)
53
+ # Initializes with a +tld+, +sld+ and +trd+.
54
+ # @param [String] tld The TLD (extension)
55
+ # @param [String] sld The SLD (domain)
56
+ # @param [String] tld The TRD (subdomain)
57
+ #
58
+ # @yield [self] Yields on self.
59
+ # @yieldparam [PublicSuffixService::Domain] self The newly creates instance
60
+ #
61
+ # @example Initialize with a TLD
62
+ # PublicSuffixService::Domain.new("com")
63
+ # # => #<PublicSuffixService::Domain @tld="com">
64
+ #
65
+ # @example Initialize with a TLD and SLD
66
+ # PublicSuffixService::Domain.new("com", "example")
67
+ # # => #<PublicSuffixService::Domain @tld="com", @trd=nil>
68
+ #
69
+ # @example Initialize with a TLD, SLD and TRD
70
+ # PublicSuffixService::Domain.new("com", "example", "wwww")
71
+ # # => #<PublicSuffixService::Domain @tld="com", @trd=nil, @sld="example">
72
+ #
37
73
  def initialize(*args, &block)
38
74
  @tld, @sld, @trd = args
39
75
  yield(self) if block_given?
40
76
  end
41
77
 
42
- # Gets a String representation of this object.
78
+ # Returns a string representation of this object.
43
79
  #
44
- # Returns a String with the domain name.
80
+ # @return [String]
45
81
  def to_s
46
82
  name
47
83
  end
48
84
 
85
+ # Returns an array containing the domain parts.
86
+ #
87
+ # @return [Array<String, nil>]
88
+ #
89
+ # @example
90
+ #
91
+ # PublicSuffixService::Domain.new("google.com").to_a
92
+ # # => [nil, "google", "com"]
93
+ #
94
+ # PublicSuffixService::Domain.new("www.google.com").to_a
95
+ # # => [nil, "google", "com"]
96
+ #
49
97
  def to_a
50
98
  [trd, sld, tld]
51
99
  end
52
100
 
53
101
 
54
- # Gets the Top Level Domain part, aka the extension.
102
+ # Returns the Top Level Domain part, aka the extension.
55
103
  #
56
- # Returns a String if tld is set, nil otherwise.
104
+ # @return [String, nil]
57
105
  def tld
58
106
  @tld
59
107
  end
60
108
 
61
- # Gets the Second Level Domain part, aka the domain part.
109
+ # Returns the Second Level Domain part, aka the domain part.
62
110
  #
63
- # Returns a String if sld is set, nil otherwise.
111
+ # @return [String, nil]
64
112
  def sld
65
113
  @sld
66
114
  end
67
115
 
68
- # Gets the Third Level Domain part, aka the subdomain part.
116
+ # Returns the Third Level Domain part, aka the subdomain part.
69
117
  #
70
- # Returns a String if trd is set, nil otherwise.
118
+ # @return [String, nil]
71
119
  def trd
72
120
  @trd
73
121
  end
74
122
 
75
123
 
76
- # Gets the domain name.
124
+ # Returns the full domain name.
77
125
  #
78
- # Examples
126
+ # @return [String]
79
127
  #
128
+ # @example Gets the domain name of a domain
80
129
  # PublicSuffixService::Domain.new("com", "google").name
81
130
  # # => "google.com"
82
131
  #
132
+ # @example Gets the domain name of a subdomain
83
133
  # PublicSuffixService::Domain.new("com", "google", "www").name
84
134
  # # => "www.google.com"
85
135
  #
86
- # Returns a String with the domain name.
87
136
  def name
88
137
  [trd, sld, tld].reject { |part| part.nil? }.join(".")
89
138
  end
90
139
 
91
140
  # Returns a domain-like representation of this object
92
- # if the object is a <tt>domain?</tt>,
93
- # <tt>nil</tt> otherwise.
141
+ # if the object is a {#domain?}, <tt>nil</tt> otherwise.
94
142
  #
95
143
  # PublicSuffixService::Domain.new("com").domain
96
144
  # # => nil
@@ -117,15 +165,18 @@ module PublicSuffixService
117
165
  # PublicSuffixService::Domain.new("com", "google", "www").sld
118
166
  # # => "google"
119
167
  #
120
- # Returns a String or nil.
168
+ # @return [String]
169
+ #
170
+ # @see #domain?
171
+ # @see #subdomain
172
+ #
121
173
  def domain
122
174
  return unless domain?
123
175
  [sld, tld].join(".")
124
176
  end
125
177
 
126
178
  # Returns a domain-like representation of this object
127
- # if the object is a <tt>subdomain?</tt>,
128
- # <tt>nil</tt> otherwise.
179
+ # if the object is a {#subdomain?}, <tt>nil</tt> otherwise.
129
180
  #
130
181
  # PublicSuffixService::Domain.new("com").subdomain
131
182
  # # => nil
@@ -152,17 +203,22 @@ module PublicSuffixService
152
203
  # PublicSuffixService::Domain.new("com", "google", "www").trd
153
204
  # # => "www"
154
205
  #
155
- # Returns a String or nil.
206
+ # @return [String]
207
+ #
208
+ # @see #subdomain?
209
+ # @see #domain
210
+ #
156
211
  def subdomain
157
212
  return unless subdomain?
158
213
  [trd, sld, tld].join(".")
159
214
  end
160
215
 
161
- # Gets the rule matching this domain
162
- # in the default PublicSuffixService::RuleList.
216
+ # Returns the rule matching this domain
217
+ # in the default {PublicSuffixService::RuleList}.
163
218
  #
164
- # Returns the PublicSuffixService::Rule::Base instance
165
- # if a rule matches current domain, nil if no rule is found.
219
+ # @return [PublicSuffixService::Rule::Base, nil]
220
+ # The rule instance a rule matches current domain,
221
+ # nil if no rule is found.
166
222
  def rule
167
223
  RuleList.default.find(name)
168
224
  end
@@ -172,11 +228,13 @@ module PublicSuffixService
172
228
  #
173
229
  # This method doesn't actually validate the domain.
174
230
  # It only checks whether the instance contains
175
- # a value for the <tt>tld</tt> and <tt>sld</tt> attributes.
231
+ # a value for the {#tld} and {#sld} attributes.
176
232
  # If you also want to validate the domain,
177
- # use <tt>#valid_domain?</tt> instead.
233
+ # use {#valid_domain?} instead.
234
+ #
235
+ # @return [Boolean]
178
236
  #
179
- # Examples
237
+ # @example
180
238
  #
181
239
  # PublicSuffixService::Domain.new("com").domain?
182
240
  # # => false
@@ -192,7 +250,8 @@ module PublicSuffixService
192
250
  # PublicSuffixService::Domain.new("zip", "google").domain?
193
251
  # # => true
194
252
  #
195
- # Returns Boolean.
253
+ # @see #subdomain?
254
+ #
196
255
  def domain?
197
256
  !(tld.nil? || sld.nil?)
198
257
  end
@@ -201,11 +260,13 @@ module PublicSuffixService
201
260
  #
202
261
  # This method doesn't actually validate the subdomain.
203
262
  # It only checks whether the instance contains
204
- # a value for the <tt>tld</tt>, <tt>sld</tt> and <tt>trd</tt> attributes.
263
+ # a value for the {#tld}, {#sld} and {#trd} attributes.
205
264
  # If you also want to validate the domain,
206
- # use <tt>#valid_subdomain?</tt> instead.
265
+ # use {#valid_subdomain?} instead.
207
266
  #
208
- # Examples
267
+ # @return [Boolean]
268
+ #
269
+ # @example
209
270
  #
210
271
  # PublicSuffixService::Domain.new("com").subdomain?
211
272
  # # => false
@@ -221,7 +282,8 @@ module PublicSuffixService
221
282
  # PublicSuffixService::Domain.new("zip", "google", "www").subdomain?
222
283
  # # => true
223
284
  #
224
- # Returns Boolean.
285
+ # @see #domain?
286
+ #
225
287
  def subdomain?
226
288
  !(tld.nil? || sld.nil? || trd.nil?)
227
289
  end
@@ -229,35 +291,56 @@ module PublicSuffixService
229
291
  # Checks whether <tt>self</tt> is exclusively a domain,
230
292
  # and not a subdomain.
231
293
  #
232
- # Returns Boolean.
294
+ # @return [Boolean]
233
295
  def is_a_domain?
234
296
  domain? && !subdomain?
235
297
  end
236
298
 
237
299
  # Checks whether <tt>self</tt> is exclusively a subdomain.
238
300
  #
239
- # Returns Boolean.
301
+ # @return [Boolean]
240
302
  def is_a_subdomain?
241
303
  subdomain?
242
304
  end
243
305
 
244
- # Checks whether <tt>self</tt> is valid
245
- # according to default <tt>RuleList</tt>.
306
+ # Checks whether <tt>self</tt> is assigned and allowed
307
+ # according to default {RuleList}.
246
308
  #
247
- # Note: this method triggers a new rule lookup in the default RuleList,
309
+ # This method triggers a new rule lookup in the default {RuleList},
248
310
  # which is a quite intensive task.
249
311
  #
250
- # Returns Boolean.
312
+ # @return [Boolean]
313
+ #
314
+ # @example Check a valid domain
315
+ # Domain.new("com", "example").valid?
316
+ # # => true
317
+ #
318
+ # @example Check a valid subdomain
319
+ # Domain.new("com", "example", "www").valid?
320
+ # # => true
321
+ #
322
+ # @example Check a not-assigned domain
323
+ # Domain.new("zip", "example").valid?
324
+ # # => false
325
+ #
326
+ # @example Check a not-allowed domain
327
+ # Domain.new("do", "example").valid?
328
+ # # => false
329
+ # Domain.new("do", "example", "www").valid?
330
+ # # => true
331
+ #
251
332
  def valid?
252
- !rule.nil?
333
+ r = rule
334
+ !r.nil? && r.allow?(name)
253
335
  end
254
336
 
337
+
255
338
  # Checks whether <tt>self</tt> looks like a domain and validates
256
- # according to default <tt>RuleList</tt>.
339
+ # according to default {RuleList}.
257
340
  #
258
- # See also <tt>#domain?</tt> and <tt>#valid?</tt>.
341
+ # @return [Boolean]
259
342
  #
260
- # Examples
343
+ # @example
261
344
  #
262
345
  # PublicSuffixService::Domain.new("com").domain?
263
346
  # # => false
@@ -272,17 +355,19 @@ module PublicSuffixService
272
355
  # PublicSuffixService::Domain.new("zip", "google").false?
273
356
  # # => true
274
357
  #
275
- # Returns true if this instance looks like a domain and is valid.
358
+ # @see #domain?
359
+ # @see #valid?
360
+ #
276
361
  def valid_domain?
277
362
  domain? && valid?
278
363
  end
279
364
 
280
365
  # Checks whether <tt>self</tt> looks like a subdomain and validates
281
- # according to default <tt>RuleList</tt>.
366
+ # according to default {RuleList}.
282
367
  #
283
- # See also <tt>#subdomain?</tt> and <tt>#valid?</tt>.
368
+ # @return [Boolean]
284
369
  #
285
- # Examples
370
+ # @example
286
371
  #
287
372
  # PublicSuffixService::Domain.new("com").subdomain?
288
373
  # # => false
@@ -297,7 +382,9 @@ module PublicSuffixService
297
382
  # PublicSuffixService::Domain.new("zip", "google", "www").subdomain?
298
383
  # # => false
299
384
  #
300
- # Returns true if this instance looks like a domain and is valid.
385
+ # @see #subdomain?
386
+ # @see #valid?
387
+ #
301
388
  def valid_subdomain?
302
389
  subdomain? && valid?
303
390
  end