public-suffix-list 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,55 @@
1
+ = Public Suffix List
2
+
3
+ * http://github.com/toddsundsted/Public-Suffix-List
4
+
5
+ == Description
6
+
7
+ The Public Suffix List (http://publicsuffix.org/) is "a cross-vendor
8
+ initiative to provide an accurate list of domain name suffixes". Such
9
+ a list is necessary because "there was and remains no algorithmic
10
+ method of finding the highest level at which a domain may be
11
+ registered for a particular top-level domain (the policies differ with
12
+ each registry)...". Public Suffix List is also a small Ruby library
13
+ designed to make the Public Suffix List (http://publicsuffix.org/)
14
+ easier to use.
15
+
16
+ == Features
17
+
18
+ * Transparent download of the TLD data file
19
+ * Optional caching of parsed data
20
+ * Tiny API
21
+
22
+ == Synopsis
23
+
24
+ PublicSuffixList.new.tld("foobar.com") # downloads the latest data file and returns "com"
25
+
26
+ == Requirements
27
+
28
+ None that I am aware of.
29
+
30
+ == Install
31
+
32
+ sudo gem install public-suffix-list
33
+
34
+ == License
35
+
36
+ Copyright (c) 2010 Todd Sundsted
37
+
38
+ Permission is hereby granted, free of charge, to any person obtaining
39
+ a copy of this software and associated documentation files (the
40
+ 'Software'), to deal in the Software without restriction, including
41
+ without limitation the rights to use, copy, modify, merge, publish,
42
+ distribute, sublicense, and/or sell copies of the Software, and to
43
+ permit persons to whom the Software is furnished to do so, subject to
44
+ the following conditions:
45
+
46
+ The above copyright notice and this permission notice shall be
47
+ included in all copies or substantial portions of the Software.
48
+
49
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
50
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
51
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
52
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
53
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
54
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
55
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__))
2
+
3
+ require 'open-uri'
4
+ require "public_suffix_list/parser.rb"
5
+
6
+ class PublicSuffixList
7
+
8
+ VERSION = "0.0.1"
9
+
10
+ def self.config
11
+ @@config ||= Config.new
12
+ end
13
+
14
+ def self.configure(&block)
15
+ yield config
16
+ end
17
+
18
+ class Config
19
+
20
+ attr_accessor :effective_tld_names_url
21
+
22
+ def initialize
23
+ @effective_tld_names_url = "http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/src/effective_tld_names.dat?raw=1"
24
+ end
25
+
26
+ end
27
+
28
+ def initialize(url = self.class.config.effective_tld_names_url)
29
+ @rules = Parser.parse(open(url))
30
+ end
31
+
32
+ def split(domain)
33
+ domain = domain.split(".")
34
+ result = best(match(domain.dup, @rules))
35
+ [domain.dup.reverse.drop(result.size + 1).reverse.join("."), domain.dup.reverse.drop(result.size).first, result.reverse.join('.')]
36
+ end
37
+
38
+ def tld(domain)
39
+ best(match(domain.split("."), @rules)).reverse.join(".")
40
+ end
41
+
42
+ private
43
+
44
+ def match(domain, rules)
45
+ return [] if domain.empty? or rules.empty?
46
+ set = []
47
+ first = domain.pop
48
+ [[first, first], ["!#{first}", "!#{first}"], ["*", first]].each do |a, b|
49
+ if rules[a]
50
+ set << [b]
51
+ match(domain, rules[a]).each { |result| set << [first] + result }
52
+ end
53
+ end
54
+ set
55
+ end
56
+
57
+ def best(results)
58
+ return [] if results.empty?
59
+ result = results.find { |r| r.last[0] == ?! } || results.sort { |a, b| a.size <=> b.size }.last
60
+ result = result[0..result.size - 2] if result.last[0] == ?!
61
+ result
62
+ end
63
+
64
+ end
@@ -0,0 +1,21 @@
1
+ class PublicSuffixList
2
+
3
+ module Parser
4
+
5
+ def self.parse(lines)
6
+ lines.inject({}) do |a, line|
7
+ line.strip!
8
+ unless line =~ %r{//} or line.empty?
9
+ t = a
10
+ line.split(".").reverse.each do |p|
11
+ t[p] = {} unless t[p]
12
+ t = t[p]
13
+ end
14
+ end
15
+ a
16
+ end
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,21 @@
1
+ require 'lib/public_suffix_list'
2
+
3
+ def cookie_calculation(public_suffix_list, domain)
4
+ s, d, t = public_suffix_list.split(domain)
5
+ s.empty? and d ? "Cookies may be set for #{domain}." : "Cookies may not be set for #{domain}."
6
+ end
7
+
8
+ describe PublicSuffixList do
9
+ it "should act as described at http://publicsuffix.org/format/" do
10
+ public_suffix_list = PublicSuffixList.new("spec/test.dat")
11
+ cookie_calculation(public_suffix_list, "foo.com").should == "Cookies may be set for foo.com."
12
+ cookie_calculation(public_suffix_list, "foo.bar.jp").should == "Cookies may be set for foo.bar.jp."
13
+ cookie_calculation(public_suffix_list, "bar.jp").should == "Cookies may not be set for bar.jp."
14
+ cookie_calculation(public_suffix_list, "foo.bar.hokkaido.jp").should == "Cookies may be set for foo.bar.hokkaido.jp."
15
+ cookie_calculation(public_suffix_list, "bar.hokkaido.jp").should == "Cookies may not be set for bar.hokkaido.jp."
16
+ cookie_calculation(public_suffix_list, "foo.bar.tokyo.jp").should == "Cookies may be set for foo.bar.tokyo.jp."
17
+ cookie_calculation(public_suffix_list, "bar.tokyo.jp").should == "Cookies may not be set for bar.tokyo.jp."
18
+ cookie_calculation(public_suffix_list, "pref.hokkaido.jp").should == "Cookies may be set for pref.hokkaido.jp."
19
+ cookie_calculation(public_suffix_list, "metro.tokyo.jp").should == "Cookies may be set for metro.tokyo.jp."
20
+ end
21
+ end
@@ -0,0 +1,6 @@
1
+ com
2
+ *.jp
3
+ *.hokkaido.jp
4
+ *.tokyo.jp
5
+ !pref.hokkaido.jp
6
+ !metro.tokyo.jp
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: public-suffix-list
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Todd Sundsted
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-09 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: The Public Suffix List (http://publicsuffix.org/) is "a cross-vendor initiative to provide an accurate list of domain name suffixes". Such a list is necessary because "there was and remains no algorithmic method of finding the highest level at which a domain may be registered for a particular top-level domain (the policies differ with each registry)...". Public Suffix List is also a small Ruby library designed to make the Public Suffix List (http://publicsuffix.org/) easier to use.
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.rdoc
26
+ - lib/public_suffix_list.rb
27
+ - lib/public_suffix_list/parser.rb
28
+ - spec/public_suffix_list.spec
29
+ - spec/test.dat
30
+ has_rdoc: true
31
+ homepage: http://github.com/toddsundsted/Public-Suffix-List
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Public Suffix List is a small Ruby library designed to make the Public Suffix List (http://publicsuffix.org/) easier to use.
58
+ test_files: []
59
+