public-suffix-list 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -21,7 +21,25 @@ easier to use.
21
21
 
22
22
  == Synopsis
23
23
 
24
- PublicSuffixList.new.tld("foobar.com") # downloads the latest data file and returns "com"
24
+ require "public_suffix_list"
25
+
26
+ # downloads and parses the latest data file and returns "com"
27
+ PublicSuffixList.new.tld("foobar.com")
28
+
29
+ # downloads and parses the latest data file and returns "foobar.com"
30
+ PublicSuffixList.new.cdn("foobar.com")
31
+
32
+ # downloads and parses the latest data file, caches it in /tmp, returns ["abc", "xyz", "co.uk"]
33
+ PublicSuffixList.new(:cache_dir => "/tmp").split("abc.xyz.co.uk")
34
+
35
+ # loads the cached data in /tmp and returns ["test", "nhs", "uk"]
36
+ PublicSuffixList.new(:cache_dir => "/tmp").split("test.nhs.uk")
37
+
38
+ # you don't have to instantiate PublicSuffixList every time you use it, of course...
39
+ p = PublicSuffixList.new
40
+ p.split("fee.fi.fo.com") # => ["fee.fi", "fo", "com"]
41
+ p.cdn("fee.fi.fo.com") # => "fo.com", "cdn" is "canonical domain name"
42
+ p.tld("fee.fi.fo.com") # => "com", "tld" is "top-level domain"
25
43
 
26
44
  == Requirements
27
45
 
@@ -5,7 +5,7 @@ require "public_suffix_list/parser.rb"
5
5
 
6
6
  class PublicSuffixList
7
7
 
8
- VERSION = "0.0.1"
8
+ VERSION = "0.0.2"
9
9
 
10
10
  def self.config
11
11
  @@config ||= Config.new
@@ -17,34 +17,73 @@ class PublicSuffixList
17
17
 
18
18
  class Config
19
19
 
20
+ attr_accessor :cache_dir
21
+ attr_accessor :cache_expiry_period
20
22
  attr_accessor :effective_tld_names_url
21
23
 
22
24
  def initialize
25
+ @cache_dir = nil
26
+ @cache_expiry_period = 30 * 24 * 60 * 60
23
27
  @effective_tld_names_url = "http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/src/effective_tld_names.dat?raw=1"
24
28
  end
25
29
 
26
30
  end
27
31
 
28
- def initialize(url = self.class.config.effective_tld_names_url)
29
- @rules = Parser.parse(open(url))
32
+ def initialize(options = {})
33
+ @config = self.class.config.dup
34
+ options.each { |k, v| @config.send("#{k}=", v) }
35
+ if @config.cache_dir && File.directory?(@config.cache_dir) && File.exist?(File.join(@config.cache_dir, name))
36
+ uncache or (download and cache)
37
+ elsif @config.cache_dir && File.directory?(@config.cache_dir)
38
+ download and cache
39
+ else
40
+ download
41
+ end
30
42
  end
31
43
 
32
44
  def split(domain)
33
45
  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('.')]
46
+ result = best(match(domain, @rules))
47
+ [gimme!(domain, result.size), gimme!(domain), domain].reverse.map { |d| d ? d.join(".") : "" }
36
48
  end
37
49
 
38
50
  def tld(domain)
39
- best(match(domain.split("."), @rules)).reverse.join(".")
51
+ domain = domain.split(".")
52
+ result = best(match(domain, @rules))
53
+ gimme!(domain, result.size).join(".")
54
+ end
55
+
56
+ def cdn(domain)
57
+ domain = domain.split(".")
58
+ result = best(match(domain, @rules))
59
+ gimme!(domain, result.size + 1).join(".")
40
60
  end
41
61
 
42
62
  private
43
63
 
64
+ def name
65
+ URI.parse(@config.effective_tld_names_url).path.split("/").last + ".cache"
66
+ end
67
+
68
+ def cache
69
+ @cache = {:rules => @rules, :created_at => Time.now}
70
+ open(File.join(@config.cache_dir, name), "w") { |f| Marshal.dump(@cache, f) }
71
+ end
72
+
73
+ def uncache
74
+ open(File.join(@config.cache_dir, name), "r") { |f| @cache = Marshal.load(f) }
75
+ @rules = @cache[:rules] if Time.now < @cache[:created_at] + @config.cache_expiry_period
76
+ end
77
+
78
+ def download
79
+ @rules = Parser.parse(open(@config.effective_tld_names_url))
80
+ end
81
+
44
82
  def match(domain, rules)
45
83
  return [] if domain.empty? or rules.empty?
46
- set = []
84
+ domain = domain.dup
47
85
  first = domain.pop
86
+ set = []
48
87
  [[first, first], ["!#{first}", "!#{first}"], ["*", first]].each do |a, b|
49
88
  if rules[a]
50
89
  set << [b]
@@ -61,4 +100,8 @@ class PublicSuffixList
61
100
  result
62
101
  end
63
102
 
103
+ def gimme!(domain, n = 1)
104
+ domain.slice!(-n, n)
105
+ end
106
+
64
107
  end
@@ -2,12 +2,13 @@ require 'lib/public_suffix_list'
2
2
 
3
3
  def cookie_calculation(public_suffix_list, domain)
4
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}."
5
+ (s.empty? and !d.empty?) ? "Cookies may be set for #{domain}." : "Cookies may not be set for #{domain}."
6
6
  end
7
7
 
8
8
  describe PublicSuffixList do
9
+
9
10
  it "should act as described at http://publicsuffix.org/format/" do
10
- public_suffix_list = PublicSuffixList.new("spec/test.dat")
11
+ public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat")
11
12
  cookie_calculation(public_suffix_list, "foo.com").should == "Cookies may be set for foo.com."
12
13
  cookie_calculation(public_suffix_list, "foo.bar.jp").should == "Cookies may be set for foo.bar.jp."
13
14
  cookie_calculation(public_suffix_list, "bar.jp").should == "Cookies may not be set for bar.jp."
@@ -18,4 +19,11 @@ describe PublicSuffixList do
18
19
  cookie_calculation(public_suffix_list, "pref.hokkaido.jp").should == "Cookies may be set for pref.hokkaido.jp."
19
20
  cookie_calculation(public_suffix_list, "metro.tokyo.jp").should == "Cookies may be set for metro.tokyo.jp."
20
21
  end
22
+
23
+ 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"
27
+ end
28
+
21
29
  end
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.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Todd Sundsted