public-suffix-list 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,12 @@
1
1
  $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__))
2
2
 
3
3
  require 'open-uri'
4
+ require "public_suffix_list/cache_file.rb"
4
5
  require "public_suffix_list/parser.rb"
5
6
 
6
7
  class PublicSuffixList
7
8
 
8
- VERSION = "0.0.3"
9
+ VERSION = "0.0.4"
9
10
 
10
11
  def self.config
11
12
  @@config ||= Config.new
@@ -29,9 +30,14 @@ class PublicSuffixList
29
30
 
30
31
  end
31
32
 
33
+ attr_reader :config, :cache_file
34
+
32
35
  def initialize(options = {})
33
36
  @config = self.class.config.dup
34
37
  options.each { |k, v| @config.send("#{k}=", v) }
38
+ if @config.cache_dir
39
+ @cache_file = CacheFile.new(@config)
40
+ end
35
41
  if @config.cache_dir && File.directory?(@config.cache_dir) && File.exist?(File.join(@config.cache_dir, name))
36
42
  uncache or (download and cache)
37
43
  elsif @config.cache_dir && File.directory?(@config.cache_dir)
@@ -66,13 +72,17 @@ class PublicSuffixList
66
72
  end
67
73
 
68
74
  def cache
69
- @cache = {:rules => @rules, :created_at => Time.now}
75
+ @cache = {:rules => @rules, :created_at => Time.now, :tag => rand(36**8).to_s(36)}
70
76
  open(File.join(@config.cache_dir, name), "w") { |f| Marshal.dump(@cache, f) }
71
77
  end
72
78
 
73
79
  def uncache
74
80
  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
81
+ @rules = @cache[:rules] unless expired?
82
+ end
83
+
84
+ def expired?
85
+ !(@config.cache_expiry_period.nil? or @config.cache_expiry_period == 0 or Time.now < @cache[:created_at] + @config.cache_expiry_period)
76
86
  end
77
87
 
78
88
  def download
@@ -0,0 +1,59 @@
1
+ class PublicSuffixList
2
+
3
+ class CacheFile
4
+
5
+ def initialize(config)
6
+ @config = config
7
+ end
8
+
9
+ def cache?
10
+ @config.cache_dir && true
11
+ end
12
+
13
+ def name
14
+ URI.parse(@config.effective_tld_names_url).path.split("/").last + ".cache" if cache?
15
+ end
16
+
17
+ def file
18
+ File.join(@config.cache_dir, name) if cache?
19
+ end
20
+
21
+ def exist?
22
+ File.exist?(file) if cache?
23
+ end
24
+
25
+ def delete
26
+ File.delete(file) if exist?
27
+ end
28
+
29
+ def load_data
30
+ open(file, "r") { |f| @data = Marshal.load(f) } if exist?
31
+ end
32
+
33
+ def dump_data
34
+ open(file, "w") { |f| Marshal.dump(@data, f) } if cache?
35
+ end
36
+
37
+ def data
38
+ @data or (load_data and @data) or @data = {:created_at => Time.now}
39
+ end
40
+
41
+ def data=(data)
42
+ @data = data and dump_data
43
+ end
44
+
45
+ def expired?
46
+ !cache? or !([0, nil].include?(@config.cache_expiry_period) or data[:created_at] + @config.cache_expiry_period > Time.now)
47
+ end
48
+
49
+ def read_attribute(attribute)
50
+ data[attribute]
51
+ end
52
+
53
+ def write_attribute(attribute, value)
54
+ data[attribute] = value
55
+ end
56
+
57
+ end
58
+
59
+ end
data/spec/caching.spec CHANGED
@@ -1,21 +1,59 @@
1
1
  require 'tmpdir'
2
2
  require 'lib/public_suffix_list'
3
3
 
4
- options = {
5
- :cache_dir => Dir.tmpdir,
6
- :cache_expiry_period => 10,
7
- :effective_tld_names_url => "spec/test.dat"
8
- }
9
-
10
4
  describe PublicSuffixList do
11
5
 
6
+ FILE = File.join(Dir.tmpdir, "test.dat.cache")
7
+
12
8
  before do
