Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Rename PubliSuffixService => PublicSuffix

  • Loading branch information...
commit de70d789307e229ef081d70eb978a088c786aa76 1 parent 5faaa13
Simone Carletti authored
2  .yardopts
... ... @@ -1,2 +1,2 @@
1 1 --readme README.rdoc
2   ---title 'Public Suffix Service API Documentation'
  2 +--title 'Ruby Public Suffix API Documentation'
49 README.md
Source Rendered
... ... @@ -1,15 +1,15 @@
1   -# Public Suffix Service
  1 +# Public Suffix List - Ruby implementation
2 2
3   -*Public Suffix Service* is a Ruby domain name parser based on the [Public Suffix List](http://publicsuffix.org).
  3 +*Public Suffix* is a Ruby domain name parser based on the [Public Suffix List](http://publicsuffix.org).
4 4
5 5 [![Build Status](https://secure.travis-ci.org/weppos/public_suffix_service.png)](http://travis-ci.org/weppos/public_suffix_service)
6 6
7 7
8 8 ## What is the Public Suffix List?
9 9
10   -The *Public Suffix Service* is a cross-vendor initiative to provide an accurate list of domain name suffixes.
  10 +The Public Suffix List is a cross-vendor initiative to provide an accurate list of domain name suffixes.
11 11
12   -The Public Suffix Service 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.
  12 +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.
13 13
14 14 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.
15 15
@@ -36,31 +36,34 @@ http://stackoverflow.com/questions/288810/get-the-subdomain-from-a-url
36 36
37 37 * Ruby >= 1.8.7
38 38
39   -*Public Suffix Service* >= 0.9.0 requires Ruby 1.8.7 or newer.
  39 +*Public Suffix* >= 0.9.0 requires Ruby 1.8.7 or newer.
40 40 For older versions of Ruby, see the CHANGELOG.md file.
41 41
42   -Successfully tested with the following interpreters
  42 +Successfully tested against the following interpreters:
43 43
44   -* Ruby 1.8.7 / 1.9.2
45   -* MacRuby
46   -* Ruby Enterprise Edition
  44 +* "Ruby":http://www.ruby-lang.org/ 1.8.x MRI
  45 +* "Ruby":http://www.ruby-lang.org/ 1.9.x MRI
  46 +* "Ruby Enterprise Edition":http://www.rubyenterpriseedition.com/
  47 +* "JRuby":http://jruby.org/
  48 +* "Rubinius":http://rubini.us/
  49 +* "MacRuby":http://www.macruby.org/
47 50
48 51
49 52 ## Installation
50 53
51   -The best way to install *Public Suffix Service* is via [RubyGems](http://www.rubygems.org).
  54 +The best way to install *Public Suffix* is via [RubyGems](http://www.rubygems.org).
52 55
53 56 $ gem install public_suffix_service
54 57
55   -You might need administrator privileges on your system to install the Gem.
  58 +You might need administrator privileges on your system to install the gem.
56 59
57 60
58 61 ## Basic Usage
59 62
60 63 Example domain without subdomains.
61 64
62   - domain = PublicSuffixService.parse("google.com")
63   - # => #<PublicSuffixService::Domain>
  65 + domain = PublicSuffix.parse("google.com")
  66 + # => #<PublicSuffix::Domain>
64 67 domain.tld
65 68 # => "com"
66 69 domain.sld
@@ -74,8 +77,8 @@ Example domain without subdomains.
74 77
75 78 Example domain with subdomains.
76 79
77   - domain = PublicSuffixService.parse("www.google.com")
78   - # => #<PublicSuffixService::Domain>
  80 + domain = PublicSuffix.parse("www.google.com")
  81 + # => #<PublicSuffix::Domain>
79 82 domain.tld
80 83 # => "com"
81 84 domain.sld
@@ -89,13 +92,13 @@ Example domain with subdomains.
89 92
90 93 Simple validation example.
91 94
92   - PublicSuffixService.valid?("google.com")
  95 + PublicSuffix.valid?("google.com")
93 96 # => true
94 97
95   - PublicSuffixService.valid?("www.google.com")
  98 + PublicSuffix.valid?("www.google.com")
96 99 # => true
97 100
98   - PublicSuffixService.valid?("x.yz")
  101 + PublicSuffix.valid?("x.yz")
99 102 # => false
100 103
101 104 ## Fully Qualified Domain Names
@@ -103,14 +106,14 @@ Simple validation example.
103 106 This library automatically recognizes Fully Qualified Domain Names. A FQDN is a domain name that end with a trailing dot.
104 107
105 108 # Parse a standard domain name
106   - domain = PublicSuffixService.parse("www.google.com")
107   - # => #<PublicSuffixService::Domain>
  109 + domain = PublicSuffix.parse("www.google.com")
  110 + # => #<PublicSuffix::Domain>
108 111 domain.tld
109 112 # => "com"
110 113
111 114 # Parse a fully qualified domain name
112   - domain = PublicSuffixService.parse("www.google.com.")
113   - # => #<PublicSuffixService::Domain>
  115 + domain = PublicSuffix.parse("www.google.com.")
  116 + # => #<PublicSuffix::Domain>
114 117 domain.tld
115 118 # => "com"
116 119
@@ -144,5 +147,5 @@ See the CHANGELOG.md file for details.
144 147
145 148 ## License
146 149
147   -*Public Suffix Service* is copyright (c) 2009-2011 Simone Carletti.
  150 +*Public Suffix* is copyright (c) 2009-2011 Simone Carletti.
148 151 This is Free Software distributed under the MIT license.
24 Rakefile
@@ -2,16 +2,14 @@ require 'rubygems'
2 2 require 'rubygems/package_task'
3 3 require 'bundler'
4 4 require 'rake/testtask'
5   -require 'yard'
6   -require 'yard/rake/yardoc_task'
7 5
8 6 $:.unshift(File.dirname(__FILE__) + "/lib")
9   -require 'public_suffix_service'
  7 +require 'public_suffix'
10 8
11 9
12 10 # Common package properties
13   -PKG_NAME = ENV['PKG_NAME'] || PublicSuffixService::GEM
14   -PKG_VERSION = ENV['PKG_VERSION'] || PublicSuffixService::VERSION
  11 +PKG_NAME = ENV['PKG_NAME'] || PublicSuffix::GEM
  12 +PKG_VERSION = ENV['PKG_VERSION'] || PublicSuffix::VERSION
15 13 RUBYFORGE_PROJECT = nil
16 14
17 15 if ENV['SNAPSHOT'].to_i == 1
@@ -32,7 +30,7 @@ spec = Gem::Specification.new do |s|
32 30 s.name = PKG_NAME
33 31 s.version = PKG_VERSION
34 32 s.summary = "Domain name parser based in the Public Suffix List."
35   - s.description = "PublicSuffixService can parse and decompose a domain name into top level domain, domain and subdomains."
  33 + s.description = "PublicSuffix can parse and decompose a domain name into top level domain, domain and subdomains."
36 34
37 35 s.required_ruby_version = ">= 1.8.7"
38 36
@@ -86,18 +84,14 @@ Rake::TestTask.new do |t|
86 84 end
87 85
88 86
  87 +require 'yard'
  88 +require 'yard/rake/yardoc_task'
  89 +
89 90 YARD::Rake::YardocTask.new(:yardoc) do |y|
90 91 y.options = ["--output-dir", "yardoc"]
91 92 end
92 93
93 94 namespace :yardoc do
94   - desc "Publish YARD documentation to the site"
95   - task :publish => ["yardoc:clobber", "yardoc"] do
96   - ENV["username"] || raise(ArgumentError, "Missing ssh username")
97   - sh "rsync -avz --delete yardoc/ #{ENV["username"]}@code:/var/www/apps/code/#{PKG_NAME}/api"
98   - end
99   -
100   - desc "Remove YARD products"
101 95 task :clobber do
102 96 rm_r "yardoc" rescue nil
103 97 end
@@ -108,7 +102,7 @@ task :clobber => "yardoc:clobber"
108 102
109 103 desc "Open an irb session preloaded with this library"
110 104 task :console do
111   - sh "irb -rubygems -I lib -r public_suffix_service.rb"
  105 + sh "irb -rubygems -I lib -r public_suffix.rb"
112 106 end
113 107
114 108
@@ -121,7 +115,7 @@ task :download_definitions do
121 115
122 116 DEFINITION_URL = "http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1"
123 117
124   - File.open("lib/public_suffix_service/definitions.txt", "w+") do |f|
  118 + File.open("lib/public_suffix/definitions.txt", "w+") do |f|
125 119 response = Net::HTTP.get_response(URI.parse(DEFINITION_URL))
126 120 f.write(response.body)
127 121 end
134 lib/public_suffix.rb
... ... @@ -0,0 +1,134 @@
  1 +#--
  2 +# Public Suffix
  3 +#
  4 +# Domain name parser based on the Public Suffix List.
  5 +#
  6 +# Copyright (c) 2009-2011 Simone Carletti <weppos@weppos.net>
  7 +#++
  8 +
  9 +
  10 +require 'public_suffix/domain'
  11 +require 'public_suffix/version'
  12 +require 'public_suffix/errors'
  13 +require 'public_suffix/rule'
  14 +require 'public_suffix/list'
  15 +
  16 +
  17 +module PublicSuffix
  18 +
  19 + NAME = "Public Suffix"
  20 + GEM = "public_suffix_service"
  21 + AUTHORS = ["Simone Carletti <weppos@weppos.net>"]
  22 +
  23 +
  24 + # Backwards compatibility
  25 + autoload :RuleList, 'public_suffix/rule_list'
  26 +
  27 +
  28 + # Parses +domain+ and returns the
  29 + # {PublicSuffix::Domain} instance.
  30 + #
  31 + # Parsing uses the default {PublicSuffix::List}.
  32 + #
  33 + # @param [String, #to_s] domain
  34 + # The domain name or fully qualified domain name to parse.
  35 + #
  36 + # @return [PublicSuffix::Domain]
  37 + #
  38 + # @example Parse a valid domain
  39 + # PublicSuffix.parse("google.com")
  40 + # # => #<PublicSuffix::Domain ...>
  41 + #
  42 + # @example Parse a valid subdomain
  43 + # PublicSuffix.parse("www.google.com")
  44 + # # => #<PublicSuffix::Domain ...>
  45 + #
  46 + # @example Parse a fully qualified domain
  47 + # PublicSuffix.parse("google.com.")
  48 + # # => #<PublicSuffix::Domain ...>
  49 + #
  50 + # @example Parse a fully qualified domain (subdomain)
  51 + # PublicSuffix.parse("www.google.com.")
  52 + # # => #<PublicSuffix::Domain ...>
  53 + #
  54 + # @example Parse an invalid domain
  55 + # PublicSuffix.parse("x.yz")
  56 + # # => PublicSuffix::DomainInvalid
  57 + #
  58 + # @example Parse an URL (not supported, only domains)
  59 + # PublicSuffix.parse("http://www.google.com")
  60 + # # => PublicSuffix::DomainInvalid
  61 + #
  62 + # @raise [PublicSuffix::Error]
  63 + # If domain is not a valid domain.
  64 + # @raise [PublicSuffix::DomainNotAllowed]
  65 + # If a rule for +domain+ is found, but the rule
  66 + # doesn't allow +domain+.
  67 + #
  68 + def self.parse(domain)
  69 + rule = List.default.find(domain)
  70 + if rule.nil?
  71 + raise DomainInvalid, "`#{domain}' is not a valid domain"
  72 + end
  73 + if !rule.allow?(domain)
  74 + raise DomainNotAllowed, "`#{domain}' is not allowed according to Registry policy"
  75 + end
  76 +
  77 + left, right = rule.decompose(domain)
  78 +
  79 + parts = left.split(".")
  80 + # If we have 0 parts left, there is just a tld and no domain or subdomain
  81 + # If we have 1 part left, there is just a tld, domain and not subdomain
  82 + # If we have 2 parts left, the last part is the domain, the other parts (combined) are the subdomain
  83 + tld = right
  84 + sld = parts.empty? ? nil : parts.pop
  85 + trd = parts.empty? ? nil : parts.join(".")
  86 +
  87 + Domain.new(tld, sld, trd)
  88 + end
  89 +
  90 + # Checks whether +domain+ is assigned and allowed,
  91 + # without actually parsing it.
  92 + #
  93 + # This method doesn't care whether domain is a domain or subdomain.
  94 + # The validation is performed using the default {PublicSuffix::List}.
  95 + #
  96 + # @param [String, #to_s] domain
  97 + # The domain name or fully qualified domain name to validate.
  98 + #
  99 + # @return [Boolean]
  100 + #
  101 + # @example Validate a valid domain
  102 + # PublicSuffix.valid?("example.com")
  103 + # # => true
  104 + #
  105 + # @example Validate a valid subdomain
  106 + # PublicSuffix.valid?("www.example.com")
  107 + # # => true
  108 + #
  109 + # @example Validate a not-assigned domain
  110 + # PublicSuffix.valid?("example.zip")
  111 + # # => false
  112 + #
  113 + # @example Validate a not-allowed domain
  114 + # PublicSuffix.valid?("example.do")
  115 + # # => false
  116 + # PublicSuffix.valid?("www.example.do")
  117 + # # => true
  118 + #
  119 + # @example Validate a fully qualified domain
  120 + # PublicSuffix.valid?("google.com.")
  121 + # # => true
  122 + # PublicSuffix.valid?("www.google.com.")
  123 + # # => true
  124 + #
  125 + # @example Check an URL (which is not a valid domain)
  126 + # PublicSuffix.valid?("http://www.example.com")
  127 + # # => false
  128 + #
  129 + def self.valid?(domain)
  130 + rule = List.default.find(domain)
  131 + !rule.nil? && rule.allow?(domain)
  132 + end
  133 +
  134 +end
0  lib/public_suffix_service/definitions.txt → lib/public_suffix/definitions.txt
File renamed without changes
90 lib/public_suffix_service/domain.rb → lib/public_suffix/domain.rb
... ... @@ -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 Domain
13 13
@@ -33,7 +33,7 @@ def self.domain_to_labels(domain)
33 33 domain.to_s.split(".").reverse
34 34 end
35 35
36   - # Creates and returns a new {PublicSuffixService::Domain} instance.
  36 + # Creates and returns a new {PublicSuffix::Domain} instance.
37 37 #
38 38 # @overload initialize(tld)
39 39 # Initializes with a +tld+.
@@ -49,19 +49,19 @@ def self.domain_to_labels(domain)
49 49 # @param [String] tld The TRD (subdomain)
50 50 #
51 51 # @yield [self] Yields on self.
52   - # @yieldparam [PublicSuffixService::Domain] self The newly creates instance
  52 + # @yieldparam [PublicSuffix::Domain] self The newly creates instance
53 53 #
54 54 # @example Initialize with a TLD
55   - # PublicSuffixService::Domain.new("com")
56   - # # => #<PublicSuffixService::Domain @tld="com">
  55 + # PublicSuffix::Domain.new("com")
  56 + # # => #<PublicSuffix::Domain @tld="com">
57 57 #
58 58 # @example Initialize with a TLD and SLD
59   - # PublicSuffixService::Domain.new("com", "example")
60   - # # => #<PublicSuffixService::Domain @tld="com", @trd=nil>
  59 + # PublicSuffix::Domain.new("com", "example")
  60 + # # => #<PublicSuffix::Domain @tld="com", @trd=nil>
61 61 #
62 62 # @example Initialize with a TLD, SLD and TRD
63   - # PublicSuffixService::Domain.new("com", "example", "wwww")
64   - # # => #<PublicSuffixService::Domain @tld="com", @trd=nil, @sld="example">
  63 + # PublicSuffix::Domain.new("com", "example", "wwww")
  64 + # # => #<PublicSuffix::Domain @tld="com", @trd=nil, @sld="example">
65 65 #
66 66 def initialize(*args, &block)
67 67 @tld, @sld, @trd = args
@@ -81,10 +81,10 @@ def to_s
81 81 #
82 82 # @example
83 83 #
84   - # PublicSuffixService::Domain.new("google.com").to_a
  84 + # PublicSuffix::Domain.new("google.com").to_a
85 85 # # => [nil, "google", "com"]
86 86 #
87   - # PublicSuffixService::Domain.new("www.google.com").to_a
  87 + # PublicSuffix::Domain.new("www.google.com").to_a
88 88 # # => [nil, "google", "com"]
89 89 #
90 90 def to_a
@@ -119,11 +119,11 @@ def trd
119 119 # @return [String]
120 120 #
121 121 # @example Gets the domain name of a domain
122   - # PublicSuffixService::Domain.new("com", "google").name
  122 + # PublicSuffix::Domain.new("com", "google").name
123 123 # # => "google.com"
124 124 #
125 125 # @example Gets the domain name of a subdomain
126   - # PublicSuffixService::Domain.new("com", "google", "www").name
  126 + # PublicSuffix::Domain.new("com", "google", "www").name
127 127 # # => "www.google.com"
128 128 #
129 129 def name
@@ -133,29 +133,29 @@ def name
133 133 # Returns a domain-like representation of this object
134 134 # if the object is a {#domain?}, <tt>nil</tt> otherwise.
135 135 #
136   - # PublicSuffixService::Domain.new("com").domain
  136 + # PublicSuffix::Domain.new("com").domain
137 137 # # => nil
138 138 #
139   - # PublicSuffixService::Domain.new("com", "google").domain
  139 + # PublicSuffix::Domain.new("com", "google").domain
140 140 # # => "google.com"
141 141 #
142   - # PublicSuffixService::Domain.new("com", "google", "www").domain
  142 + # PublicSuffix::Domain.new("com", "google", "www").domain
143 143 # # => "www.google.com"
144 144 #
145 145 # This method doesn't validate the input. It handles the domain
146 146 # as a valid domain name and simply applies the necessary transformations.
147 147 #
148 148 # # This is an invalid domain
149   - # PublicSuffixService::Domain.new("zip", "google").domain
  149 + # PublicSuffix::Domain.new("zip", "google").domain
150 150 # # => "google.zip"
151 151 #
152 152 # This method returns a FQD, not just the domain part.
153 153 # To get the domain part, use <tt>#sld</tt> (aka second level domain).
154 154 #
155   - # PublicSuffixService::Domain.new("com", "google", "www").domain
  155 + # PublicSuffix::Domain.new("com", "google", "www").domain
156 156 # # => "google.com"
157 157 #
158   - # PublicSuffixService::Domain.new("com", "google", "www").sld
  158 + # PublicSuffix::Domain.new("com", "google", "www").sld
159 159 # # => "google"
160 160 #
161 161 # @return [String]
@@ -171,29 +171,29 @@ def domain
171 171 # Returns a domain-like representation of this object
172 172 # if the object is a {#subdomain?}, <tt>nil</tt> otherwise.
173 173 #
174   - # PublicSuffixService::Domain.new("com").subdomain
  174 + # PublicSuffix::Domain.new("com").subdomain
175 175 # # => nil
176 176 #
177   - # PublicSuffixService::Domain.new("com", "google").subdomain
  177 + # PublicSuffix::Domain.new("com", "google").subdomain
178 178 # # => nil
179 179 #
180   - # PublicSuffixService::Domain.new("com", "google", "www").subdomain
  180 + # PublicSuffix::Domain.new("com", "google", "www").subdomain
181 181 # # => "www.google.com"
182 182 #
183 183 # This method doesn't validate the input. It handles the domain
184 184 # as a valid domain name and simply applies the necessary transformations.
185 185 #
186 186 # # This is an invalid domain
187   - # PublicSuffixService::Domain.new("zip", "google", "www").subdomain
  187 + # PublicSuffix::Domain.new("zip", "google", "www").subdomain
188 188 # # => "www.google.zip"
189 189 #
190 190 # This method returns a FQD, not just the domain part.
191 191 # To get the domain part, use <tt>#tld</tt> (aka third level domain).
192 192 #
193   - # PublicSuffixService::Domain.new("com", "google", "www").subdomain
  193 + # PublicSuffix::Domain.new("com", "google", "www").subdomain
194 194 # # => "www.google.com"
195 195 #
196   - # PublicSuffixService::Domain.new("com", "google", "www").trd
  196 + # PublicSuffix::Domain.new("com", "google", "www").trd
197 197 # # => "www"
198 198 #
199 199 # @return [String]
@@ -207,9 +207,9 @@ def subdomain
207 207 end
208 208
209 209 # Returns the rule matching this domain
210   - # in the default {PublicSuffixService::List}.
  210 + # in the default {PublicSuffix::List}.
211 211 #
212   - # @return [PublicSuffixService::Rule::Base, nil]
  212 + # @return [PublicSuffix::Rule::Base, nil]
213 213 # The rule instance a rule matches current domain,
214 214 # nil if no rule is found.
215 215 def rule
@@ -229,18 +229,18 @@ def rule
229 229 #
230 230 # @example
231 231 #
232   - # PublicSuffixService::Domain.new("com").domain?
  232 + # PublicSuffix::Domain.new("com").domain?
233 233 # # => false
234 234 #
235   - # PublicSuffixService::Domain.new("com", "google").domain?
  235 + # PublicSuffix::Domain.new("com", "google").domain?
236 236 # # => true
237 237 #
238   - # PublicSuffixService::Domain.new("com", "google", "www").domain?
  238 + # PublicSuffix::Domain.new("com", "google", "www").domain?
239 239 # # => true
240 240 #
241 241 # # This is an invalid domain, but returns true
242 242 # # because this method doesn't validate the content.
243   - # PublicSuffixService::Domain.new("zip", "google").domain?
  243 + # PublicSuffix::Domain.new("zip", "google").domain?
244 244 # # => true
245 245 #
246 246 # @see #subdomain?
@@ -261,18 +261,18 @@ def domain?
261 261 #
262 262 # @example
263 263 #
264   - # PublicSuffixService::Domain.new("com").subdomain?
  264 + # PublicSuffix::Domain.new("com").subdomain?
265 265 # # => false
266 266 #
267   - # PublicSuffixService::Domain.new("com", "google").subdomain?
  267 + # PublicSuffix::Domain.new("com", "google").subdomain?
268 268 # # => false
269 269 #
270   - # PublicSuffixService::Domain.new("com", "google", "www").subdomain?
  270 + # PublicSuffix::Domain.new("com", "google", "www").subdomain?
271 271 # # => true
272 272 #
273 273 # # This is an invalid domain, but returns true
274 274 # # because this method doesn't validate the content.
275   - # PublicSuffixService::Domain.new("zip", "google", "www").subdomain?
  275 + # PublicSuffix::Domain.new("zip", "google", "www").subdomain?
276 276 # # => true
277 277 #
278 278 # @see #domain?
@@ -335,17 +335,17 @@ def valid?
335 335 #
336 336 # @example
337 337 #
338   - # PublicSuffixService::Domain.new("com").domain?
  338 + # PublicSuffix::Domain.new("com").domain?
339 339 # # => false
340 340 #
341   - # PublicSuffixService::Domain.new("com", "google").domain?
  341 + # PublicSuffix::Domain.new("com", "google").domain?
342 342 # # => true
343 343 #
344   - # PublicSuffixService::Domain.new("com", "google", "www").domain?
  344 + # PublicSuffix::Domain.new("com", "google", "www").domain?
345 345 # # => true
346 346 #
347 347 # # This is an invalid domain
348   - # PublicSuffixService::Domain.new("zip", "google").false?
  348 + # PublicSuffix::Domain.new("zip", "google").false?
349 349 # # => true
350 350 #
351 351 # @see #domain?
@@ -362,17 +362,17 @@ def valid_domain?
362 362 #
363 363 # @example
364 364 #
365   - # PublicSuffixService::Domain.new("com").subdomain?
  365 + # PublicSuffix::Domain.new("com").subdomain?
366 366 # # => false
367 367 #
368   - # PublicSuffixService::Domain.new("com", "google").subdomain?
  368 + # PublicSuffix::Domain.new("com", "google").subdomain?
369 369 # # => false
370 370 #
371   - # PublicSuffixService::Domain.new("com", "google", "www").subdomain?
  371 + # PublicSuffix::Domain.new("com", "google", "www").subdomain?
372 372 # # => true
373 373 #
374 374 # # This is an invalid domain
375   - # PublicSuffixService::Domain.new("zip", "google", "www").subdomain?
  375 + # PublicSuffix::Domain.new("zip", "google", "www").subdomain?
376 376 # # => false
377 377 #
378 378 # @see #subdomain?
24 lib/public_suffix_service/errors.rb → lib/public_suffix/errors.rb
... ... @@ -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 @@ class Error < StandardError
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 @@ class DomainInvalid < Error
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 @@ class DomainNotAllowed < DomainInvalid
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
64 lib/public_suffix_service/list.rb → lib/public_suffix/list.rb
... ... @@ -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::List} 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::List},
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::List.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,22 +30,22 @@ 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::List} you want.
38   - # The {PublicSuffixService::List.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::List} implements +Enumerable+ module.
  41 + # {PublicSuffix::List} implements +Enumerable+ module.
42 42 #
43 43 class List
44 44 include Enumerable
45 45
46 46 # Gets the array of rules.
47 47 #
48   - # @return [Array<PublicSuffixService::Rule::*>]
  48 + # @return [Array<PublicSuffix::Rule::*>]
49 49 attr_reader :rules
50 50
51 51 # Gets the naive index, a hash that with the keys being the first label of
@@ -55,10 +55,10 @@ class List
55 55 attr_reader :indexes
56 56
57 57
58   - # Initializes an empty {PublicSuffixService::List}.
  58 + # Initializes an empty {PublicSuffix::List}.
59 59 #
60 60 # @yield [self] Yields on self.
61   - # @yieldparam [PublicSuffixService::List] self The newly created instance.
  61 + # @yieldparam [PublicSuffix::List] self The newly created instance.
62 62 #
63 63 def initialize(&block)
64 64 @rules = []
@@ -69,7 +69,7 @@ def initialize(&block)
69 69
70 70 # Creates a naive index for +@rules+. Just a hash that will tell
71 71 # us where the elements of +@rules+ are relative to its first
72   - # {PublicSuffixService::Rule::Base#labels} element.
  72 + # {PublicSuffix::Rule::Base#labels} element.
73 73 #
74 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
@@ -87,10 +87,10 @@ def create_index!
87 87 # Checks whether two lists are equal.
88 88 #
89 89 # List <tt>one</tt> is equal to <tt>two</tt>, if <tt>two</tt> is an instance of
90   - # {PublicSuffixService::List} and each +PublicSuffixService::Rule::*+
  90 + # {PublicSuffix::List} and each +PublicSuffix::Rule::*+
91 91 # in list <tt>one</tt> is available in list <tt>two</tt>, in the same order.
92 92 #
93   - # @param [PublicSuffixService::List] other
  93 + # @param [PublicSuffix::List] other
94 94 # The List to compare.
95 95 #
96 96 # @return [Boolean]
@@ -108,7 +108,7 @@ def each(*args, &block)
108 108
109 109 # Gets the list as array.
110 110 #
111   - # @return [Array<PublicSuffixService::Rule::*>]
  111 + # @return [Array<PublicSuffix::Rule::*>]
112 112 def to_a
113 113 @rules
114 114 end
@@ -116,7 +116,7 @@ def to_a
116 116 # Adds the given object to the list
117 117 # and optionally refreshes the rule index.
118 118 #
119   - # @param [PublicSuffixService::Rule::*] rule
  119 + # @param [PublicSuffix::Rule::*] rule
120 120 # The rule to add to the list.
121 121 # @param [Boolean] index
122 122 # Set to true to recreate the rule index
@@ -179,7 +179,7 @@ def clear
179 179 #
180 180 # @param [String, #to_s] domain The domain name.
181 181 #
182   - # @return [PublicSuffixService::Rule::*, nil]
  182 + # @return [PublicSuffix::Rule::*, nil]
183 183 def find(domain)
184 184 rules = select(domain)
185 185 rules.select { |r| r.type == :exception }.first ||
@@ -193,7 +193,7 @@ def find(domain)
193 193 #
194 194 # @param [String, #to_s] domain The domain name.
195 195 #
196   - # @return [Array<PublicSuffixService::Rule::*>]
  196 + # @return [Array<PublicSuffix::Rule::*>]
197 197 def select(domain)
198 198 indices = (@indexes[Domain.domain_to_labels(domain).first] || [])
199 199 @rules.values_at(*indices).select { |rule| rule.match?(domain) }
@@ -205,20 +205,20 @@ def select(domain)
205 205 class << self
206 206
207 207 # Gets the default rule list.
208   - # Initializes a new {PublicSuffixService::List} parsing the content
209   - # of {PublicSuffixService::List.default_definition}, if required.
  208 + # Initializes a new {PublicSuffix::List} parsing the content
  209 + # of {PublicSuffix::List.default_definition}, if required.
210 210 #
211   - # @return [PublicSuffixService::List]
  211 + # @return [PublicSuffix::List]
212 212 def default
213 213 @@default ||= parse(default_definition)
214 214 end
215 215
216 216 # Sets the default rule list to +value+.
217 217 #
218   - # @param [PublicSuffixService::List] value
  218 + # @param [PublicSuffix::List] value
219 219 # The new rule list.
220 220 #
221   - # @return [PublicSuffixService::List]
  221 + # @return [PublicSuffix::List]
222 222 def default=(value)
223 223 @@default = value
224 224 end
@@ -232,9 +232,9 @@ def clear
232 232 end
233 233
234 234 # Resets the default rule list and reinitialize it
235   - # parsing the content of {PublicSuffixService::List.default_definition}.
  235 + # parsing the content of {PublicSuffix::List.default_definition}.
236 236 #
237   - # @return [PublicSuffixService::List]
  237 + # @return [PublicSuffix::List]
238 238 def reload
239 239 self.clear.default
240 240 end
@@ -256,7 +256,7 @@ def default_definition
256 256 #
257 257 # @param [String] input The rule list to parse.
258 258 #
259   - # @return [Array<PublicSuffixService::Rule::*>]
  259 + # @return [Array<PublicSuffix::Rule::*>]
260 260 def parse(input)
261 261 new do |list|
262 262 input.each_line do |line|
58 lib/public_suffix_service/rule.rb → lib/public_suffix/rule.rb
... ... @@ -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 @@ class Rule
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 @@ def self.factory(name)
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 @@ def self.factory(name)
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 @@ def self.factory(name)
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 @@ def self.factory(name)
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 @@ def self.factory(name)
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 @@ def initialize(name, value = nil)
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 @@ def ==(other)
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 @@ def match?(domain)
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")
14 lib/public_suffix/rule_list.rb
... ... @@ -0,0 +1,14 @@
  1 +#--
  2 +# Public Suffix
  3 +#
  4 +# Domain name parser based on the Public Suffix List.
  5 +#
  6 +# Copyright (c) 2009-2011 Simone Carletti <weppos@weppos.net>
  7 +#++
  8 +
  9 +
  10 +warn("The PublicSuffix::RuleList object has been deprecated and will be removed in PublicSuffix 1.0. Please use PublicSuffix::List instead.")
  11 +
  12 +module PublicSuffix
  13 + RuleList = List
  14 +end
6 lib/public_suffix_service/version.rb → lib/public_suffix/version.rb
... ... @@ -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 module Version
13 13 MAJOR = 0
130 lib/public_suffix_service.rb
... ... @@ -1,134 +1,14 @@
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   -require 'public_suffix_service/domain'
11   -require 'public_suffix_service/version'
12   -require 'public_suffix_service/errors'
13   -require 'public_suffix_service/rule'
14   -require 'public_suffix_service/list'
  10 +warn("The PublicSuffixService object has been deprecated. Please use PublicSuffix instead.")
15 11
  12 +require 'public_suffix'
16 13
17   -module PublicSuffixService
18   -
19   - NAME = "Public Suffix Service"
20   - GEM = "public_suffix_service"
21   - AUTHORS = ["Simone Carletti <weppos@weppos.net>"]
22   -
23   -
24   - # Backwards compatibility
25   - autoload :RuleList, 'public_suffix_service/rule_list'
26   -
27   -
28   - # Parses +domain+ and returns the
29   - # {PublicSuffixService::Domain} instance.
30   - #
31   - # Parsing uses the default {PublicSuffixService::List}.
32   - #
33   - # @param [String, #to_s] domain
34   - # The domain name or fully qualified domain name to parse.
35   - #
36   - # @return [PublicSuffixService::Domain]
37   - #
38   - # @example Parse a valid domain
39   - # PublicSuffixService.parse("google.com")
40   - # # => #<PublicSuffixService::Domain ...>
41   - #
42   - # @example Parse a valid subdomain
43   - # PublicSuffixService.parse("www.google.com")
44   - # # => #<PublicSuffixService::Domain ...>
45   - #
46   - # @example Parse a fully qualified domain
47   - # PublicSuffixService.parse("google.com.")
48   - # # => #<PublicSuffixService::Domain ...>
49   - #
50   - # @example Parse a fully qualified domain (subdomain)
51   - # PublicSuffixService.parse("www.google.com.")
52   - # # => #<PublicSuffixService::Domain ...>
53   - #
54   - # @example Parse an invalid domain
55   - # PublicSuffixService.parse("x.yz")
56   - # # => PublicSuffixService::DomainInvalid
57   - #
58   - # @example Parse an URL (not supported, only domains)
59   - # PublicSuffixService.parse("http://www.google.com")
60   - # # => PublicSuffixService::DomainInvalid
61   - #
62   - # @raise [PublicSuffixService::Error]
63   - # If domain is not a valid domain.
64   - # @raise [PublicSuffixService::DomainNotAllowed]
65   - # If a rule for +domain+ is found, but the rule
66   - # doesn't allow +domain+.
67   - #
68   - def self.parse(domain)
69   - rule = List.default.find(domain)
70   - if rule.nil?
71   - raise DomainInvalid, "`#{domain}' is not a valid domain"
72   - end
73   - if !rule.allow?(domain)
74   - raise DomainNotAllowed, "`#{domain}' is not allowed according to Registry policy"
75   - end
76   -
77   - left, right = rule.decompose(domain)
78   -
79   - parts = left.split(".")
80   - # If we have 0 parts left, there is just a tld and no domain or subdomain
81   - # If we have 1 part left, there is just a tld, domain and not subdomain
82   - # If we have 2 parts left, the last part is the domain, the other parts (combined) are the subdomain
83   - tld = right
84   - sld = parts.empty? ? nil : parts.pop
85   - trd = parts.empty? ? nil : parts.join(".")
86   -
87   - Domain.new(tld, sld, trd)
88   - end
89   -
90   - # Checks whether +domain+ is assigned and allowed,
91   - # without actually parsing it.
92   - #
93   - # This method doesn't care whether domain is a domain or subdomain.
94   - # The validation is performed using the default {PublicSuffixService::List}.
95   - #
96   - # @param [String, #to_s] domain
97   - # The domain name or fully qualified domain name to validate.
98   - #
99   - # @return [Boolean]
100   - #
101   - # @example Validate a valid domain
102   - # PublicSuffixService.valid?("example.com")
103   - # # => true
104   - #
105   - # @example Validate a valid subdomain
106   - # PublicSuffixService.valid?("www.example.com")
107   - # # => true
108   - #
109   - # @example Validate a not-assigned domain
110   - # PublicSuffixService.valid?("example.zip")
111   - # # => false
112   - #
113   - # @example Validate a not-allowed domain
114   - # PublicSuffixService.valid?("example.do")
115   - # # => false
116   - # PublicSuffixService.valid?("www.example.do")
117   - # # => true
118   - #
119   - # @example Validate a fully qualified domain
120   - # PublicSuffixService.valid?("google.com.")
121   - # # => true
122   - # PublicSuffixService.valid?("www.google.com.")
123   - # # => true
124   - #
125   - # @example Check an URL (which is not a valid domain)
126   - # PublicSuffixService.valid?("http://www.example.com")
127   - # # => false
128   - #
129   - def self.valid?(domain)
130   - rule = List.default.find(domain)
131   - !rule.nil? && rule.allow?(domain)
132   - end
133   -
134   -end
  14 +PublicSuffixService = PublicSuffix
14 <