Browse files

Rename PubliSuffixService => PublicSuffix

  • Loading branch information...
1 parent 5faaa13 commit de70d789307e229ef081d70eb978a088c786aa76 @weppos committed Dec 23, 2011
View
2 .yardopts
@@ -1,2 +1,2 @@
--readme README.rdoc
---title 'Public Suffix Service API Documentation'
+--title 'Ruby Public Suffix API Documentation'
View
49 README.md
@@ -1,15 +1,15 @@
-# Public Suffix Service
+# Public Suffix List - Ruby implementation
-*Public Suffix Service* is a Ruby domain name parser based on the [Public Suffix List](http://publicsuffix.org).
+*Public Suffix* is a Ruby domain name parser based on the [Public Suffix List](http://publicsuffix.org).
[![Build Status](https://secure.travis-ci.org/weppos/public_suffix_service.png)](http://travis-ci.org/weppos/public_suffix_service)
## What is the Public Suffix List?
-The *Public Suffix Service* is a cross-vendor initiative to provide an accurate list of domain name suffixes.
+The Public Suffix List is a cross-vendor initiative to provide an accurate list of domain name suffixes.
-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.
+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.
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.
@@ -36,31 +36,34 @@ http://stackoverflow.com/questions/288810/get-the-subdomain-from-a-url
* Ruby >= 1.8.7
-*Public Suffix Service* >= 0.9.0 requires Ruby 1.8.7 or newer.
+*Public Suffix* >= 0.9.0 requires Ruby 1.8.7 or newer.
For older versions of Ruby, see the CHANGELOG.md file.
-Successfully tested with the following interpreters
+Successfully tested against the following interpreters:
-* Ruby 1.8.7 / 1.9.2
-* MacRuby
-* Ruby Enterprise Edition
+* "Ruby":http://www.ruby-lang.org/ 1.8.x MRI
+* "Ruby":http://www.ruby-lang.org/ 1.9.x MRI
+* "Ruby Enterprise Edition":http://www.rubyenterpriseedition.com/
+* "JRuby":http://jruby.org/
+* "Rubinius":http://rubini.us/
+* "MacRuby":http://www.macruby.org/
## Installation
-The best way to install *Public Suffix Service* is via [RubyGems](http://www.rubygems.org).
+The best way to install *Public Suffix* is via [RubyGems](http://www.rubygems.org).
$ gem install public_suffix_service
-You might need administrator privileges on your system to install the Gem.
+You might need administrator privileges on your system to install the gem.
## Basic Usage
Example domain without subdomains.
- domain = PublicSuffixService.parse("google.com")
- # => #<PublicSuffixService::Domain>
+ domain = PublicSuffix.parse("google.com")
+ # => #<PublicSuffix::Domain>
domain.tld
# => "com"
domain.sld
@@ -74,8 +77,8 @@ Example domain without subdomains.
Example domain with subdomains.
- domain = PublicSuffixService.parse("www.google.com")
- # => #<PublicSuffixService::Domain>
+ domain = PublicSuffix.parse("www.google.com")
+ # => #<PublicSuffix::Domain>
domain.tld
# => "com"
domain.sld
@@ -89,28 +92,28 @@ Example domain with subdomains.
Simple validation example.
- PublicSuffixService.valid?("google.com")
+ PublicSuffix.valid?("google.com")
# => true
- PublicSuffixService.valid?("www.google.com")
+ PublicSuffix.valid?("www.google.com")
# => true
- PublicSuffixService.valid?("x.yz")
+ PublicSuffix.valid?("x.yz")
# => false
## Fully Qualified Domain Names
This library automatically recognizes Fully Qualified Domain Names. A FQDN is a domain name that end with a trailing dot.
# Parse a standard domain name
- domain = PublicSuffixService.parse("www.google.com")
- # => #<PublicSuffixService::Domain>
+ domain = PublicSuffix.parse("www.google.com")
+ # => #<PublicSuffix::Domain>
domain.tld
# => "com"
# Parse a fully qualified domain name
- domain = PublicSuffixService.parse("www.google.com.")
- # => #<PublicSuffixService::Domain>
+ domain = PublicSuffix.parse("www.google.com.")
+ # => #<PublicSuffix::Domain>
domain.tld
# => "com"
@@ -144,5 +147,5 @@ See the CHANGELOG.md file for details.
## License
-*Public Suffix Service* is copyright (c) 2009-2011 Simone Carletti.
+*Public Suffix* is copyright (c) 2009-2011 Simone Carletti.
This is Free Software distributed under the MIT license.
View
24 Rakefile
@@ -2,16 +2,14 @@ require 'rubygems'
require 'rubygems/package_task'
require 'bundler'
require 'rake/testtask'
-require 'yard'
-require 'yard/rake/yardoc_task'
$:.unshift(File.dirname(__FILE__) + "/lib")
-require 'public_suffix_service'
+require 'public_suffix'
# Common package properties
-PKG_NAME = ENV['PKG_NAME'] || PublicSuffixService::GEM
-PKG_VERSION = ENV['PKG_VERSION'] || PublicSuffixService::VERSION
+PKG_NAME = ENV['PKG_NAME'] || PublicSuffix::GEM
+PKG_VERSION = ENV['PKG_VERSION'] || PublicSuffix::VERSION
RUBYFORGE_PROJECT = nil
if ENV['SNAPSHOT'].to_i == 1
@@ -32,7 +30,7 @@ spec = Gem::Specification.new do |s|
s.name = PKG_NAME
s.version = PKG_VERSION
s.summary = "Domain name parser based in the Public Suffix List."
- s.description = "PublicSuffixService can parse and decompose a domain name into top level domain, domain and subdomains."
+ s.description = "PublicSuffix can parse and decompose a domain name into top level domain, domain and subdomains."
s.required_ruby_version = ">= 1.8.7"
@@ -86,18 +84,14 @@ Rake::TestTask.new do |t|
end
+require 'yard'
+require 'yard/rake/yardoc_task'
+
YARD::Rake::YardocTask.new(:yardoc) do |y|
y.options = ["--output-dir", "yardoc"]
end
namespace :yardoc do
- desc "Publish YARD documentation to the site"
- task :publish => ["yardoc:clobber", "yardoc"] do
- ENV["username"] || raise(ArgumentError, "Missing ssh username")
- sh "rsync -avz --delete yardoc/ #{ENV["username"]}@code:/var/www/apps/code/#{PKG_NAME}/api"
- end
-
- desc "Remove YARD products"
task :clobber do
rm_r "yardoc" rescue nil
end
@@ -108,7 +102,7 @@ task :clobber => "yardoc:clobber"
desc "Open an irb session preloaded with this library"
task :console do
- sh "irb -rubygems -I lib -r public_suffix_service.rb"
+ sh "irb -rubygems -I lib -r public_suffix.rb"
end
@@ -121,7 +115,7 @@ task :download_definitions do
DEFINITION_URL = "http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1"
- File.open("lib/public_suffix_service/definitions.txt", "w+") do |f|
+ File.open("lib/public_suffix/definitions.txt", "w+") do |f|
response = Net::HTTP.get_response(URI.parse(DEFINITION_URL))
f.write(response.body)
end
View
134 lib/public_suffix.rb
@@ -0,0 +1,134 @@
+#--
+# Public Suffix
+#
+# Domain name parser based on the Public Suffix List.
+#
+# Copyright (c) 2009-2011 Simone Carletti <weppos@weppos.net>
+#++
+
+
+require 'public_suffix/domain'
+require 'public_suffix/version'
+require 'public_suffix/errors'
+require 'public_suffix/rule'
+require 'public_suffix/list'
+
+
+module PublicSuffix
+
+ NAME = "Public Suffix"
+ GEM = "public_suffix_service"
+ AUTHORS = ["Simone Carletti <weppos@weppos.net>"]
+
+
+ # Backwards compatibility
+ autoload :RuleList, 'public_suffix/rule_list'
+
+
+ # Parses +domain+ and returns the
+ # {PublicSuffix::Domain} instance.
+ #
+ # Parsing uses the default {PublicSuffix::List}.
+ #
+ # @param [String, #to_s] domain
+ # The domain name or fully qualified domain name to parse.
+ #
+ # @return [PublicSuffix::Domain]
+ #
+ # @example Parse a valid domain
+ # PublicSuffix.parse("google.com")
+ # # => #<PublicSuffix::Domain ...>
+ #
+ # @example Parse a valid subdomain
+ # PublicSuffix.parse("www.google.com")
+ # # => #<PublicSuffix::Domain ...>
+ #
+ # @example Parse a fully qualified domain
+ # PublicSuffix.parse("google.com.")
+ # # => #<PublicSuffix::Domain ...>
+ #
+ # @example Parse a fully qualified domain (subdomain)
+ # PublicSuffix.parse("www.google.com.")
+ # # => #<PublicSuffix::Domain ...>
+ #
+ # @example Parse an invalid domain
+ # PublicSuffix.parse("x.yz")
+ # # => PublicSuffix::DomainInvalid
+ #
+ # @example Parse an URL (not supported, only domains)
+ # PublicSuffix.parse("http://www.google.com")
+ # # => PublicSuffix::DomainInvalid
+ #
+ # @raise [PublicSuffix::Error]
+ # If domain is not a valid domain.
+ # @raise [PublicSuffix::DomainNotAllowed]
+ # If a rule for +domain+ is found, but the rule
+ # doesn't allow +domain+.
+ #
+ def self.parse(domain)
+ rule = List.default.find(domain)
+ if rule.nil?
+ raise DomainInvalid, "`#{domain}' is not a valid domain"
+ end
+ if !rule.allow?(domain)
+ raise DomainNotAllowed, "`#{domain}' is not allowed according to Registry policy"
+ end
+
+ left, right = rule.decompose(domain)
+
+ parts = left.split(".")
+ # If we have 0 parts left, there is just a tld and no domain or subdomain
+ # If we have 1 part left, there is just a tld, domain and not subdomain
+ # If we have 2 parts left, the last part is the domain, the other parts (combined) are the subdomain
+ tld = right
+ sld = parts.empty? ? nil : parts.pop
+ trd = parts.empty? ? nil : parts.join(".")
+
+ Domain.new(tld, sld, trd)
+ end
+
+ # Checks whether +domain+ is assigned and allowed,
+ # without actually parsing it.
+ #
+ # This method doesn't care whether domain is a domain or subdomain.
+ # The validation is performed using the default {PublicSuffix::List}.
+ #
+ # @param [String, #to_s] domain
+ # The domain name or fully qualified domain name to validate.
+ #
+ # @return [Boolean]
+ #
+ # @example Validate a valid domain
+ # PublicSuffix.valid?("example.com")
+ # # => true
+ #
+ # @example Validate a valid subdomain
+ # PublicSuffix.valid?("www.example.com")
+ # # => true
+ #
+ # @example Validate a not-assigned domain
+ # PublicSuffix.valid?("example.zip")
+ # # => false
+ #
+ # @example Validate a not-allowed domain
+ # PublicSuffix.valid?("example.do")
+ # # => false
+ # PublicSuffix.valid?("www.example.do")
+ # # => true
+ #
+ # @example Validate a fully qualified domain
+ # PublicSuffix.valid?("google.com.")
+ # # => true
+ # PublicSuffix.valid?("www.google.com.")
+ # # => true
+ #
+ # @example Check an URL (which is not a valid domain)
+ # PublicSuffix.valid?("http://www.example.com")
+ # # => false
+ #
+ def self.valid?(domain)
+ rule = List.default.find(domain)
+ !rule.nil? && rule.allow?(domain)
+ end
+
+end
View
0 lib/public_suffix_service/definitions.txt → lib/public_suffix/definitions.txt
File renamed without changes.
View
90 lib/public_suffix_service/domain.rb → lib/public_suffix/domain.rb
@@ -1,13 +1,13 @@
#--
-# Public Suffix Service
+# Public Suffix
#
-# Domain Name parser based on the Public Suffix List.
+# Domain name parser based on the Public Suffix List.
#
# Copyright (c) 2009-2011 Simone Carletti <weppos@weppos.net>
#++
-module PublicSuffixService
+module PublicSuffix
class Domain
@@ -33,7 +33,7 @@ def self.domain_to_labels(domain)
domain.to_s.split(".").reverse
end
- # Creates and returns a new {PublicSuffixService::Domain} instance.
+ # Creates and returns a new {PublicSuffix::Domain} instance.
#
# @overload initialize(tld)
# Initializes with a +tld+.
@@ -49,19 +49,19 @@ def self.domain_to_labels(domain)
# @param [String] tld The TRD (subdomain)
#
# @yield [self] Yields on self.
- # @yieldparam [PublicSuffixService::Domain] self The newly creates instance
+ # @yieldparam [PublicSuffix::Domain] self The newly creates instance
#
# @example Initialize with a TLD
- # PublicSuffixService::Domain.new("com")
- # # => #<PublicSuffixService::Domain @tld="com">
+ # PublicSuffix::Domain.new("com")
+ # # => #<PublicSuffix::Domain @tld="com">
#
# @example Initialize with a TLD and SLD
- # PublicSuffixService::Domain.new("com", "example")
- # # => #<PublicSuffixService::Domain @tld="com", @trd=nil>
+ # PublicSuffix::Domain.new("com", "example")
+ # # => #<PublicSuffix::Domain @tld="com", @trd=nil>
#
# @example Initialize with a TLD, SLD and TRD
- # PublicSuffixService::Domain.new("com", "example", "wwww")
- # # => #<PublicSuffixService::Domain @tld="com", @trd=nil, @sld="example">
+ # PublicSuffix::Domain.new("com", "example", "wwww")
+ # # => #<PublicSuffix::Domain @tld="com", @trd=nil, @sld="example">
#
def initialize(*args, &block)
@tld, @sld, @trd = args
@@ -81,10 +81,10 @@ def to_s
#
# @example
#
- # PublicSuffixService::Domain.new("google.com").to_a
+ # PublicSuffix::Domain.new("google.com").to_a
# # => [nil, "google", "com"]
#
- # PublicSuffixService::Domain.new("www.google.com").to_a
+ # PublicSuffix::Domain.new("www.google.com").to_a
# # => [nil, "google", "com"]
#
def to_a
@@ -119,11 +119,11 @@ def trd
# @return [String]
#
# @example Gets the domain name of a domain
- # PublicSuffixService::Domain.new("com", "google").name
+ # PublicSuffix::Domain.new("com", "google").name
# # => "google.com"
#
# @example Gets the domain name of a subdomain
- # PublicSuffixService::Domain.new("com", "google", "www").name
+ # PublicSuffix::Domain.new("com", "google", "www").name
# # => "www.google.com"
#
def name
@@ -133,29 +133,29 @@ def name
# Returns a domain-like representation of this object
# if the object is a {#domain?}, <tt>nil</tt> otherwise.
#
- # PublicSuffixService::Domain.new("com").domain
+ # PublicSuffix::Domain.new("com").domain
# # => nil
#
- # PublicSuffixService::Domain.new("com", "google").domain
+ # PublicSuffix::Domain.new("com", "google").domain
# # => "google.com"
#
- # PublicSuffixService::Domain.new("com", "google", "www").domain
+ # PublicSuffix::Domain.new("com", "google", "www").domain
# # => "www.google.com"
#
# This method doesn't validate the input. It handles the domain
# as a valid domain name and simply applies the necessary transformations.
#
# # This is an invalid domain
- # PublicSuffixService::Domain.new("zip", "google").domain
+ # PublicSuffix::Domain.new("zip", "google").domain
# # => "google.zip"
#
# This method returns a FQD, not just the domain part.
# To get the domain part, use <tt>#sld</tt> (aka second level domain).
#
- # PublicSuffixService::Domain.new("com", "google", "www").domain
+ # PublicSuffix::Domain.new("com", "google", "www").domain
# # => "google.com"
#
- # PublicSuffixService::Domain.new("com", "google", "www").sld
+ # PublicSuffix::Domain.new("com", "google", "www").sld
# # => "google"
#
# @return [String]
@@ -171,29 +171,29 @@ def domain
# Returns a domain-like representation of this object
# if the object is a {#subdomain?}, <tt>nil</tt> otherwise.
#
- # PublicSuffixService::Domain.new("com").subdomain
+ # PublicSuffix::Domain.new("com").subdomain
# # => nil
#
- # PublicSuffixService::Domain.new("com", "google").subdomain
+ # PublicSuffix::Domain.new("com", "google").subdomain
# # => nil
#
- # PublicSuffixService::Domain.new("com", "google", "www").subdomain
+ # PublicSuffix::Domain.new("com", "google", "www").subdomain
# # => "www.google.com"
#
# This method doesn't validate the input. It handles the domain
# as a valid domain name and simply applies the necessary transformations.
#
# # This is an invalid domain
- # PublicSuffixService::Domain.new("zip", "google", "www").subdomain
+ # PublicSuffix::Domain.new("zip", "google", "www").subdomain
# # => "www.google.zip"
#
# This method returns a FQD, not just the domain part.
# To get the domain part, use <tt>#tld</tt> (aka third level domain).
#
- # PublicSuffixService::Domain.new("com", "google", "www").subdomain
+ # PublicSuffix::Domain.new("com", "google", "www").subdomain
# # => "www.google.com"
#
- # PublicSuffixService::Domain.new("com", "google", "www").trd
+ # PublicSuffix::Domain.new("com", "google", "www").trd
# # => "www"
#
# @return [String]
@@ -207,9 +207,9 @@ def subdomain
end
# Returns the rule matching this domain
- # in the default {PublicSuffixService::List}.
+ # in the default {PublicSuffix::List}.
#
- # @return [PublicSuffixService::Rule::Base, nil]
+ # @return [PublicSuffix::Rule::Base, nil]
# The rule instance a rule matches current domain,
# nil if no rule is found.
def rule
@@ -229,18 +229,18 @@ def rule
#
# @example
#
- # PublicSuffixService::Domain.new("com").domain?
+ # PublicSuffix::Domain.new("com").domain?
# # => false
#
- # PublicSuffixService::Domain.new("com", "google").domain?
+ # PublicSuffix::Domain.new("com", "google").domain?
# # => true
#
- # PublicSuffixService::Domain.new("com", "google", "www").domain?
+ # PublicSuffix::Domain.new("com", "google", "www").domain?
# # => true
#
# # This is an invalid domain, but returns true
# # because this method doesn't validate the content.
- # PublicSuffixService::Domain.new("zip", "google").domain?
+ # PublicSuffix::Domain.new("zip", "google").domain?
# # => true
#
# @see #subdomain?
@@ -261,18 +261,18 @@ def domain?
#
# @example
#
- # PublicSuffixService::Domain.new("com").subdomain?
+ # PublicSuffix::Domain.new("com").subdomain?
# # => false
#
- # PublicSuffixService::Domain.new("com", "google").subdomain?
+ # PublicSuffix::Domain.new("com", "google").subdomain?
# # => false
#
- # PublicSuffixService::Domain.new("com", "google", "www").subdomain?
+ # PublicSuffix::Domain.new("com", "google", "www").subdomain?
# # => true
#
# # This is an invalid domain, but returns true
# # because this method doesn't validate the content.
- # PublicSuffixService::Domain.new("zip", "google", "www").subdomain?
+ # PublicSuffix::Domain.new("zip", "google", "www").subdomain?
# # => true
#
# @see #domain?
@@ -335,17 +335,17 @@ def valid?
#
# @example
#
- # PublicSuffixService::Domain.new("com").domain?
+ # PublicSuffix::Domain.new("com").domain?
# # => false
#
- # PublicSuffixService::Domain.new("com", "google").domain?
+ # PublicSuffix::Domain.new("com", "google").domain?
# # => true
#
- # PublicSuffixService::Domain.new("com", "google", "www").domain?
+ # PublicSuffix::Domain.new("com", "google", "www").domain?
# # => true
#
# # This is an invalid domain
- # PublicSuffixService::Domain.new("zip", "google").false?
+ # PublicSuffix::Domain.new("zip", "google").false?
# # => true
#
# @see #domain?
@@ -362,17 +362,17 @@ def valid_domain?
#
# @example
#
- # PublicSuffixService::Domain.new("com").subdomain?
+ # PublicSuffix::Domain.new("com").subdomain?
# # => false
#
- # PublicSuffixService::Domain.new("com", "google").subdomain?
+ # PublicSuffix::Domain.new("com", "google").subdomain?
# # => false
#
- # PublicSuffixService::Domain.new("com", "google", "www").subdomain?
+ # PublicSuffix::Domain.new("com", "google", "www").subdomain?
# # => true
#
# # This is an invalid domain
- # PublicSuffixService::Domain.new("zip", "google", "www").subdomain?
+ # PublicSuffix::Domain.new("zip", "google", "www").subdomain?
# # => false
#
# @see #subdomain?
View
24 lib/public_suffix_service/errors.rb → lib/public_suffix/errors.rb
@@ -1,13 +1,13 @@
#--
-# Public Suffix Service
+# Public Suffix
#
-# Domain Name parser based on the Public Suffix List.
+# Domain name parser based on the Public Suffix List.
#
# Copyright (c) 2009-2011 Simone Carletti <weppos@weppos.net>
#++
-module PublicSuffixService
+module PublicSuffix
class Error < StandardError
end
@@ -18,11 +18,11 @@ class Error < StandardError
#
# @example
#
- # PublicSuffixService.parse("nic.test")
- # # => PublicSuffixService::DomainInvalid
+ # PublicSuffix.parse("nic.test")
+ # # => PublicSuffix::DomainInvalid
#
- # PublicSuffixService.parse("http://www.nic.it")
- # # => PublicSuffixService::DomainInvalid
+ # PublicSuffix.parse("http://www.nic.it")
+ # # => PublicSuffix::DomainInvalid
#
# @since 0.6.0
#
@@ -36,11 +36,11 @@ class DomainInvalid < Error
#
# @example
#
- # PublicSuffixService.parse("nic.do")
- # # => PublicSuffixService::DomainNotAllowed
+ # PublicSuffix.parse("nic.do")
+ # # => PublicSuffix::DomainNotAllowed
#
- # PublicSuffixService.parse("www.nic.do")
- # # => PublicSuffixService::Domain
+ # PublicSuffix.parse("www.nic.do")
+ # # => PublicSuffix::Domain
#
# @since 0.6.0
#
@@ -50,7 +50,7 @@ class DomainNotAllowed < DomainInvalid
# Backward Compatibility
#
- # @deprecated Use {PublicSuffixService::DomainInvalid}.
+ # @deprecated Use {PublicSuffix::DomainInvalid}.
#
InvalidDomain = DomainInvalid
View
64 lib/public_suffix_service/list.rb → lib/public_suffix/list.rb
@@ -1,51 +1,51 @@
#--
-# Public Suffix Service
+# Public Suffix
#
-# Domain Name parser based on the Public Suffix List.
+# Domain name parser based on the Public Suffix List.
#
# Copyright (c) 2009-2011 Simone Carletti <weppos@weppos.net>
#++
-module PublicSuffixService
+module PublicSuffix
- # A {PublicSuffixService::List} is a collection of one
- # or more {PublicSuffixService::Rule}.
+ # A {PublicSuffix::List} is a collection of one
+ # or more {PublicSuffix::Rule}.
#
- # Given a {PublicSuffixService::List},
- # you can add or remove {PublicSuffixService::Rule},
+ # Given a {PublicSuffix::List},
+ # you can add or remove {PublicSuffix::Rule},
# iterate all items in the list or search for the first rule
# which matches a specific domain name.
#
# # Create a new list
- # list = PublicSuffixService::List.new
+ # list = PublicSuffix::List.new
#
# # Push two rules to the list
- # list << PublicSuffixService::Rule.factory("it")
- # list << PublicSuffixService::Rule.factory("com")
+ # list << PublicSuffix::Rule.factory("it")
+ # list << PublicSuffix::Rule.factory("com")
#
# # Get the size of the list
# list.size
# # => 2
#
# # Search for the rule matching given domain
# list.find("example.com")
- # # => #<PublicSuffixService::Rule::Normal>
+ # # => #<PublicSuffix::Rule::Normal>
# list.find("example.org")
# # => nil
#
- # You can create as many {PublicSuffixService::List} you want.
- # The {PublicSuffixService::List.default} rule list is used
+ # You can create as many {PublicSuffix::List} you want.
+ # The {PublicSuffix::List.default} rule list is used
# to tokenize and validate a domain.
#
- # {PublicSuffixService::List} implements +Enumerable+ module.
+ # {PublicSuffix::List} implements +Enumerable+ module.
#
class List
include Enumerable
# Gets the array of rules.
#
- # @return [Array<PublicSuffixService::Rule::*>]
+ # @return [Array<PublicSuffix::Rule::*>]
attr_reader :rules
# Gets the naive index, a hash that with the keys being the first label of
@@ -55,10 +55,10 @@ class List
attr_reader :indexes
- # Initializes an empty {PublicSuffixService::List}.
+ # Initializes an empty {PublicSuffix::List}.
#
# @yield [self] Yields on self.
- # @yieldparam [PublicSuffixService::List] self The newly created instance.
+ # @yieldparam [PublicSuffix::List] self The newly created instance.
#
def initialize(&block)
@rules = []
@@ -69,7 +69,7 @@ def initialize(&block)
# Creates a naive index for +@rules+. Just a hash that will tell
# us where the elements of +@rules+ are relative to its first
- # {PublicSuffixService::Rule::Base#labels} element.
+ # {PublicSuffix::Rule::Base#labels} element.
#
# For instance if @rules[5] and @rules[4] are the only elements of the list
# where Rule#labels.first is 'us' @indexes['us'] #=> [5,4], that way in
@@ -87,10 +87,10 @@ def create_index!
# Checks whether two lists are equal.
#
# List <tt>one</tt> is equal to <tt>two</tt>, if <tt>two</tt> is an instance of
- # {PublicSuffixService::List} and each +PublicSuffixService::Rule::*+
+ # {PublicSuffix::List} and each +PublicSuffix::Rule::*+
# in list <tt>one</tt> is available in list <tt>two</tt>, in the same order.
#
- # @param [PublicSuffixService::List] other
+ # @param [PublicSuffix::List] other
# The List to compare.
#
# @return [Boolean]
@@ -108,15 +108,15 @@ def each(*args, &block)
# Gets the list as array.
#
- # @return [Array<PublicSuffixService::Rule::*>]
+ # @return [Array<PublicSuffix::Rule::*>]
def to_a
@rules
end
# Adds the given object to the list
# and optionally refreshes the rule index.
#
- # @param [PublicSuffixService::Rule::*] rule
+ # @param [PublicSuffix::Rule::*] rule
# The rule to add to the list.
# @param [Boolean] index
# Set to true to recreate the rule index
@@ -179,7 +179,7 @@ def clear
#
# @param [String, #to_s] domain The domain name.
#
- # @return [PublicSuffixService::Rule::*, nil]
+ # @return [PublicSuffix::Rule::*, nil]
def find(domain)
rules = select(domain)
rules.select { |r| r.type == :exception }.first ||
@@ -193,7 +193,7 @@ def find(domain)
#
# @param [String, #to_s] domain The domain name.
#
- # @return [Array<PublicSuffixService::Rule::*>]
+ # @return [Array<PublicSuffix::Rule::*>]
def select(domain)
indices = (@indexes[Domain.domain_to_labels(domain).first] || [])
@rules.values_at(*indices).select { |rule| rule.match?(domain) }
@@ -205,20 +205,20 @@ def select(domain)
class << self
# Gets the default rule list.
- # Initializes a new {PublicSuffixService::List} parsing the content
- # of {PublicSuffixService::List.default_definition}, if required.
+ # Initializes a new {PublicSuffix::List} parsing the content
+ # of {PublicSuffix::List.default_definition}, if required.
#
- # @return [PublicSuffixService::List]
+ # @return [PublicSuffix::List]
def default
@@default ||= parse(default_definition)
end
# Sets the default rule list to +value+.
#
- # @param [PublicSuffixService::List] value
+ # @param [PublicSuffix::List] value
# The new rule list.
#
- # @return [PublicSuffixService::List]
+ # @return [PublicSuffix::List]
def default=(value)
@@default = value
end
@@ -232,9 +232,9 @@ def clear
end
# Resets the default rule list and reinitialize it
- # parsing the content of {PublicSuffixService::List.default_definition}.
+ # parsing the content of {PublicSuffix::List.default_definition}.
#
- # @return [PublicSuffixService::List]
+ # @return [PublicSuffix::List]
def reload
self.clear.default
end
@@ -256,7 +256,7 @@ def default_definition
#
# @param [String] input The rule list to parse.
#
- # @return [Array<PublicSuffixService::Rule::*>]
+ # @return [Array<PublicSuffix::Rule::*>]
def parse(input)
new do |list|
input.each_line do |line|
View
58 lib/public_suffix_service/rule.rb → lib/public_suffix/rule.rb
@@ -1,24 +1,24 @@
#--
-# Public Suffix Service
+# Public Suffix
#
-# Domain Name parser based on the Public Suffix List.
+# Domain name parser based on the Public Suffix List.
#
# Copyright (c) 2009-2011 Simone Carletti <weppos@weppos.net>
#++
-module PublicSuffixService
+module PublicSuffix
# A Rule is a special object which holds a single definition
# of the Public Suffix List.
#
# There are 3 types of ruleas, each one represented by a specific
- # subclass within the +PublicSuffixService::Rule+ namespace.
+ # subclass within the +PublicSuffix::Rule+ namespace.
#
- # To create a new Rule, use the {PublicSuffixService::Rule#factory} method.
+ # To create a new Rule, use the {PublicSuffix::Rule#factory} method.
#
- # PublicSuffixService::Rule.factory("ar")
- # # => #<PublicSuffixService::Rule::Normal>
+ # PublicSuffix::Rule.factory("ar")
+ # # => #<PublicSuffix::Rule::Normal>
#
class Rule
@@ -28,19 +28,19 @@ class Rule
#
# @param [String] name The rule definition.
#
- # @return [PublicSuffixService::Rule::*] A rule instance.
+ # @return [PublicSuffix::Rule::*] A rule instance.
#
# @example Creates a Normal rule
- # PublicSuffixService::Rule.factory("ar")
- # # => #<PublicSuffixService::Rule::Normal>
+ # PublicSuffix::Rule.factory("ar")
+ # # => #<PublicSuffix::Rule::Normal>
#
# @example Creates a Wildcard rule
- # PublicSuffixService::Rule.factory("*.ar")
- # # => #<PublicSuffixService::Rule::Wildcard>
+ # PublicSuffix::Rule.factory("*.ar")
+ # # => #<PublicSuffix::Rule::Wildcard>
#
# @example Creates an Exception rule
- # PublicSuffixService::Rule.factory("!congresodelalengua3.ar")
- # # => #<PublicSuffixService::Rule::Exception>
+ # PublicSuffix::Rule.factory("!congresodelalengua3.ar")
+ # # => #<PublicSuffix::Rule::Exception>
#
def self.factory(name)
klass = case name.to_s[0..0]
@@ -63,9 +63,9 @@ def self.factory(name)
# of this class is to expose a common interface
# for all the available subclasses.
#
- # * {PublicSuffixService::Rule::Normal}
- # * {PublicSuffixService::Rule::Exception}
- # * {PublicSuffixService::Rule::Wildcard}
+ # * {PublicSuffix::Rule::Normal}
+ # * {PublicSuffix::Rule::Exception}
+ # * {PublicSuffix::Rule::Wildcard}
#
# == Properties
#
@@ -80,8 +80,8 @@ def self.factory(name)
#
# Here's an example
#
- # PublicSuffixService::Rule.factory("*.google.com")
- # #<PublicSuffixService::Rule::Wildcard:0x1015c14b0
+ # PublicSuffix::Rule.factory("*.google.com")
+ # #<PublicSuffix::Rule::Wildcard:0x1015c14b0
# @labels=["com", "google"],
# @name="*.google.com",
# @type=:wildcard,
@@ -91,13 +91,13 @@ def self.factory(name)
# == Rule Creation
#
# The best way to create a new rule is passing the rule name
- # to the <tt>PublicSuffixService::Rule.factory</tt> method.
+ # to the <tt>PublicSuffix::Rule.factory</tt> method.
#
- # PublicSuffixService::Rule.factory("com")
- # # => PublicSuffixService::Rule::Normal
+ # PublicSuffix::Rule.factory("com")
+ # # => PublicSuffix::Rule::Normal
#
- # PublicSuffixService::Rule.factory("*.com")
- # # => PublicSuffixService::Rule::Wildcard
+ # PublicSuffix::Rule.factory("*.com")
+ # # => PublicSuffix::Rule::Wildcard
#
# This method will detect the rule type and create an instance
# from the proper rule class.
@@ -112,7 +112,7 @@ def self.factory(name)
# can be handled by the current rule.
# You can use the <tt>#match?</tt> method.
#
- # rule = PublicSuffixService::Rule.factory("com")
+ # rule = PublicSuffix::Rule.factory("com")
#
# rule.match?("google.com")
# # => true
@@ -126,7 +126,7 @@ def self.factory(name)
#
# When you have the right rule, you can use it to tokenize the domain name.
#
- # rule = PublicSuffixService::Rule.factory("com")
+ # rule = PublicSuffix::Rule.factory("com")
#
# rule.decompose("google.com")
# # => ["google", "com"]
@@ -157,7 +157,7 @@ def initialize(name, value = nil)
# Checks whether this rule is equal to <tt>other</tt>.
#
- # @param [PublicSuffixService::Rule::*] other
+ # @param [PublicSuffix::Rule::*] other
# The rule to compare.
#
# @return [Boolean]
@@ -180,7 +180,7 @@ def ==(other)
#
# @example
# rule = Rule.factory("com")
- # # #<PublicSuffixService::Rule::Normal>
+ # # #<PublicSuffix::Rule::Normal>
# rule.match?("example.com")
# # => true
# rule.match?("example.net")
@@ -201,7 +201,7 @@ def match?(domain)
#
# @example
# rule = Rule.factory("*.do")
- # # => #<PublicSuffixService::Rule::Wildcard>
+ # # => #<PublicSuffix::Rule::Wildcard>
# rule.allow?("example.do")
# # => false
# rule.allow?("www.example.do")
View
14 lib/public_suffix/rule_list.rb
@@ -0,0 +1,14 @@
+#--
+# Public Suffix
+#
+# Domain name parser based on the Public Suffix List.
+#
+# Copyright (c) 2009-2011 Simone Carletti <weppos@weppos.net>
+#++
+
+
+warn("The PublicSuffix::RuleList object has been deprecated and will be removed in PublicSuffix 1.0. Please use PublicSuffix::List instead.")
+
+module PublicSuffix
+ RuleList = List
+end
View
6 lib/public_suffix_service/version.rb → lib/public_suffix/version.rb
@@ -1,13 +1,13 @@
#--
-# Public Suffix Service
+# Public Suffix
#
-# Domain Name parser based on the Public Suffix List.
+# Domain name parser based on the Public Suffix List.
#
# Copyright (c) 2009-2011 Simone Carletti <weppos@weppos.net>
#++
-module PublicSuffixService
+module PublicSuffix
module Version
MAJOR = 0
View
130 lib/public_suffix_service.rb
@@ -1,134 +1,14 @@
#--
-# Public Suffix Service
+# Public Suffix
#
-# Domain Name parser based on the Public Suffix List.
+# Domain name parser based on the Public Suffix List.
#
# Copyright (c) 2009-2011 Simone Carletti <weppos@weppos.net>
#++
-require 'public_suffix_service/domain'
-require 'public_suffix_service/version'
-require 'public_suffix_service/errors'
-require 'public_suffix_service/rule'
-require 'public_suffix_service/list'
+warn("The PublicSuffixService object has been deprecated. Please use PublicSuffix instead.")
+require 'public_suffix'
-module PublicSuffixService
-
- NAME = "Public Suffix Service"
- GEM = "public_suffix_service"
- AUTHORS = ["Simone Carletti <weppos@weppos.net>"]
-
-
- # Backwards compatibility
- autoload :RuleList, 'public_suffix_service/rule_list'
-
-
- # Parses +domain+ and returns the
- # {PublicSuffixService::Domain} instance.
- #
- # Parsing uses the default {PublicSuffixService::List}.
- #
- # @param [String, #to_s] domain
- # The domain name or fully qualified domain name to parse.
- #
- # @return [PublicSuffixService::Domain]
- #
- # @example Parse a valid domain
- # PublicSuffixService.parse("google.com")
- # # => #<PublicSuffixService::Domain ...>
- #
- # @example Parse a valid subdomain
- # PublicSuffixService.parse("www.google.com")
- # # => #<PublicSuffixService::Domain ...>
- #
- # @example Parse a fully qualified domain
- # PublicSuffixService.parse("google.com.")
- # # => #<PublicSuffixService::Domain ...>
- #
- # @example Parse a fully qualified domain (subdomain)
- # PublicSuffixService.parse("www.google.com.")
- # # => #<PublicSuffixService::Domain ...>
- #
- # @example Parse an invalid domain
- # PublicSuffixService.parse("x.yz")
- # # => PublicSuffixService::DomainInvalid
- #
- # @example Parse an URL (not supported, only domains)
- # PublicSuffixService.parse("http://www.google.com")
- # # => PublicSuffixService::DomainInvalid
- #
- # @raise [PublicSuffixService::Error]
- # If domain is not a valid domain.
- # @raise [PublicSuffixService::DomainNotAllowed]
- # If a rule for +domain+ is found, but the rule
- # doesn't allow +domain+.
- #
- def self.parse(domain)
- rule = List.default.find(domain)
- if rule.nil?
- raise DomainInvalid, "`#{domain}' is not a valid domain"
- end
- if !rule.allow?(domain)
- raise DomainNotAllowed, "`#{domain}' is not allowed according to Registry policy"
- end
-
- left, right = rule.decompose(domain)
-
- parts = left.split(".")
- # If we have 0 parts left, there is just a tld and no domain or subdomain
- # If we have 1 part left, there is just a tld, domain and not subdomain
- # If we have 2 parts left, the last part is the domain, the other parts (combined) are the subdomain
- tld = right
- sld = parts.empty? ? nil : parts.pop
- trd = parts.empty? ? nil : parts.join(".")
-
- Domain.new(tld, sld, trd)
- end
-
- # Checks whether +domain+ is assigned and allowed,
- # without actually parsing it.
- #
- # This method doesn't care whether domain is a domain or subdomain.
- # The validation is performed using the default {PublicSuffixService::List}.
- #
- # @param [String, #to_s] domain
- # The domain name or fully qualified domain name to validate.
- #
- # @return [Boolean]
- #
- # @example Validate a valid domain
- # PublicSuffixService.valid?("example.com")
- # # => true
- #
- # @example Validate a valid subdomain
- # PublicSuffixService.valid?("www.example.com")
- # # => true
- #
- # @example Validate a not-assigned domain
- # PublicSuffixService.valid?("example.zip")
- # # => false
- #
- # @example Validate a not-allowed domain
- # PublicSuffixService.valid?("example.do")
- # # => false
- # PublicSuffixService.valid?("www.example.do")
- # # => true
- #
- # @example Validate a fully qualified domain
- # PublicSuffixService.valid?("google.com.")
- # # => true
- # PublicSuffixService.valid?("www.google.com.")
- # # => true
- #
- # @example Check an URL (which is not a valid domain)
- # PublicSuffixService.valid?("http://www.example.com")
- # # => false
- #
- def self.valid?(domain)
- rule = List.default.find(domain)
- !rule.nil? && rule.allow?(domain)
- end
-
-end
+PublicSuffixService = PublicSuffix
View
14 lib/public_suffix_service/rule_list.rb
@@ -1,14 +0,0 @@
-#--
-# Public Suffix Service
-#
-# Domain Name parser based on the Public Suffix List.
-#
-# Copyright (c) 2009-2011 Simone Carletti <weppos@weppos.net>
-#++
-
-
-warn("The PublicSuffixService::RuleList object has been deprecated and will be removed in PublicSuffixService 1.0. Please use PublicSuffixService::List instead.")
-
-module PublicSuffixService
- RuleList = List
-end
View
6 test/acceptance_test.rb
@@ -15,7 +15,7 @@ class AcceptanceTest < Test::Unit::TestCase
def test_valid
ValidCases.each do |name, results|
- domain = PublicSuffixService.parse(name)
+ domain = PublicSuffix.parse(name)
trd, sld, tld = results
assert_equal tld, domain.tld, "Invalid tld for '#{name}'"
assert_equal sld, domain.sld, "Invalid sld for '#{name}'"
@@ -24,12 +24,12 @@ def test_valid
end
InvalidCases = {
- "nic.ke" => PublicSuffixService::DomainNotAllowed,
+ "nic.ke" => PublicSuffix::DomainNotAllowed,
}
def test_invalid
InvalidCases.each do |name, error|
- assert_raise(error) { PublicSuffixService.parse(name) }
+ assert_raise(error) { PublicSuffix.parse(name) }
end
end
View
23 test/public_suffix_service/errors_test.rb
@@ -1,23 +0,0 @@
-require 'test_helper'
-
-class ErrorsTest < Test::Unit::TestCase
-
- # Inherits from StandardError
- def test_error_inheritance
- assert_kind_of StandardError,
- PublicSuffixService::Error.new
- end
-
- # Inherits from PublicSuffixService::Error
- def test_domain_invalid_inheritance
- assert_kind_of PublicSuffixService::Error,
- PublicSuffixService::DomainInvalid.new
- end
-
- # Inherits from PublicSuffixService::DomainInvalid
- def test_domain_not_allowed_inheritance
- assert_kind_of PublicSuffixService::DomainInvalid,
- PublicSuffixService::DomainNotAllowed.new
- end
-
-end
View
193 test/public_suffix_service/list_test.rb
@@ -1,193 +0,0 @@
-require 'test_helper'
-
-class PublicSuffixService::ListTest < Test::Unit::TestCase
-
- def setup
- @list = PublicSuffixService::List.new
- end
-
- def teardown
- PublicSuffixService::List.clear
- end
-
-
- def test_initialize
- assert_instance_of PublicSuffixService::List, @list
- assert_equal 0, @list.length
- end
-
- def test_initialize_create_index_when_empty
- assert_equal({}, @list.indexes)
- end
-
- def test_indexes
- @list = PublicSuffixService::List.parse(<<EOS)
-// com : http://en.wikipedia.org/wiki/.com
-com
-
-// uk : http://en.wikipedia.org/wiki/.uk
-*.uk
-*.sch.uk
-!bl.uk
-!british-library.uk
-EOS
-
- assert !@list.indexes.empty?
- assert_equal [1,2,3,4], @list.indexes.delete('uk')
- assert_equal [0], @list.indexes.delete('com')
- assert @list.indexes.empty?
- end
-
-
- def test_equality_with_self
- list = PublicSuffixService::List.new
- assert_equal list, list
- end
-
- def test_equality_with_internals
- rule = PublicSuffixService::Rule.factory("com")
- assert_equal PublicSuffixService::List.new.add(rule), PublicSuffixService::List.new.add(rule)
- end
-
-
- def test_add
- assert_equal @list, @list.add(PublicSuffixService::Rule.factory(""))
- assert_equal @list, @list << PublicSuffixService::Rule.factory("")
- assert_equal 2, @list.length
- end
-
- def test_add_should_recreate_index
- @list = PublicSuffixService::List.parse("com")
- assert_equal PublicSuffixService::Rule.factory("com"), @list.find("google.com")
- assert_equal nil, @list.find("google.net")
-
- @list << PublicSuffixService::Rule.factory("net")
- assert_equal PublicSuffixService::Rule.factory("com"), @list.find("google.com")
- assert_equal PublicSuffixService::Rule.factory("net"), @list.find("google.net")
- end
-
- def test_empty?
- assert @list.empty?
- @list.add(PublicSuffixService::Rule.factory(""))
- assert !@list.empty?
- end
-
- def test_size
- assert_equal 0, @list.length
- assert_equal @list, @list.add(PublicSuffixService::Rule.factory(""))
- assert_equal 1, @list.length
- end
-
- def test_clear
- assert_equal 0, @list.length
- assert_equal @list, @list.add(PublicSuffixService::Rule.factory(""))
- assert_equal 1, @list.length
- assert_equal @list, @list.clear
- assert_equal 0, @list.length
- end
-
-
- def test_find
- @list = PublicSuffixService::List.parse(<<EOS)
-// com : http://en.wikipedia.org/wiki/.com
-com
-
-// uk : http://en.wikipedia.org/wiki/.uk
-*.uk
-*.sch.uk
-!bl.uk
-!british-library.uk
-EOS
- assert_equal PublicSuffixService::Rule.factory("com"), @list.find("google.com")
- assert_equal PublicSuffixService::Rule.factory("com"), @list.find("foo.google.com")
- assert_equal PublicSuffixService::Rule.factory("*.uk"), @list.find("google.uk")
- assert_equal PublicSuffixService::Rule.factory("*.uk"), @list.find("google.co.uk")
- assert_equal PublicSuffixService::Rule.factory("*.uk"), @list.find("foo.google.co.uk")
- assert_equal PublicSuffixService::Rule.factory("!british-library.uk"), @list.find("british-library.uk")
- assert_equal PublicSuffixService::Rule.factory("!british-library.uk"), @list.find("foo.british-library.uk")
- end
-
- def test_select
- @list = PublicSuffixService::List.parse(<<EOS)
-// com : http://en.wikipedia.org/wiki/.com
-com
-
-// uk : http://en.wikipedia.org/wiki/.uk
-*.uk
-*.sch.uk
-!bl.uk
-!british-library.uk
-EOS
- assert_equal 2, @list.select("british-library.uk").size
- end
-
-
- def test_self_default_getter
- assert_equal nil, PublicSuffixService::List.send(:class_variable_get, :"@@default")
- PublicSuffixService::List.default
- assert_not_equal nil, PublicSuffixService::List.send(:class_variable_get, :"@@default")
- end
-
- def test_self_default_setter
- PublicSuffixService::List.default
- assert_not_equal nil, PublicSuffixService::List.send(:class_variable_get, :"@@default")
- PublicSuffixService::List.default = nil
- assert_equal nil, PublicSuffixService::List.send(:class_variable_get, :"@@default")
- end
-
- def test_self_clear
- PublicSuffixService::List.default
- assert_not_equal nil, PublicSuffixService::List.send(:class_variable_get, :"@@default")
- PublicSuffixService::List.clear
- assert_equal nil, PublicSuffixService::List.send(:class_variable_get, :"@@default")
- end
-
- def test_self_reload
- PublicSuffixService::List.default
- PublicSuffixService::List.expects(:default_definition).returns("")
-
- PublicSuffixService::List.reload
- assert_equal PublicSuffixService::List.new, PublicSuffixService::List.default
- end
-
-
- def test_self_parse
- list = PublicSuffixService::List.parse(<<EOS)
-// ***** BEGIN LICENSE BLOCK *****
-// Version: MPL 1.1/GPL 2.0/LGPL 2.1
-//
-// ***** END LICENSE BLOCK *****
-
-// ac : http://en.wikipedia.org/wiki/.ac
-ac
-com.ac
-
-// ad : http://en.wikipedia.org/wiki/.ad
-ad
-
-// ar : http://en.wikipedia.org/wiki/.ar
-*.ar
-!congresodelalengua3.ar
-EOS
-
- assert_instance_of PublicSuffixService::List, list
- assert_equal 5, list.length
- assert_equal %w(ac com.ac ad *.ar !congresodelalengua3.ar).map { |name| PublicSuffixService::Rule.factory(name) }, list.to_a
- end
-
- def test_self_parse_should_create_cache
- list = PublicSuffixService::List.parse(<<EOS)
-// com : http://en.wikipedia.org/wiki/.com
-com
-
-// uk : http://en.wikipedia.org/wiki/.uk
-*.uk
-*.sch.uk
-!bl.uk
-!british-library.uk
-EOS
-
- assert_equal PublicSuffixService::Rule.factory("com"), list.find("google.com")
- end
-
-end
View
85 test/public_suffix_service_test.rb
@@ -1,85 +0,0 @@
-require 'test_helper'
-
-class PublicSuffixServiceTest < Test::Unit::TestCase
-
- def test_self_parse_a_domain_with_tld_and_sld
- domain = PublicSuffixService.parse("example.com")
- assert_instance_of PublicSuffixService::Domain, domain
- assert_equal "com", domain.tld
- assert_equal "example", domain.sld
- assert_equal nil, domain.trd
-
- domain = PublicSuffixService.parse("example.co.uk")
- assert_instance_of PublicSuffixService::Domain, domain
- assert_equal "co.uk", domain.tld
- assert_equal "example", domain.sld
- assert_equal nil, domain.trd
- end
-
- def test_self_parse_a_domain_with_tld_and_sld_and_trd
- domain = PublicSuffixService.parse("alpha.example.com")
- assert_instance_of PublicSuffixService::Domain, domain
- assert_equal "com", domain.tld
- assert_equal "example", domain.sld
- assert_equal "alpha", domain.trd
-
- domain = PublicSuffixService.parse("alpha.example.co.uk")
- assert_instance_of PublicSuffixService::Domain, domain
- assert_equal "co.uk", domain.tld
- assert_equal "example", domain.sld
- assert_equal "alpha", domain.trd
- end
-
- def test_self_parse_a_domain_with_tld_and_sld_and_4rd
- domain = PublicSuffixService.parse("one.two.example.com")
- assert_instance_of PublicSuffixService::Domain, domain
- assert_equal "com", domain.tld
- assert_equal "example", domain.sld
- assert_equal "one.two", domain.trd
-
- domain = PublicSuffixService.parse("one.two.example.co.uk")
- assert_instance_of PublicSuffixService::Domain, domain
- assert_equal "co.uk", domain.tld
- assert_equal "example", domain.sld
- assert_equal "one.two", domain.trd
- end
-
- def test_self_parse_a_fully_qualified_domain_name
- domain = PublicSuffixService.parse("www.example.com.")
- assert_instance_of PublicSuffixService::Domain, domain
- assert_equal "com", domain.tld
- assert_equal "example", domain.sld
- assert_equal "www", domain.trd
- end
-
- def test_self_parse_should_raise_with_invalid_domain
- error = assert_raise(PublicSuffixService::DomainInvalid) { PublicSuffixService.parse("example.zip") }
- assert_match %r{example\.zip}, error.message
- end
-
- def test_self_parse_should_raise_with_unallowed_domain
- error = assert_raise(PublicSuffixService::DomainNotAllowed) { PublicSuffixService.parse("example.ke") }
- assert_match %r{example\.ke}, error.message
- end
-
-
- def test_self_valid
- assert PublicSuffixService.valid?("google.com")
- assert PublicSuffixService.valid?("www.google.com")
- assert PublicSuffixService.valid?("google.co.uk")
- assert PublicSuffixService.valid?("www.google.co.uk")
- end
-
- # Returns false when domain has an invalid TLD
- def test_self_valid_with_invalid_tld
- assert !PublicSuffixService.valid?("google.zip")
- assert !PublicSuffixService.valid?("www.google.zip")
- end
-
- def test_self_valid_with_fully_qualified_domain_name
- assert PublicSuffixService.valid?("google.com.")
- assert PublicSuffixService.valid?("google.co.uk.")
- assert !PublicSuffixService.valid?("google.zip.")
- end
-
-end
View
20 test/public_suffix_service/domain_test.rb → test/unit/domain_test.rb
@@ -1,31 +1,31 @@
require 'test_helper'
-class PublicSuffixService::DomainTest < Test::Unit::TestCase
+class PublicSuffix::DomainTest < Test::Unit::TestCase
def setup
- @klass = PublicSuffixService::Domain
+ @klass = PublicSuffix::Domain
end
# Tokenizes given input into labels.
def test_self_domain_to_labels
assert_equal %w( com live spaces someone ),
- PublicSuffixService::Domain.domain_to_labels("someone.spaces.live.com")
+ PublicSuffix::Domain.domain_to_labels("someone.spaces.live.com")
assert_equal %w( com zoho wiki leontina23samiko ),
- PublicSuffixService::Domain.domain_to_labels("leontina23samiko.wiki.zoho.com")
+ PublicSuffix::Domain.domain_to_labels("leontina23samiko.wiki.zoho.com")
end
# Converts input into String.
def test_self_domain_to_labels_converts_input_to_string
assert_equal %w( com live spaces someone ),
- PublicSuffixService::Domain.domain_to_labels(:"someone.spaces.live.com")
+ PublicSuffix::Domain.domain_to_labels(:"someone.spaces.live.com")
end
# Ignores trailing .
def test_self_domain_to_labels_ignores_trailing_dot
assert_equal %w( com live spaces someone ),
- PublicSuffixService::Domain.domain_to_labels("someone.spaces.live.com")
+ PublicSuffix::Domain.domain_to_labels("someone.spaces.live.com")
assert_equal %w( com live spaces someone ),
- PublicSuffixService::Domain.domain_to_labels(:"someone.spaces.live.com")
+ PublicSuffix::Domain.domain_to_labels(:"someone.spaces.live.com")
end
@@ -103,9 +103,9 @@ def test_subdomain
def test_rule
assert_equal nil, @klass.new("zip").rule
- assert_equal PublicSuffixService::Rule.factory("com"), @klass.new("com").rule
- assert_equal PublicSuffixService::Rule.factory("com"), @klass.new("com", "google").rule
- assert_equal PublicSuffixService::Rule.factory("com"), @klass.new("com", "google", "www").rule
+ assert_equal PublicSuffix::Rule.factory("com"), @klass.new("com").rule
+ assert_equal PublicSuffix::Rule.factory("com"), @klass.new("com", "google").rule
+ assert_equal PublicSuffix::Rule.factory("com"), @klass.new("com", "google", "www").rule
end
View
23 test/unit/errors_test.rb
@@ -0,0 +1,23 @@
+require 'test_helper'
+
+class ErrorsTest < Test::Unit::TestCase
+
+ # Inherits from StandardError
+ def test_error_inheritance
+ assert_kind_of StandardError,
+ PublicSuffix::Error.new
+ end
+
+ # Inherits from PublicSuffix::Error
+ def test_domain_invalid_inheritance
+ assert_kind_of PublicSuffix::Error,
+ PublicSuffix::DomainInvalid.new
+ end
+
+ # Inherits from PublicSuffix::DomainInvalid
+ def test_domain_not_allowed_inheritance
+ assert_kind_of PublicSuffix::DomainInvalid,
+ PublicSuffix::DomainNotAllowed.new
+ end
+
+end
View
193 test/unit/list_test.rb
@@ -0,0 +1,193 @@
+require 'test_helper'
+
+class PublicSuffix::ListTest < Test::Unit::TestCase
+
+ def setup
+ @list = PublicSuffix::List.new
+ end
+
+ def teardown
+ PublicSuffix::List.clear
+ end
+
+
+ def test_initialize
+ assert_instance_of PublicSuffix::List, @list
+ assert_equal 0, @list.length
+ end
+
+ def test_initialize_create_index_when_empty
+ assert_equal({}, @list.indexes)
+ end
+
+ def test_indexes
+ @list = PublicSuffix::List.parse(<<EOS)
+// com : http://en.wikipedia.org/wiki/.com
+com
+
+// uk : http://en.wikipedia.org/wiki/.uk
+*.uk
+*.sch.uk
+!bl.uk
+!british-library.uk
+EOS
+
+ assert !@list.indexes.empty?
+ assert_equal [1,2,3,4], @list.indexes.delete('uk')
+ assert_equal [0], @list.indexes.delete('com')
+ assert @list.indexes.empty?
+ end
+
+
+ def test_equality_with_self
+ list = PublicSuffix::List.new
+ assert_equal list, list
+ end
+
+ def test_equality_with_internals
+ rule = PublicSuffix::Rule.factory("com")
+ assert_equal PublicSuffix::List.new.add(rule), PublicSuffix::List.new.add(rule)
+ end
+
+
+ def test_add
+ assert_equal @list, @list.add(PublicSuffix::Rule.factory(""))
+ assert_equal @list, @list << PublicSuffix::Rule.factory("")
+ assert_equal 2, @list.length
+ end
+
+ def test_add_should_recreate_index
+ @list = PublicSuffix::List.parse("com")
+ assert_equal PublicSuffix::Rule.factory("com"), @list.find("google.com")
+ assert_equal nil, @list.find("google.net")
+
+ @list << PublicSuffix::Rule.factory("net")
+ assert_equal PublicSuffix::Rule.factory("com"), @list.find("google.com")
+ assert_equal PublicSuffix::Rule.factory("net"), @list.find("google.net")
+ end
+
+ def test_empty?
+ assert @list.empty?
+ @list.add(PublicSuffix::Rule.factory(""))
+ assert !@list.empty?
+ end
+
+ def test_size
+ assert_equal 0, @list.length
+ assert_equal @list, @list.add(PublicSuffix::Rule.factory(""))
+ assert_equal 1, @list.length
+ end
+
+ def test_clear
+ assert_equal 0, @list.length
+ assert_equal @list, @list.add(PublicSuffix::Rule.factory(""))
+ assert_equal 1, @list.length
+ assert_equal @list, @list.clear
+ assert_equal 0, @list.length
+ end
+
+
+ def test_find
+ @list = PublicSuffix::List.parse(<<EOS)
+// com : http://en.wikipedia.org/wiki/.com
+com
+
+// uk : http://en.wikipedia.org/wiki/.uk
+*.uk
+*.sch.uk
+!bl.uk
+!british-library.uk
+EOS
+ assert_equal PublicSuffix::Rule.factory("com"), @list.find("google.com")
+ assert_equal PublicSuffix::Rule.factory("com"), @list.find("foo.google.com")
+ assert_equal PublicSuffix::Rule.factory("*.uk"), @list.find("google.uk")
+ assert_equal PublicSuffix::Rule.factory("*.uk"), @list.find("google.co.uk")
+ assert_equal PublicSuffix::Rule.factory("*.uk"), @list.find("foo.google.co.uk")
+ assert_equal PublicSuffix::Rule.factory("!british-library.uk"), @list.find("british-library.uk")
+ assert_equal PublicSuffix::Rule.factory("!british-library.uk"), @list.find("foo.british-library.uk")
+ end
+
+ def test_select
+ @list = PublicSuffix::List.parse(<<EOS)
+// com : http://en.wikipedia.org/wiki/.com
+com
+
+// uk : http://en.wikipedia.org/wiki/.uk
+*.uk
+*.sch.uk
+!bl.uk
+!british-library.uk
+EOS
+ assert_equal 2, @list.select("british-library.uk").size
+ end
+
+
+ def test_self_default_getter
+ assert_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
+ PublicSuffix::List.default
+ assert_not_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
+ end
+
+ def test_self_default_setter
+ PublicSuffix::List.default
+ assert_not_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
+ PublicSuffix::List.default = nil
+ assert_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
+ end
+
+ def test_self_clear
+ PublicSuffix::List.default
+ assert_not_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
+ PublicSuffix::List.clear
+ assert_equal nil, PublicSuffix::List.send(:class_variable_get, :"@@default")
+ end
+
+ def test_self_reload
+ PublicSuffix::List.default
+ PublicSuffix::List.expects(:default_definition).returns("")
+
+ PublicSuffix::List.reload
+ assert_equal PublicSuffix::List.new, PublicSuffix::List.default
+ end
+
+
+ def test_self_parse
+ list = PublicSuffix::List.parse(<<EOS)
+// ***** BEGIN LICENSE BLOCK *****
+// Version: MPL 1.1/GPL 2.0/LGPL 2.1
+//
+// ***** END LICENSE BLOCK *****
+
+// ac : http://en.wikipedia.org/wiki/.ac
+ac
+com.ac
+
+// ad : http://en.wikipedia.org/wiki/.ad
+ad
+
+// ar : http://en.wikipedia.org/wiki/.ar
+*.ar
+!congresodelalengua3.ar
+EOS
+
+ assert_instance_of PublicSuffix::List, list
+ assert_equal 5, list.length
+ assert_equal %w(ac com.ac ad *.ar !congresodelalengua3.ar).map { |name| PublicSuffix::Rule.factory(name) }, list.to_a
+ end
+
+ def test_self_parse_should_create_cache
+ list = PublicSuffix::List.parse(<<EOS)
+// com : http://en.wikipedia.org/wiki/.com
+com
+
+// uk : http://en.wikipedia.org/wiki/.uk
+*.uk
+*.sch.uk
+!bl.uk
+!british-library.uk
+EOS
+
+ assert_equal PublicSuffix::Rule.factory("com"), list.find("google.com")
+ end
+
+end
View
85 test/unit/public_suffix_test.rb
@@ -0,0 +1,85 @@
+require 'test_helper'
+
+class PublicSuffixTest < Test::Unit::TestCase
+
+ def test_self_parse_a_domain_with_tld_and_sld
+ domain = PublicSuffix.parse("example.com")
+ assert_instance_of PublicSuffix::Domain, domain
+ assert_equal "com", domain.tld
+ assert_equal "example", domain.sld
+ assert_equal nil, domain.trd
+
+ domain = PublicSuffix.parse("example.co.uk")
+ assert_instance_of PublicSuffix::Domain, domain
+ assert_equal "co.uk", domain.tld
+ assert_equal "example", domain.sld
+ assert_equal nil, domain.trd
+ end
+
+ def test_self_parse_a_domain_with_tld_and_sld_and_trd
+ domain = PublicSuffix.parse("alpha.example.com")
+ assert_instance_of PublicSuffix::Domain, domain
+ assert_equal "com", domain.tld
+ assert_equal "example", domain.sld
+ assert_equal "alpha", domain.trd
+
+ domain = PublicSuffix.parse("alpha.example.co.uk")
+ assert_instance_of PublicSuffix::Domain, domain
+ assert_equal "co.uk", domain.tld
+ assert_equal "example", domain.sld
+ assert_equal "alpha", domain.trd
+ end
+
+ def test_self_parse_a_domain_with_tld_and_sld_and_4rd
+ domain = PublicSuffix.parse("one.two.example.com")
+ assert_instance_of PublicSuffix::Domain, domain
+ assert_equal "com", domain.tld
+ assert_equal "example", domain.sld
+ assert_equal "one.two", domain.trd
+
+ domain = PublicSuffix.parse("one.two.example.co.uk")
+ assert_instance_of PublicSuffix::Domain, domain
+ assert_equal "co.uk", domain.tld
+ assert_equal "example", domain.sld
+ assert_equal "one.two", domain.trd
+ end
+
+ def test_self_parse_a_fully_qualified_domain_name
+ domain = PublicSuffix.parse("www.example.com.")
+ assert_instance_of PublicSuffix::Domain, domain
+ assert_equal "com", domain.tld
+ assert_equal "example", domain.sld
+ assert_equal "www", domain.trd
+ end
+
+ def test_self_parse_should_raise_with_invalid_domain
+ error = assert_raise(PublicSuffix::DomainInvalid) { PublicSuffix.parse("example.zip") }
+ assert_match %r{example\.zip}, error.message
+ end
+
+ def test_self_parse_should_raise_with_unallowed_domain
+ error = assert_raise(PublicSuffix::DomainNotAllowed) { PublicSuffix.parse("example.ke") }
+ assert_match %r{example\.ke}, error.message
+ end
+
+
+ def test_self_valid
+ assert PublicSuffix.valid?("google.com")
+ assert PublicSuffix.valid?("www.google.com")
+ assert PublicSuffix.valid?("google.co.uk")
+ assert PublicSuffix.valid?("www.google.co.uk")
+ end
+
+ # Returns false when domain has an invalid TLD
+ def test_self_valid_with_invalid_tld
+ assert !PublicSuffix.valid?("google.zip")
+ assert !PublicSuffix.valid?("www.google.zip")
+ end
+
+ def test_self_valid_with_fully_qualified_domain_name
+ assert PublicSuffix.valid?("google.com.")
+ assert PublicSuffix.valid?("google.co.uk.")
+ assert !PublicSuffix.valid?("google.zip.")
+ end
+
+end
View
44 test/public_suffix_service/rule_test.rb → test/unit/rule_test.rb
@@ -1,38 +1,38 @@
require 'test_helper'
-class PublicSuffixService::RuleTest < Test::Unit::TestCase
+class PublicSuffix::RuleTest < Test::Unit::TestCase
def test_factory_should_return_rule_normal
- rule = PublicSuffixService::Rule.factory("com")
- assert_instance_of PublicSuffixService::Rule::Normal, rule
+ rule = PublicSuffix::Rule.factory("com")
+ assert_instance_of PublicSuffix::Rule::Normal, rule
- rule = PublicSuffixService::Rule.factory("verona.it")
- assert_instance_of PublicSuffixService::Rule::Normal, rule
+ rule = PublicSuffix::Rule.factory("verona.it")
+ assert_instance_of PublicSuffix::Rule::Normal, rule
end
def test_factory_should_return_rule_exception
- rule = PublicSuffixService::Rule.factory("!british-library.uk")
- assert_instance_of PublicSuffixService::Rule::Exception, rule
+ rule = PublicSuffix::Rule.factory("!british-library.uk")
+ assert_instance_of PublicSuffix::Rule::Exception, rule
end
def test_factory_should_return_rule_wildcard
- rule = PublicSuffixService::Rule.factory("*.do")
- assert_instance_of PublicSuffixService::Rule::Wildcard, rule
+ rule = PublicSuffix::Rule.factory("*.do")
+ assert_instance_of PublicSuffix::Rule::Wildcard, rule
- rule = PublicSuffixService::Rule.factory("*.sch.uk")
- assert_instance_of PublicSuffixService::Rule::Wildcard, rule
+ rule = PublicSuffix::Rule.factory("*.sch.uk")
+ assert_instance_of PublicSuffix::Rule::Wildcard, rule
end
end
-class PublicSuffixService::RuleBaseTest < Test::Unit::TestCase
+class PublicSuffix::RuleBaseTest < Test::Unit::TestCase
- class ::PublicSuffixService::Rule::Test < ::PublicSuffixService::Rule::Base
+ class ::PublicSuffix::Rule::Test < ::PublicSuffix::Rule::Base
end
def setup
- @klass = PublicSuffixService::Rule::Base
+ @klass = PublicSuffix::Rule::Base
end
@@ -47,14 +47,14 @@ def test_initialize
end
def test_equality_with_self
- rule = PublicSuffixService::Rule::Base.new("foo")
+ rule = PublicSuffix::Rule::Base.new("foo")
assert_equal rule, rule
end
def test_equality_with_internals
assert_equal @klass.new("foo"), @klass.new("foo")
assert_not_equal @klass.new("foo"), @klass.new("bar")
- assert_not_equal @klass.new("foo"), PublicSuffixService::Rule::Test.new("bar")
+ assert_not_equal @klass.new("foo"), PublicSuffix::Rule::Test.new("bar")
assert_not_equal @klass.new("foo"), Class.new { def name; foo; end }.new
end
@@ -88,10 +88,10 @@ def test_decompose
end
-class PublicSuffixService::RuleNormalTest < Test::Unit::TestCase
+class PublicSuffix::RuleNormalTest < Test::Unit::TestCase
def setup
- @klass = PublicSuffixService::Rule::Normal
+ @klass = PublicSuffix::Rule::Normal
end
@@ -165,10 +165,10 @@ def test_decompose_with_fully_qualified_domain_name
end
-class PublicSuffixService::RuleExceptionTest < Test::Unit::TestCase
+class PublicSuffix::RuleExceptionTest < Test::Unit::TestCase
def setup
- @klass = PublicSuffixService::Rule::Exception
+ @klass = PublicSuffix::Rule::Exception
end
@@ -236,10 +236,10 @@ def test_decompose_with_fully_qualified_domain_name
end
-class PublicSuffixService::RuleWildcardTest < Test::Unit::TestCase
+class PublicSuffix::RuleWildcardTest < Test::Unit::TestCase
def setup
- @klass = PublicSuffixService::Rule::Wildcard
+ @klass = PublicSuffix::Rule::Wildcard
end

0 comments on commit de70d78

Please sign in to comment.