13
- File.delete(File.join(Dir.tmpdir, "test.dat.cache")) if File.exist?(File.join(Dir.tmpdir, "test.dat.cache"))
9
+ File.delete(FILE) if File.exist?(FILE)
10
+ end
11
+
12
+ it "should cache when instructed to do so" do
13
+ File.exist?(FILE).should be false
14
+ public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat", :cache_dir => Dir.tmpdir, :cache_expiry_period => 10)
15
+ public_suffix_list.cache_file.cache?.should be true
16
+ public_suffix_list.cache_file.exist?.should be true
17
+ created_at = public_suffix_list.cache_file.read_attribute(:created_at)
18
+ tag = public_suffix_list.cache_file.read_attribute(:tag)
19
+ public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat", :cache_dir => Dir.tmpdir, :cache_expiry_period => 10)
20
+ public_suffix_list.cache_file.cache?.should be true
21
+ public_suffix_list.cache_file.exist?.should be true
22
+ public_suffix_list.cache_file.read_attribute(:created_at).should == created_at
23
+ public_suffix_list.cache_file.read_attribute(:tag).should == tag
24
+ public_suffix_list.cache_file.write_attribute :created_at, Time.now - 100
25
+ public_suffix_list.cache_file.dump_data
26
+ public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat", :cache_dir => Dir.tmpdir, :cache_expiry_period => 10)
27
+ public_suffix_list.cache_file.cache?.should be true
28
+ public_suffix_list.cache_file.exist?.should be true
29
+ public_suffix_list.cache_file.read_attribute(:created_at).should_not == created_at
30
+ public_suffix_list.cache_file.read_attribute(:tag).should_not == tag
14
31
  end
15
32
 
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
33
+ it "should allow 0 or nil to specify an infinite cache expiry period" do
34
+ File.exist?(FILE).should be false
35
+ public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat", :cache_dir => Dir.tmpdir, :cache_expiry_period => 10)
36
+ public_suffix_list.cache_file.exist?.should be true
37
+ public_suffix_list.cache_file.expired?.should be false
38
+ public_suffix_list.cache_file.write_attribute :created_at, Time.now - 100
39
+ public_suffix_list.cache_file.expired?.should be true
40
+ public_suffix_list.cache_file.exist?.should be true
41
+ public_suffix_list.cache_file.delete
42
+ public_suffix_list.cache_file.exist?.should be false
43
+ public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat", :cache_dir => Dir.tmpdir, :cache_expiry_period => 0)
44
+ public_suffix_list.cache_file.exist?.should be true
45
+ public_suffix_list.cache_file.expired?.should be false
46
+ public_suffix_list.cache_file.write_attribute :created_at, Time.now - 10000000
47
+ public_suffix_list.cache_file.expired?.should be false
48
+ public_suffix_list.cache_file.exist?.should be true
49
+ public_suffix_list.cache_file.delete
50
+ public_suffix_list.cache_file.exist?.should be false
51
+ public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat", :cache_dir => Dir.tmpdir, :cache_expiry_period => nil)
52
+ public_suffix_list.cache_file.exist?.should be true
53
+ public_suffix_list.cache_file.expired?.should be false
54
+ public_suffix_list.cache_file.write_attribute :created_at, Time.now - 10000000
55
+ public_suffix_list.cache_file.expired?.should be false
56
+ public_suffix_list.cache_file.exist?.should be true
19
57
  end
20
58
 
21
59
  end
@@ -4,6 +4,7 @@ describe PublicSuffixList do
4
4
 
5
5
  before do
6
6
  @public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat")
7
+ @public_suffix_list.cache_file.should be nil
7
8
  end
8
9
 
9
10
  it "should calculate tld and cdn correctly" do
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.3
4
+ version: 0.0.4
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-10 00:00:00 -05:00
12
+ date: 2010-01-11 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -25,6 +25,7 @@ files:
25
25
  - README.rdoc
26
26
  - lib/public_suffix_list.rb
27
27
  - lib/public_suffix_list/parser.rb
28
+ - lib/public_suffix_list/cache_file.rb
28
29
  - spec/caching.spec
29
30
  - spec/cookie_calculation.spec
30
31
  - spec/public_suffix_list.spec