public-suffix-list 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/lib/public_suffix_list.rb +6 -6
- data/lib/public_suffix_list/parser.rb +22 -5
- data/spec/caching.spec +21 -0
- data/spec/cookie_calculation.spec +23 -0
- data/spec/public_suffix_list.spec +20 -19
- data/spec/test.dat +7 -0
- metadata +4 -2
data/README.rdoc
CHANGED
data/lib/public_suffix_list.rb
CHANGED
@@ -5,7 +5,7 @@ require "public_suffix_list/parser.rb"
|
|
5
5
|
|
6
6
|
class PublicSuffixList
|
7
7
|
|
8
|
-
VERSION = "0.0.
|
8
|
+
VERSION = "0.0.3"
|
9
9
|
|
10
10
|
def self.config
|
11
11
|
@@config ||= Config.new
|
@@ -84,10 +84,10 @@ class PublicSuffixList
|
|
84
84
|
domain = domain.dup
|
85
85
|
first = domain.pop
|
86
86
|
set = []
|
87
|
-
[[first, first], ["!#{first}", "!#{first}"], ["*", first]].each do |
|
88
|
-
if rules[
|
89
|
-
set << [
|
90
|
-
match(domain, rules[
|
87
|
+
[[first, first], ["!#{first}", "!#{first}"], ["*", first]].each do |pattern, name|
|
88
|
+
if rules[pattern]
|
89
|
+
set << [name] if rules[pattern][:term]
|
90
|
+
match(domain, rules[pattern]).each { |result| set << [first] + result }
|
91
91
|
end
|
92
92
|
end
|
93
93
|
set
|
@@ -101,7 +101,7 @@ class PublicSuffixList
|
|
101
101
|
end
|
102
102
|
|
103
103
|
def gimme!(domain, n = 1)
|
104
|
-
domain.slice!(-n, n)
|
104
|
+
domain.slice!(-n, n) || []
|
105
105
|
end
|
106
106
|
|
107
107
|
end
|
@@ -2,17 +2,34 @@ class PublicSuffixList
|
|
2
2
|
|
3
3
|
module Parser
|
4
4
|
|
5
|
+
# com
|
6
|
+
# *.jp
|
7
|
+
# *.hokkaido.jp
|
8
|
+
# *.tokyo.jp
|
9
|
+
# !pref.hokkaido.jp
|
10
|
+
# !metro.tokyo.jp
|
11
|
+
|
12
|
+
# {
|
13
|
+
# "com" => {:term => true},
|
14
|
+
# "jp" => {
|
15
|
+
# "tokyo" => {"!metro" => {:term => true}, "*" => {:term => true}},
|
16
|
+
# "hokkaido" => {"!pref" => {:term => true}, "*" => {:term => true}},
|
17
|
+
# "*" => {:term => true}
|
18
|
+
# }
|
19
|
+
# }
|
20
|
+
|
5
21
|
def self.parse(lines)
|
6
|
-
lines.inject({}) do |
|
22
|
+
lines.inject({}) do |acc, line|
|
7
23
|
line.strip!
|
8
24
|
unless line =~ %r{//} or line.empty?
|
9
|
-
|
25
|
+
tmp = acc
|
10
26
|
line.split(".").reverse.each do |p|
|
11
|
-
|
12
|
-
|
27
|
+
tmp[p] = {} unless tmp[p]
|
28
|
+
tmp = tmp[p]
|
13
29
|
end
|
30
|
+
tmp[:term] = true
|
14
31
|
end
|
15
|
-
|
32
|
+
acc
|
16
33
|
end
|
17
34
|
end
|
18
35
|
|
data/spec/caching.spec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'tmpdir'
|
2
|
+
require 'lib/public_suffix_list'
|
3
|
+
|
4
|
+
options = {
|
5
|
+
:cache_dir => Dir.tmpdir,
|
6
|
+
:cache_expiry_period => 10,
|
7
|
+
:effective_tld_names_url => "spec/test.dat"
|
8
|
+
}
|
9
|
+
|
10
|
+
describe PublicSuffixList do
|
11
|
+
|
12
|
+
before do
|
13
|
+
File.delete(File.join(Dir.tmpdir, "test.dat.cache")) if File.exist?(File.join(Dir.tmpdir, "test.dat.cache"))
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should cache when instructed to" do
|
17
|
+
public_suffix_list = PublicSuffixList.new(options)
|
18
|
+
File.exist?(File.join(Dir.tmpdir, "test.dat.cache")).should be true
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,23 @@
|
|
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? && !d.empty? ? "Cookies may be set for #{domain}." : "Cookies may not be set for #{domain}."
|
6
|
+
end
|
7
|
+
|
8
|
+
describe PublicSuffixList do
|
9
|
+
|
10
|
+
it "should calculate cookies as described at http://publicsuffix.org/format/" do
|
11
|
+
public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat")
|
12
|
+
cookie_calculation(public_suffix_list, "foo.com").should == "Cookies may be set for foo.com."
|
13
|
+
cookie_calculation(public_suffix_list, "foo.bar.jp").should == "Cookies may be set for foo.bar.jp."
|
14
|
+
cookie_calculation(public_suffix_list, "bar.jp").should == "Cookies may not be set for bar.jp."
|
15
|
+
cookie_calculation(public_suffix_list, "foo.bar.hokkaido.jp").should == "Cookies may be set for foo.bar.hokkaido.jp."
|
16
|
+
cookie_calculation(public_suffix_list, "bar.hokkaido.jp").should == "Cookies may not be set for bar.hokkaido.jp."
|
17
|
+
cookie_calculation(public_suffix_list, "foo.bar.tokyo.jp").should == "Cookies may be set for foo.bar.tokyo.jp."
|
18
|
+
cookie_calculation(public_suffix_list, "bar.tokyo.jp").should == "Cookies may not be set for bar.tokyo.jp."
|
19
|
+
cookie_calculation(public_suffix_list, "pref.hokkaido.jp").should == "Cookies may be set for pref.hokkaido.jp."
|
20
|
+
cookie_calculation(public_suffix_list, "metro.tokyo.jp").should == "Cookies may be set for metro.tokyo.jp."
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -1,29 +1,30 @@
|
|
1
1
|
require 'lib/public_suffix_list'
|
2
2
|
|
3
|
-
def cookie_calculation(public_suffix_list, domain)
|
4
|
-
s, d, t = public_suffix_list.split(domain)
|
5
|
-
(s.empty? and !d.empty?) ? "Cookies may be set for #{domain}." : "Cookies may not be set for #{domain}."
|
6
|
-
end
|
7
|
-
|
8
3
|
describe PublicSuffixList do
|
9
4
|
|
10
|
-
|
11
|
-
public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat")
|
12
|
-
cookie_calculation(public_suffix_list, "foo.com").should == "Cookies may be set for foo.com."
|
13
|
-
cookie_calculation(public_suffix_list, "foo.bar.jp").should == "Cookies may be set for foo.bar.jp."
|
14
|
-
cookie_calculation(public_suffix_list, "bar.jp").should == "Cookies may not be set for bar.jp."
|
15
|
-
cookie_calculation(public_suffix_list, "foo.bar.hokkaido.jp").should == "Cookies may be set for foo.bar.hokkaido.jp."
|
16
|
-
cookie_calculation(public_suffix_list, "bar.hokkaido.jp").should == "Cookies may not be set for bar.hokkaido.jp."
|
17
|
-
cookie_calculation(public_suffix_list, "foo.bar.tokyo.jp").should == "Cookies may be set for foo.bar.tokyo.jp."
|
18
|
-
cookie_calculation(public_suffix_list, "bar.tokyo.jp").should == "Cookies may not be set for bar.tokyo.jp."
|
19
|
-
cookie_calculation(public_suffix_list, "pref.hokkaido.jp").should == "Cookies may be set for pref.hokkaido.jp."
|
20
|
-
cookie_calculation(public_suffix_list, "metro.tokyo.jp").should == "Cookies may be set for metro.tokyo.jp."
|
5
|
+
before do
|
6
|
+
@public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat")
|
21
7
|
end
|
22
8
|
|
23
9
|
it "should calculate tld and cdn correctly" do
|
24
|
-
public_suffix_list
|
25
|
-
public_suffix_list.
|
26
|
-
public_suffix_list.
|
10
|
+
@public_suffix_list.cdn("foo.bar.com").should == "bar.com"
|
11
|
+
@public_suffix_list.tld("foo.bar.com").should == "com"
|
12
|
+
@public_suffix_list.cdn("foobar.com").should == "foobar.com"
|
13
|
+
@public_suffix_list.tld("foobar.com").should == "com"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should not assume the root name is a top level domain" do
|
17
|
+
@public_suffix_list.tld("com").should == "com"
|
18
|
+
@public_suffix_list.tld("bar.foo").should == "bar.foo"
|
19
|
+
@public_suffix_list.tld("baz.foo").should == "baz.foo"
|
20
|
+
@public_suffix_list.tld("qux.foo").should == ""
|
21
|
+
@public_suffix_list.tld("foo").should == ""
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should handle edge cases correctly" do
|
25
|
+
@public_suffix_list.split("").should == ["", "", ""]
|
26
|
+
@public_suffix_list.cdn("").should == ""
|
27
|
+
@public_suffix_list.tld("").should == ""
|
27
28
|
end
|
28
29
|
|
29
30
|
end
|
data/spec/test.dat
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: public-suffix-list
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Todd Sundsted
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-10 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -25,6 +25,8 @@ files:
|
|
25
25
|
- README.rdoc
|
26
26
|
- lib/public_suffix_list.rb
|
27
27
|
- lib/public_suffix_list/parser.rb
|
28
|
+
- spec/caching.spec
|
29
|
+
- spec/cookie_calculation.spec
|
28
30
|
- spec/public_suffix_list.spec
|
29
31
|
- spec/test.dat
|
30
32
|
has_rdoc: true
|