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.
@@ -1,13 +1,13 @@
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
  class Error < StandardError
13
13
  end
@@ -18,11 +18,11 @@ module PublicSuffixService
18
18
  #
19
19
  # @example
20
20
  #
21
- # PublicSuffixService.parse("nic.test")
22
- # # => PublicSuffixService::DomainInvalid
21
+ # PublicSuffix.parse("nic.test")
22
+ # # => PublicSuffix::DomainInvalid
23
23
  #
24
- # PublicSuffixService.parse("http://www.nic.it")
25
- # # => PublicSuffixService::DomainInvalid
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
- # PublicSuffixService.parse("nic.do")
40
- # # => PublicSuffixService::DomainNotAllowed
39
+ # PublicSuffix.parse("nic.do")
40
+ # # => PublicSuffix::DomainNotAllowed
41
41
  #
42
- # PublicSuffixService.parse("www.nic.do")
43
- # # => PublicSuffixService::Domain
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 {PublicSuffixService::DomainInvalid}.
53
+ # @deprecated Use {PublicSuffix::DomainInvalid}.
54
54
  #
55
55
  InvalidDomain = DomainInvalid
56
56
 
@@ -1,28 +1,28 @@
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
- # A {PublicSuffixService::RuleList} is a collection of one
13
- # or more {PublicSuffixService::Rule}.
12
+ # A {PublicSuffix::List} is a collection of one
13
+ # or more {PublicSuffix::Rule}.
14
14
  #
15
- # Given a {PublicSuffixService::RuleList},
16
- # you can add or remove {PublicSuffixService::Rule},
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 = PublicSuffixService::RuleList.new
21
+ # list = PublicSuffix::List.new
22
22
  #
23
23
  # # Push two rules to the list
24
- # list << PublicSuffixService::Rule.factory("it")
25
- # list << PublicSuffixService::Rule.factory("com")
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
- # # => #<PublicSuffixService::Rule::Normal>
33
+ # # => #<PublicSuffix::Rule::Normal>
34
34
  # list.find("example.org")
35
35
  # # => nil
36
36
  #
37
- # You can create as many {PublicSuffixService::RuleList} you want.
38
- # The {PublicSuffixService::RuleList.default} rule list is used
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
- # {PublicSuffixService::RuleList} implements +Enumerable+ module.
41
+ # {PublicSuffix::List} implements +Enumerable+ module.
42
42
  #
43
- class RuleList
43
+ class List
44
44
  include Enumerable
45
45
 
46
- # Gets the list of rules.
46
+ # Gets the array of rules.
47
47
  #
48
- # @return [Array<PublicSuffixService::Rule::*>]
49
- attr_reader :list
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 @list)
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 {PublicSuffixService::RuleList}.
58
+ # Initializes an empty {PublicSuffix::List}.
59
59
  #
60
60
  # @yield [self] Yields on self.
61
- # @yieldparam [PublicSuffixService::RuleList] self The newly creates instance
61
+ # @yieldparam [PublicSuffix::List] self The newly created instance.
62
62
  #
63
63
  def initialize(&block)
64
- @list = []
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 +@list+. Just a hash that will tell
71
- # us where the elements of +@list+ are relative to its first
72
- # {PublicSuffixService::Rule::Base#labels} element.
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 @list[5] and @list[4] are the only elements of the list
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
- @list.map { |l| l.labels.first }.each_with_index do |elm, inx|
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
- # RuleList <tt>one</tt> is equal to <tt>two</tt>, if <tt>two</tt> is an instance of
90
- # {PublicSuffixService::RuleList} and each +PublicSuffixService::Rule::*+
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 [PublicSuffixService::RuleList] other
95
- # The rule list to compare.
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?(RuleList)
98
+ return false unless other.is_a?(List)
100
99
  self.equal?(other) ||
101
- self.list == other.list
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
- @list.each(*args, &block)
106
+ @rules.each(*args, &block)
108
107
  end
109
108
 
110
109
  # Gets the list as array.
111
110
  #
112
- # @return [Array<PublicSuffixService::Rule::*>]
111
+ # @return [Array<PublicSuffix::Rule::*>]
113
112
  def to_a
114
- @list
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 [PublicSuffixService::Rule::*] rule
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
- @list << rule
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
- @list.size
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
- @list.empty?
148
+ @rules.empty?
150
149
  end
