public_suffix_service 0.4.0 → 0.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.
data/CHANGELOG.rdoc CHANGED
@@ -1,6 +1,13 @@
1
1
  = Changelog
2
2
 
3
3
 
4
+ == Release 0.5.0
5
+
6
+ * CHANGED: Improve documentation for Domain#domain and Domain#subdomain (#1).
7
+
8
+ * CHANGED: Performance improvements (#2).
9
+
10
+
4
11
  == Release 0.4.0
5
12
 
6
13
  * CHANGED: Rename library from DomainName to PublicSuffixService to reduce the probability of name conflicts.
data/README.rdoc CHANGED
@@ -1,13 +1,13 @@
1
1
  = Public Suffix Service
2
2
 
3
- Public Suffix Service is a Ruby domain name parser based on the Public Suffix List available at http://publicsuffix.org.
3
+ *Public Suffix Service* is a Ruby domain name parser based on the {Public Suffix List}[http://publicsuffix.org].
4
4
 
5
5
 
6
6
  == What is the Public Suffix List?
7
7
 
8
- The Public Suffix List is a cross-vendor initiative to provide an accurate list of domain name suffixes.
8
+ The *Public Suffix List* is a cross-vendor initiative to provide an accurate list of domain name suffixes.
9
9
 
10
- The Public Suffix List is an initiative of the Mozilla Project, but is maintained as a community resource. It is available for use in any software, but was originally created to meet the needs of browser manufacturers.
10
+ The *Public Suffix List* is an initiative of the Mozilla Project, but is maintained as a community resource. It is available for use in any software, but was originally created to meet the needs of browser manufacturers.
11
11
 
12
12
  A "public suffix" is one under which Internet users can directly register names. Some examples of public suffixes are ".com", ".co.uk" and "pvt.k12.wy.us". The Public Suffix List is a list of all known public suffixes.
13
13
 
@@ -32,22 +32,22 @@ http://stackoverflow.com/questions/288810/get-the-subdomain-from-a-url
32
32
 
33
33
  == Requirements
34
34
 
35
- * Ruby >= 1.8.6 (not tested with previous versions)
35
+ * Ruby >= 1.8.6 or Ruby 1.9.x
36
36
 
37
- Successfully tested against
37
+ Successfully tested with the following interpreters
38
38
 
39
39
  * Ruby 1.8.6 / 1.8.7 / 1.9.1
40
- * MacRuby 0.4
40
+ * MacRuby
41
41
  * Ruby Enterprise Edition
42
42
 
43
43
 
44
44
  == Installation
45
45
 
46
- Via RubyGems
46
+ The best way to install *Public Suffix Service* is via {RubyGems}[http://www.rubygems.org].
47
47
 
48
48
  $ gem install public_suffix_service
49
49
 
50
- You might need administrator privileges on your system to install it.
50
+ You might need administrator privileges on your system to install the Gem.
51
51
 
52
52
 
53
53
  == Usage
@@ -55,22 +55,32 @@ You might need administrator privileges on your system to install it.
55
55
  Example domain without subdomains.
56
56
 
57
57
  domain = PublicSuffixService.parse("google.com")
58
+ # => #<PublicSuffixService::Domain>
58
59
  domain.tld
59
60
  # => "com"
60
- domain.domain
61
+ domain.sld
61
62
  # => "google"
63
+ domain.trd
64
+ # => nil
65
+ domain.domain
66
+ # => "google.com"
62
67
  domain.subdomain
63
68
  # => nil
64
69
 
65
70
  Example domain with subdomains.
66
71
 
67
72
  domain = PublicSuffixService.parse("www.google.com")
73
+ # => #<PublicSuffixService::Domain>
68
74
  domain.tld
69
75
  # => "com"
70
- domain.domain
76
+ domain.sld
71
77
  # => "google"
72
- domain.subdomain
78
+ domain.trd
73
79
  # => "www"
80
+ domain.domain
81
+ # => "google.com"
82
+ domain.subdomain
83
+ # => "google.com"
74
84
 
75
85
  Simple validation example.
76
86
 
@@ -89,6 +99,21 @@ Simple validation example.
89
99
  Author:: {Simone Carletti}[http://www.simonecarletti.com/] <weppos@weppos.net>
90
100
 
91
101
 
102
+ == FeedBack and Bug reports
103
+
104
+ If you use this library and find yourself missing any functionality I have missed, please {let me know}[mailto:weppos@weppos.net].
105
+
106
+ Bug reports and Feature suggestions {are welcomed}[http://github.com/weppos/public_suffix_service/issues].
107
+
108
+
109
+ == More
110
+
111
+ * {Homepage}[http://www.simonecarletti.com/code/public_suffix_service]
112
+ * {Repository}[http://github.com/weppos/public_suffix_service]
113
+ * {API Documentation}[http://www.simonecarletti.com/code/public_suffix_service/api/] (RDoc)
114
+ * {Introducing the Public Suffix List library for Ruby}[http://www.simonecarletti.com/blog/2010/06/public-suffix-list-library-for-ruby/]
115
+
116
+
92
117
  == Changelog
93
118
 
94
119
  See the CHANGELOG.rdoc file for details.
@@ -96,4 +121,5 @@ See the CHANGELOG.rdoc file for details.
96
121
 
97
122
  == License
98
123
 
99
- Copyright (c) 2009-2010 Simone Carletti, DomainName is released under the MIT license.
124
+ Copyright (c) 2009-2010 Simone Carletti,
125
+ *Public Suffix Service* is released under the MIT license.
data/Rakefile CHANGED
@@ -1,10 +1,14 @@
1
1
  require "rubygems"
2
2
  require "rake/testtask"
3
- require "rake/rdoctask"
4
3
  require "rake/gempackagetask"
4
+ begin
5
+ require "hanna/rdoctask"
6
+ rescue LoadError
7
+ require "rake/rdoctask"
8
+ end
5
9
 
6
10
  $:.unshift(File.dirname(__FILE__) + "/lib")
7
- require 'public_suffix_service'
11
+ require "public_suffix_service"
8
12
 
9
13
 
10
14
  # Common package properties
@@ -49,8 +53,8 @@ spec = Gem::Specification.new do |s|
49
53
  s.email = "weppos@weppos.net"
50
54
  s.homepage = "http://www.simonecarletti.com/code/public_suffix_service"
51
55
  s.description = <<-EOD
52
- Intelligent Domain Name parser based in the Public Suffic List. \
53
- Domain Name can parse and decompose a domain name into top level domain, \
56
+ Intelligent domain name parser based in the Public Suffic List. \
57
+ PublicSuffixService can parse and decompose a domain name into top level domain, \
54
58
  domain and subdomains.
55
59
  EOD
56
60
 
@@ -86,9 +90,9 @@ task :gemspec do
86
90
  File.open(file, "w") {|f| f << spec.to_ruby }
87
91
  end
88
92
 
89
- desc "Remove any temporary products, including gemspec."
93
+ desc "Remove any temporary products, including gemspec"
90
94
  task :clean => [:clobber] do
91
- rm "#{spec.name}.gemspec"
95
+ rm "#{spec.name}.gemspec" if File.file?("#{spec.name}.gemspec")
92
96
  end
93
97
 
94
98
  desc "Remove any generated file"
@@ -100,7 +104,7 @@ task :package => [:gemspec]
100
104
  begin
101
105
  require "rcov/rcovtask"
102
106
 
103
- desc "Create a code coverage report."
107
+ desc "Create a code coverage report"
104
108
  Rcov::RcovTask.new do |t|
105
109
  t.test_files = FileList["test/**/*_test.rb"]
106
110
  t.ruby_opts << "-Itest -x mocha,rcov,Rakefile"
@@ -117,7 +121,7 @@ task :console do
117
121
  end
118
122
 
119
123
  begin
120
- require 'code_statistics'
124
+ require "code_statistics"
121
125
  desc "Show library's code statistics"
122
126
  task :stats do
123
127
  CodeStatistics.new(["Public Suffix Service", "lib"],
@@ -127,6 +131,13 @@ rescue LoadError
127
131
  puts "CodeStatistics (Rails) is not available"
128
132
  end
129
133
 
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
+
140
+
130
141
  Dir["tasks/**/*.rake"].each do |file|
131
142
  load(file)
132
143
  end
@@ -18,6 +18,22 @@ 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.
23
+ # The input is not validated, but it is assumed to be a valid domain.
24
+ #
25
+ # domain - The String domain to be split.
26
+ #
27
+ # Examples
28
+ #
29
+ # domain_to_labels('google.com.uk')
30
+ # # => ['uk', 'com', 'google']
31
+ #
32
+ # Returns an Array of String representing the possible labels.
33
+ def self.domain_to_labels(domain)
34
+ domain.to_s.split(".").reverse
35
+ end
36
+
21
37
  def initialize(*args, &block)
22
38
  @tld, @sld, @trd = args
23
39
  yield(self) if block_given?
@@ -61,10 +77,10 @@ module PublicSuffixService
61
77
  #
62
78
  # Examples
63
79
  #
64
- # DomainName.new("com", "google").name
80
+ # PublicSuffixService::Domain.new("com", "google").name
65
81
  # # => "google.com"
66
82
  #
67
- # DomainName.new("com", "google", "www").name
83
+ # PublicSuffixService::Domain.new("com", "google", "www").name
68
84
  # # => "www.google.com"
69
85
  #
70
86
  # Returns a String with the domain name.
@@ -75,23 +91,78 @@ module PublicSuffixService
75
91
  # Returns a domain-like representation of this object
76
92
  # if the object is a <tt>domain?</tt>,
77
93
  # <tt>nil</tt> otherwise.
94
+ #
95
+ # PublicSuffixService::Domain.new("com").domain
96
+ # # => nil
97
+ #
98
+ # PublicSuffixService::Domain.new("com", "google").domain
99
+ # # => "google.com"
100
+ #
101
+ # PublicSuffixService::Domain.new("com", "google", "www").domain
102
+ # # => "www.google.com"
103
+ #
104
+ # This method doesn't validate the input. It handles the domain
105
+ # as a valid domain name and simply applies the necessary transformations.
106
+ #
107
+ # # This is an invalid domain
108
+ # PublicSuffixService::Domain.new("zip", "google").domain
109
+ # # => "google.zip"
110
+ #
111
+ # This method returns a FQD, not just the domain part.
112
+ # To get the domain part, use <tt>#sld</tt> (aka second level domain).
113
+ #
114
+ # PublicSuffixService::Domain.new("com", "google", "www").domain
115
+ # # => "google.com"
116
+ #
117
+ # PublicSuffixService::Domain.new("com", "google", "www").sld
118
+ # # => "google"
119
+ #
120
+ # Returns a String or nil.
78
121
  def domain
79
122
  return unless domain?
80
123
  [sld, tld].join(".")
81
124
  end
82
125
 
83
- # Returns a subdomain-like representation of this object
126
+ # Returns a domain-like representation of this object
84
127
  # if the object is a <tt>subdomain?</tt>,
85
128
  # <tt>nil</tt> otherwise.
129
+ #
130
+ # PublicSuffixService::Domain.new("com").subdomain
131
+ # # => nil
132
+ #
133
+ # PublicSuffixService::Domain.new("com", "google").subdomain
134
+ # # => nil
135
+ #
136
+ # PublicSuffixService::Domain.new("com", "google", "www").subdomain
137
+ # # => "www.google.com"
138
+ #
139
+ # This method doesn't validate the input. It handles the domain
140
+ # as a valid domain name and simply applies the necessary transformations.
141
+ #
142
+ # # This is an invalid domain
143
+ # PublicSuffixService::Domain.new("zip", "google", "www").subdomain
144
+ # # => "www.google.zip"
145
+ #
146
+ # This method returns a FQD, not just the domain part.
147
+ # To get the domain part, use <tt>#tld</tt> (aka third level domain).
148
+ #
149
+ # PublicSuffixService::Domain.new("com", "google", "www").subdomain
150
+ # # => "www.google.com"
151
+ #
152
+ # PublicSuffixService::Domain.new("com", "google", "www").trd
153
+ # # => "www"
154
+ #
155
+ # Returns a String or nil.
86
156
  def subdomain
87
157
  return unless subdomain?
88
158
  [trd, sld, tld].join(".")
89
159
  end
90
160
 
91
- # Gets the rule matching this domain in the default PublicSuffixService::RuleList.
161
+ # Gets the rule matching this domain
162
+ # in the default PublicSuffixService::RuleList.
92
163
  #
93
- # Returns an instance of PublicSuffixService::Rule::Base if a rule matches current domain,
94
- # nil if no rule is found.
164
+ # Returns the PublicSuffixService::Rule::Base instance
165
+ # if a rule matches current domain, nil if no rule is found.
95
166
  def rule
96
167
  RuleList.default.find(name)
97
168
  end
@@ -102,25 +173,26 @@ module PublicSuffixService
102
173
  # This method doesn't actually validate the domain.
103
174
  # It only checks whether the instance contains
104
175
  # a value for the <tt>tld</tt> and <tt>sld</tt> attributes.
105
- # If you also want to validate the domain, use <tt>#valid_domain?</tt> instead.
176
+ # If you also want to validate the domain,
177
+ # use <tt>#valid_domain?</tt> instead.
106
178
  #
107
179
  # Examples
108
180
  #
109
- # DomainName.new("com").domain?
181
+ # PublicSuffixService::Domain.new("com").domain?
110
182
  # # => false
111
183
  #
112
- # DomainName.new("com", "google").domain?
184
+ # PublicSuffixService::Domain.new("com", "google").domain?
113
185
  # # => true
114
186
  #
115
- # DomainName.new("com", "google", "www").domain?
187
+ # PublicSuffixService::Domain.new("com", "google", "www").domain?
116
188
  # # => true
117
189
  #
118
190
  # # This is an invalid domain, but returns true
119
191
  # # because this method doesn't validate the content.
120
- # DomainName.new("zip", "google").domain?
192
+ # PublicSuffixService::Domain.new("zip", "google").domain?
121
193
  # # => true
122
194
  #
123
- # Returns true if this instance looks like a domain.
195
+ # Returns Boolean.
124
196
  def domain?
125
197
  !(tld.nil? || sld.nil?)
126
198
  end
@@ -130,36 +202,41 @@ module PublicSuffixService
130
202
  # This method doesn't actually validate the subdomain.
131
203
  # It only checks whether the instance contains
132
204
  # a value for the <tt>tld</tt>, <tt>sld</tt> and <tt>trd</tt> attributes.
133
- # If you also want to validate the domain, use <tt>#valid_subdomain?</tt> instead.
205
+ # If you also want to validate the domain,
206
+ # use <tt>#valid_subdomain?</tt> instead.
134
207
  #
135
208
  # Examples
136
209
  #
137
- # DomainName.new("com").subdomain?
210
+ # PublicSuffixService::Domain.new("com").subdomain?
138
211
  # # => false
139
212
  #
140
- # DomainName.new("com", "google").subdomain?
213
+ # PublicSuffixService::Domain.new("com", "google").subdomain?
141
214
  # # => false
142
215
  #
143
- # DomainName.new("com", "google", "www").subdomain?
216
+ # PublicSuffixService::Domain.new("com", "google", "www").subdomain?
144
217
  # # => true
145
218
  #
146
219
  # # This is an invalid domain, but returns true
147
220
  # # because this method doesn't validate the content.
148
- # DomainName.new("zip", "google", "www").subdomain?
221
+ # PublicSuffixService::Domain.new("zip", "google", "www").subdomain?
149
222
  # # => true
150
223
  #
151
- # Returns true if this instance looks like a subdomain.
224
+ # Returns Boolean.
152
225
  def subdomain?
153
226
  !(tld.nil? || sld.nil? || trd.nil?)
154
227
  end
155
228
 
156
229
  # Checks whether <tt>self</tt> is exclusively a domain,
157
230
  # and not a subdomain.
231
+ #
232
+ # Returns Boolean.
158
233
  def is_a_domain?
159
234
  domain? && !subdomain?
160
235
  end
161
236
 
162
237
  # Checks whether <tt>self</tt> is exclusively a subdomain.
238
+ #
239
+ # Returns Boolean.
163
240
  def is_a_subdomain?
164
241
  subdomain?
165
242
  end
@@ -170,7 +247,7 @@ module PublicSuffixService
170
247
  # Note: this method triggers a new rule lookup in the default RuleList,
171
248
  # which is a quite intensive task.
172
249
  #
173
- # Returns true if this instance is valid.
250
+ # Returns Boolean.
174
251
  def valid?
175
252
  !rule.nil?
176
253
  end
@@ -178,21 +255,21 @@ module PublicSuffixService
178
255
  # Checks whether <tt>self</tt> looks like a domain and validates
179
256
  # according to default <tt>RuleList</tt>.
180
257
  #
181
- # See also <tt>DomainName#domain?</tt> and <tt>DomainName#valid?</tt>.
258
+ # See also <tt>#domain?</tt> and <tt>#valid?</tt>.
182
259
  #
183
260
  # Examples
184
261
  #
185
- # DomainName.new("com").domain?
262
+ # PublicSuffixService::Domain.new("com").domain?
186
263
  # # => false
187
264
  #
188
- # DomainName.new("com", "google").domain?
265
+ # PublicSuffixService::Domain.new("com", "google").domain?
189
266
  # # => true
190
267
  #
191
- # DomainName.new("com", "google", "www").domain?
268
+ # PublicSuffixService::Domain.new("com", "google", "www").domain?
192
269
  # # => true
193
270
  #
194
271
  # # This is an invalid domain
195
- # DomainName.new("zip", "google").false?
272
+ # PublicSuffixService::Domain.new("zip", "google").false?
196
273
  # # => true
197
274
  #
198
275
  # Returns true if this instance looks like a domain and is valid.
@@ -203,21 +280,21 @@ module PublicSuffixService
203
280
  # Checks whether <tt>self</tt> looks like a subdomain and validates
204
281
  # according to default <tt>RuleList</tt>.
205
282
  #
206
- # See also <tt>DomainName#subdomain?</tt> and <tt>DomainName#valid?</tt>.
283
+ # See also <tt>#subdomain?</tt> and <tt>#valid?</tt>.
207
284
  #
208
285
  # Examples
209
286
  #
210
- # DomainName.new("com").subdomain?
287
+ # PublicSuffixService::Domain.new("com").subdomain?
211
288
  # # => false
212
289
  #
213
- # DomainName.new("com", "google").subdomain?
290
+ # PublicSuffixService::Domain.new("com", "google").subdomain?
214
291
  # # => false
215
292
  #
216
- # DomainName.new("com", "google", "www").subdomain?
293
+ # PublicSuffixService::Domain.new("com", "google", "www").subdomain?
217
294
  # # => true
218
295
  #
219
296
  # # This is an invalid domain
220
- # DomainName.new("zip", "google", "www").subdomain?
297
+ # PublicSuffixService::Domain.new("zip", "google", "www").subdomain?
221
298
  # # => false
222
299
  #
223
300
  # Returns true if this instance looks like a domain and is valid.
@@ -22,7 +22,7 @@ module PublicSuffixService
22
22
  # and creates a new instance of that class.
23
23
  # The <tt>name</tt> becomes the rule value.
24
24
  #
25
- # name - The rule String definition
25
+ # name - The String rule definition.
26
26
  #
27
27
  # Examples
28
28
  #
@@ -35,6 +35,7 @@ module PublicSuffixService
35
35
  # PublicSuffixService::Rule.factory("!congresodelalengua3.ar")
36
36
  # # => #<PublicSuffixService::Rule::Exception>
37
37
  #
38
+ # Returns a rule instance, a kind of PublicSuffixService::Rule::Base.
38
39
  def self.factory(name)
39
40
  klass = case name.to_s[0..0]
40
41
  when "*" then "wildcard"
@@ -137,12 +138,12 @@ module PublicSuffixService
137
138
  @name = name.to_s
138
139
  @value = value || @name
139
140
  @type = self.class.name.split("::").last.downcase.to_sym
140
- @labels = domain_to_labels(@value)
141
+ @labels = Domain.domain_to_labels(@value)
141
142
  end
142
143
 
143
144
  # Checks whether this rule is equal to <tt>other</tt>.
144
145
  #
145
- # other - An other PublicSuffixService::Rule::Base to compare.
146
+ # other - The PublicSuffixService::Rule::Base to compare.
146
147
  #
147
148
  # Returns true if this rule and other are instances of the same class
148
149
  # and has the same value, false otherwise.
@@ -156,13 +157,12 @@ module PublicSuffixService
156
157
 
157
158
  # Checks whether this rule matches <tt>domain</tt>.
158
159
  #
159
- # domain - A string with the domain name to check.
160
+ # domain - The String domain name to check.
160
161
  #
161
- # Returns a true if this rule matches domain,
162
- # false otherwise.
162
+ # Returns Boolean.
163
163
  def match?(domain)
164
164
  l1 = labels
165
- l2 = domain_to_labels(domain)
165
+ l2 = Domain.domain_to_labels(domain)
166
166
  odiff(l1, l2).empty?
167
167
  end
168
168
 
@@ -188,9 +188,6 @@ module PublicSuffixService
188
188
 
189
189
  private
190
190
 
191
- def domain_to_labels(domain)
192
- domain.to_s.split(".").reverse
193
- end
194
191
 
195
192
  def odiff(one, two)
196
193
  ii = 0
@@ -218,7 +215,7 @@ module PublicSuffixService
218
215
 
219
216
  # Decomposes the domain according to rule properties.
220
217
  #
221
- # domain - A String with the domain name to parse
218
+ # domain - The String domain name to parse.
222
219
  #
223
220
  # Return an Array with [trd + sld, tld].
224
221
  def decompose(domain)
@@ -248,7 +245,7 @@ module PublicSuffixService
248
245
 
249
246
  # Decomposes the domain according to rule properties.
250
247
  #
251
- # domain - A String with the domain name to parse
248
+ # domain - The String domain name to parse.
252
249
  #
253
250
  # Return an Array with [trd + sld, tld].
254
251
  def decompose(domain)
@@ -279,7 +276,7 @@ module PublicSuffixService
279
276
 
280
277
  # Decomposes the domain according to rule properties.
281
278
  #
282
- # domain - A String with the domain name to parse
279
+ # domain - The String domain name to parse.
283
280
  #
284
281
  # Return an Array with [trd + sld, tld].
285
282
  def decompose(domain)
@@ -291,4 +288,4 @@ module PublicSuffixService
291
288
 
292
289
  end
293
290
 
294
- end
291
+ end
@@ -51,17 +51,43 @@ module PublicSuffixService
51
51
  include Enumerable
52
52
 
53
53
  # Gets the list of rules.
54
- # Each rule is expected to be a subclass of PublicSuffixService::Rule::Base.
55
54
  #
56
- # Returns an Array of rules.
55
+ # Returns the Array of rule instances.
56
+ # Each rule is an instance of the proper corresponding of
57
+ # PublicSuffixService::Rule::Base.
57
58
  attr_reader :list
58
59
 
60
+ # Gets the naive index, a hash that with the keys being the first label of
61
+ # every rule pointing to an array of integers (indexes of the rules in @list)
62
+ attr_reader :indexes
63
+
59
64
 
60
65
  # Initializes an empty PublicSuffixService::RuleList.
61
66
  # If block is given, yields on self.
62
67
  def initialize(&block) # :yields: self
63
- @list = []
68
+ @list = []
69
+ @indexes = {}
64
70
  yield(self) if block_given?
71
+ create_index!
72
+ end
73
+
74
+ # Creates a naive index for @list. Just a hash that will tell
75
+ # us where the elements of @list are relative to its first
76
+ # Rule#labels element.
77
+ #
78
+ # For instance if @list[5] and @list[4] are the only elements of the list
79
+ # where Rule#labels.first is 'us' @indexes['us'] #=> [5,4], that way in
80
+ # select we can avoid mapping every single rule against the candidate domain.
81
+ #
82
+ # Returns nothing.
83
+ def create_index!
84
+ @list.map{|l| l.labels.first }.each_with_index do |elm, inx|
85
+ if !@indexes.has_key?(elm)
86
+ @indexes[elm] = [inx]
87
+ else
88
+ @indexes[elm] << inx
89
+ end
90
+ end
65
91
  end
66
92
 
67
93
  # Checks whether two lists are equal.
@@ -161,10 +187,15 @@ module PublicSuffixService
161
187
 
162
188
  # Selects all the rules matching given domain.
163
189
  #
164
- # Returns an Array of rules.
165
- # Each rule is expected to be a subclass of PublicSuffixService::Rule::Base.
190
+ # Will use @indexes to try only the rules that share the same first label,
191
+ # that will speed up things when using RuleList.find('foo') a lot.
192
+ #
193
+ # Returns an Array of rule instances.
194
+ # Each rule is an instance of the corresponding subclass of
195
+ # PublicSuffixService::Rule::Base.
166
196
  def select(domain)
167
- @list.select { |rule| rule.match?(domain) }
197
+ indices = (@indexes[ Domain.domain_to_labels(domain).first ] || [])
198
+ @list.values_at(*indices).select { |rule| rule.match?(domain) }
168
199
  end
169
200
 
170
201
 
@@ -172,11 +203,11 @@ module PublicSuffixService
172
203
 
173
204
  class << self
174
205
 
175
- # Gets the default <tt>PublicSuffixService::RuleList</tt>.
176
- # Initializes a new <tt>PublicSuffixService::RuleList</tt>
177
- # parsing the content of <tt>PublicSuffixService::RuleList.default_definition</tt> if necessary.
206
+ # Gets the default PublicSuffixService::RuleList.
207
+ # Initializes a new PublicSuffixService::RuleList parsing the content
208
+ # of PublicSuffixService::RuleList.default_definition, if required.
178
209
  #
179
- # Returns an instance of PublicSuffixService::RuleList.
210
+ # Returns a PublicSuffixService::RuleList.
180
211
  def default
181
212
  @@default ||= parse(default_definition)
182
213
  end
@@ -185,12 +216,12 @@ module PublicSuffixService
185
216
  #
186
217
  # value - The new PublicSuffixService::RuleList.
187
218
  #
188
- # Returns the new PublicSuffixService::RuleList.
219
+ # Returns the PublicSuffixService::RuleList.
189
220
  def default=(value)
190
221
  @@default = value
191
222
  end
192
223
 
193
- # Sets the default <tt>PublicSuffixService::RuleList</tt> to <tt>nil</tt>.
224
+ # Sets the default PublicSuffixService::RuleList to nil.
194
225
  #
195
226
  # Returns self.
196
227
  def clear
@@ -201,16 +232,17 @@ module PublicSuffixService
201
232
  # Resets the default <tt>PublicSuffixService::RuleList</tt> and reinitialize it
202
233
  # parsing the content of <tt>PublicSuffixService::RuleList.default_definition</tt>.
203
234
  #
204
- # Returns an instance of PublicSuffixService::RuleList.
235
+ # Returns a PublicSuffixService::RuleList.
205
236
  def reload
206
237
  self.clear.default
207
238
  end
208
239
 
209
240
  # Gets the default definition list.
210
- # Can be any <tt>IOStream</tt> including a <tt>File</tt> or a simple <tt>String</tt>.
241
+ # Can be any <tt>IOStream</tt> including a <tt>File</tt>
242
+ # or a simple <tt>String</tt>.
211
243
  # The object must respond to <tt>#each_line</tt>.
212
244
  #
213
- # Returns an object which responds to <tt>#each_line</tt>.
245
+ # Returns a File.
214
246
  def default_definition
215
247
  File.new(File.join(File.dirname(__FILE__), "definitions.dat"))
216
248
  end
@@ -219,7 +251,9 @@ module PublicSuffixService
219
251
  # Parse given <tt>input</tt> treating the content as Public Suffic List.
220
252
  # See http://publicsuffix.org/format/ for more details about input format.
221
253
  #
222
- # Returns an Array of PublicSuffixService::Rule::Base.
254
+ # Returns an Array of rule instances.
255
+ # Each rule is an instance of the corresponding subclass of
256
+ # PublicSuffixService::Rule::Base.
223
257
  def parse(input)
224
258
  new do |list|
225
259
  input.each_line do |line|
@@ -18,7 +18,7 @@ module PublicSuffixService
18
18
 
19
19
  module Version
20
20
  MAJOR = 0
21
- MINOR = 4
21
+ MINOR = 5
22
22
  PATCH = 0
23
23
  BUILD = nil
24
24
 
@@ -27,4 +27,4 @@ module PublicSuffixService
27
27
 
28
28
  VERSION = Version::STRING
29
29
 
30
- end
30
+ end
@@ -28,9 +28,12 @@ module PublicSuffixService
28
28
  AUTHORS = ["Simone Carletti <weppos@weppos.net>"]
29
29
 
30
30
 
31
- # Parses <tt>domain</tt> and returns a new <tt>PubliSuffixService::Domain</tt> instance.
31
+ # Parses <tt>domain</tt> and returns the
32
+ # PubliSuffixService::Domain instance.
32
33
  #
33
- # domain - A String with the domain name to parse.
34
+ # domain - The String domain name to parse.
35
+ #
36
+ # Examples
34
37
  #
35
38
  # PublicSuffixService.parse("google.com")
36
39
  # # => #<PubliSuffixService::Domain ...>
@@ -45,6 +48,8 @@ module PublicSuffixService
45
48
  # # => PublicSuffixService::InvalidDomain
46
49
  #
47
50
  # Raises PublicSuffixService::Error if domain is not a valid domain
51
+ #
52
+ # Returns the PubliSuffixService::Domain domain.
48
53
  def self.parse(domain)
49
54
  rule = RuleList.default.find(domain) || raise(InvalidDomain, "`#{domain}' is not a valid domain")
50
55
 
@@ -64,9 +69,9 @@ module PublicSuffixService
64
69
  # Checks whether <tt>domain</tt> is a valid domain name.
65
70
  #
66
71
  # This method doesn't care whether domain is a domain or subdomain.
67
- # The check is performed using the default <tt>PublicSuffixService::RuleList</tt>.
72
+ # The check is performed using the default PublicSuffixService::RuleList.
68
73
  #
69
- # domain - A String with the domain name to check.
74
+ # domain - The String domain name to parse.
70
75
  #
71
76
  # Examples
72
77
  #
@@ -82,7 +87,7 @@ module PublicSuffixService
82
87
  # PublicSuffixService.valid?("x.yz")
83
88
  # # => false
84
89
  #
85
- # Returns true if <tt>domain</tt> is a valid domain.
90
+ # Returns Boolean.
86
91
  def self.valid?(domain)
87
92
  !RuleList.default.find(domain).nil?
88
93
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{public_suffix_service}
5
- s.version = "0.3.0"
5
+ s.version = "0.4.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Simone Carletti"]
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.email = %q{weppos@weppos.net}
13
13
  s.extra_rdoc_files = ["CHANGELOG.rdoc", "LICENSE.rdoc", "README.rdoc"]
14
14
  s.files = ["Rakefile", "CHANGELOG.rdoc", "LICENSE.rdoc", "README.rdoc", "public_suffix_service.gemspec", "test/acceptance_test.rb", "test/public_suffix_service/domain_test.rb", "test/public_suffix_service/rule_list_test.rb", "test/public_suffix_service/rule_test.rb", "test/public_suffix_service_test.rb", "test/test_helper.rb", "lib/public_suffix_service/definitions.dat", "lib/public_suffix_service/domain.rb", "lib/public_suffix_service/errors.rb", "lib/public_suffix_service/rule.rb", "lib/public_suffix_service/rule_list.rb", "lib/public_suffix_service/version.rb", "lib/public_suffix_service.rb"]
15
- s.homepage = %q{http://www.simonecarletti.com/code/public-suffix-service}
15
+ s.homepage = %q{http://www.simonecarletti.com/code/public_suffix_service}
16
16
  s.rdoc_options = ["--main", "README.rdoc"]
17
17
  s.require_paths = ["lib"]
18
18
  s.rubygems_version = %q{1.3.7}
@@ -6,6 +6,11 @@ class PublicSuffixService::DomainTest < Test::Unit::TestCase
6
6
  @klass = PublicSuffixService::Domain
7
7
  end
8
8
 
9
+ def test_domain_to_lables
10
+ assert_equal %w{com live spaces someone}, PublicSuffixService::Domain.domain_to_labels('someone.spaces.live.com')
11
+ assert_equal %w{com zoho wiki leontina23samiko}, PublicSuffixService::Domain.domain_to_labels('leontina23samiko.wiki.zoho.com')
12
+ end
13
+
9
14
 
10
15
  def test_initialize_with_tld
11
16
  domain = @klass.new("com")
@@ -138,4 +143,4 @@ class PublicSuffixService::DomainTest < Test::Unit::TestCase
138
143
  assert !@klass.new("com", "google").valid_subdomain?
139
144
  end
140
145
 
141
- end
146
+ end
@@ -16,6 +16,28 @@ class PublicSuffixService::RuleListTest < Test::Unit::TestCase
16
16
  assert_equal 0, @list.length
17
17
  end
18
18
 
19
+ def test_initialize_create_index_when_empty
20
+ assert_equal({}, @list.indexes)
21
+ end
22
+
23
+ def test_find__with_index
24
+ @list = PublicSuffixService::RuleList.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
+
19
41
 
20
42
  def test_equality_with_self
21
43
  list = PublicSuffixService::RuleList.new
@@ -179,4 +201,4 @@ EOS
179
201
  assert_equal %w(ac com.ac ad *.ar !congresodelalengua3.ar).map { |name| PublicSuffixService::Rule.factory(name) }, list.to_a
180
202
  end
181
203
 
182
- end
204
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: public_suffix_service
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 4
8
+ - 5
9
9
  - 0
10
- version: 0.4.0
10
+ version: 0.5.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Simone Carletti
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-05-31 00:00:00 +02:00
18
+ date: 2010-09-13 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: "0"
33
33
  type: :development
34
34
  version_requirements: *id001
35
- description: " Intelligent Domain Name parser based in the Public Suffic List. Domain Name can parse and decompose a domain name into top level domain, domain and subdomains.\n"
35
+ description: " Intelligent domain name parser based in the Public Suffic List. PublicSuffixService can parse and decompose a domain name into top level domain, domain and subdomains.\n"
36
36
  email: weppos@weppos.net
37
37
  executables: []
38
38