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 CHANGED
@@ -16,7 +16,7 @@ easier to use.
16
16
  == Features
17
17
 
18
18
  * Transparent download of the TLD data file
19
- * Optional caching of parsed data
19
+ * Optional caching of optimized, parsed TLD data
20
20
  * Tiny API
21
21
 
22
22
  == Synopsis
@@ -5,7 +5,7 @@ require "public_suffix_list/parser.rb"
5
5
 
6
6
  class PublicSuffixList
7
7
 
8
- VERSION = "0.0.2"
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 |a, b|
88
- if rules[a]
89
- set << [b]
90
- match(domain, rules[a]).each { |result| set << [first] + result }
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 |a, line|
22
+ lines.inject({}) do |acc, line|
7
23
  line.strip!
8
24
  unless line =~ %r{//} or line.empty?
9
- t = a
25
+ tmp = acc
10
26
  line.split(".").reverse.each do |p|
11
- t[p] = {} unless t[p]
12
- t = t[p]
27
+ tmp[p] = {} unless tmp[p]
28
+ tmp = tmp[p]
13
29
  end
30
+ tmp[:term] = true
14
31
  end
15
- a
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
- it "should act 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."
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 = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat")
25
- public_suffix_list.cdn("foo.bar.com").should == "bar.com"
26
- public_suffix_list.tld("foo.bar.com").should == "com"
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
@@ -1,6 +1,13 @@
1
+ // data for cookie calculation, http://publicsuffix.org/format/
1
2
  com
2
3
  *.jp
3
4
  *.hokkaido.jp
4
5
  *.tokyo.jp
5
6
  !pref.hokkaido.jp
6
7
  !metro.tokyo.jp
8
+
9
+ // "foo" and "qux.foo" are not tlds
10
+ bar.foo
11
+ baz.foo
12
+ one.qux.foo
13
+ two.qux.foo
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.2
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-09 00:00:00 -05:00
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