151
150
 
152
151
  # Removes all elements.
153
152
  #
154
153
  # @return [self]
155
154
  def clear
156
- @list.clear
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 [PublicSuffixService::Rule::*, nil]
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 +RuleList.find('foo')+ a lot.
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<PublicSuffixService::Rule::*>]
196
+ # @return [Array<PublicSuffix::Rule::*>]
198
197
  def select(domain)
199
198
  indices = (@indexes[Domain.domain_to_labels(domain).first] || [])
200
- @list.values_at(*indices).select { |rule| rule.match?(domain) }
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 {PublicSuffixService::RuleList} parsing the content
210
- # of {PublicSuffixService::RuleList.default_definition}, if required.
208
+ # Initializes a new {PublicSuffix::List} parsing the content
209
+ # of {PublicSuffix::List.default_definition}, if required.
211
210
  #
212
- # @return [PublicSuffixService::RuleList]
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 [PublicSuffixService::RuleList] value
218
+ # @param [PublicSuffix::List] value
220
219
  # The new rule list.
221
220
  #
222
- # @return [PublicSuffixService::RuleList]
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 {PublicSuffixService::RuleList.default_definition}.
235
+ # parsing the content of {PublicSuffix::List.default_definition}.
237
236
  #
238
- # @return [PublicSuffixService::RuleList]
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<PublicSuffixService::Rule::*>]
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 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
  # 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 +PublicSuffixService::Rule+ namespace.
16
+ # subclass within the +PublicSuffix::Rule+ namespace.
17
17
  #
18
- # To create a new Rule, use the {PublicSuffixService::Rule#factory} method.
18
+ # To create a new Rule, use the {PublicSuffix::Rule#factory} method.
19
19
  #
20
- # PublicSuffixService::Rule.factory("ar")
21
- # # => #<PublicSuffixService::Rule::Normal>
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 [PublicSuffixService::Rule::*] A rule instance.
31
+ # @return [PublicSuffix::Rule::*] A rule instance.
32
32
  #
33
33
  # @example Creates a Normal rule
34
- # PublicSuffixService::Rule.factory("ar")
35
- # # => #<PublicSuffixService::Rule::Normal>
34
+ # PublicSuffix::Rule.factory("ar")
35
+ # # => #<PublicSuffix::Rule::Normal>
36
36
  #
37
37
  # @example Creates a Wildcard rule
38
- # PublicSuffixService::Rule.factory("*.ar")
39
- # # => #<PublicSuffixService::Rule::Wildcard>
38
+ # PublicSuffix::Rule.factory("*.ar")
39
+ # # => #<PublicSuffix::Rule::Wildcard>
40
40
  #
41
41
  # @example Creates an Exception rule
42
- # PublicSuffixService::Rule.factory("!congresodelalengua3.ar")
43
- # # => #<PublicSuffixService::Rule::Exception>
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
- # * {PublicSuffixService::Rule::Normal}
67
- # * {PublicSuffixService::Rule::Exception}
68
- # * {PublicSuffixService::Rule::Wildcard}
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
- # PublicSuffixService::Rule.factory("*.google.com")
84
- # #<PublicSuffixService::Rule::Wildcard:0x1015c14b0
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>PublicSuffixService::Rule.factory</tt> method.
94
+ # to the <tt>PublicSuffix::Rule.factory</tt> method.
95
95
  #
96
- # PublicSuffixService::Rule.factory("com")
97
- # # => PublicSuffixService::Rule::Normal
96
+ # PublicSuffix::Rule.factory("com")
97
+ # # => PublicSuffix::Rule::Normal
98
98
  #
99
- # PublicSuffixService::Rule.factory("*.com")
100
- # # => PublicSuffixService::Rule::Wildcard
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 = PublicSuffixService::Rule.factory("com")
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 = PublicSuffixService::Rule.factory("com")
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 [PublicSuffixService::Rule::*] other
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
- # # #<PublicSuffixService::Rule::Normal>
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
- # # => #<PublicSuffixService::Rule::Wildcard>
204
+ # # => #<PublicSuffix::Rule::Wildcard>
205
205
  # rule.allow?("example.do")
206
206
  # # => false
207
207
  # rule.allow?("www.example.do")