public-suffix-list 0.0.5 → 0.0.6

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.
@@ -6,7 +6,7 @@ require "public_suffix_list/parser.rb"
6
6
 
7
7
  class PublicSuffixList
8
8
 
9
- VERSION = "0.0.5"
9
+ VERSION = "0.0.6"
10
10
 
11
11
  def self.config
12
12
  @@config ||= Config.new
@@ -39,54 +39,48 @@ class PublicSuffixList
39
39
  @cache_file = CacheFile.new(@config)
40
40
  end
41
41
  if @cache_file and @cache_file.exist?
42
- uncache or (download and cache)
42
+ uncache or (fetch and cache)
43
43
  elsif @cache_file
44
- download and cache
44
+ fetch and cache
45
45
  else
46
- download
46
+ fetch
47
47
  end
48
48
  end
49
49
 
50
+ def rules
51
+ @rules
52
+ end
53
+
50
54
  def split(domain)
51
55
  domain = domain.split(".")
52
- result = best(match(domain, @rules))
56
+ result = best(match(domain, rules))
53
57
  [gimme!(domain, result.size), gimme!(domain), domain].reverse.map { |d| d ? d.join(".") : "" }
54
58
  end
55
59
 
56
60
  def tld(domain)
57
61
  domain = domain.split(".")
58
- result = best(match(domain, @rules))
62
+ result = best(match(domain, rules))
59
63
  gimme!(domain, result.size).join(".")
60
64
  end
61
65
 
62
66
  def cdn(domain)
63
67
  domain = domain.split(".")
64
- result = best(match(domain, @rules))
68
+ result = best(match(domain, rules))
65
69
  gimme!(domain, result.size + 1).join(".")
66
70
  end
67
71
 
68
72
  private
69
73
 
70
- def name
71
- URI.parse(@config.effective_tld_names_url).path.split("/").last + ".cache"
74
+ def fetch
75
+ @rules = Parser.parse(open(@config.effective_tld_names_url))
72
76
  end
73
77
 
74
78
  def cache
75
- @cache = {:rules => @rules, :created_at => Time.now, :tag => rand(36**8).to_s(36)}
76
- open(File.join(@config.cache_dir, name), "w") { |f| Marshal.dump(@cache, f) }
79
+ @cache_file[:rules] = rules
77
80
  end
78
81
 
79
82
  def uncache
80
- open(File.join(@config.cache_dir, name), "r") { |f| @cache = Marshal.load(f) }
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)
86
- end
87
-
88
- def download
89
- @rules = Parser.parse(open(@config.effective_tld_names_url))
83
+ rules = @cache_file[:rules] unless @cache_file.expired?
90
84
  end
91
85
 
92
86
  def match(domain, rules)
@@ -38,6 +38,14 @@ class PublicSuffixList
38
38
  @data = data and dump_data
39
39
  end
40
40
 
41
+ def [](key)
42
+ data[key]
43
+ end
44
+
45
+ def []=(key, value)
46
+ data[key] = value and dump_data
47
+ end
48
+
41
49
  def expired?
42
50
  !cache? or !([0, nil].include?(@config.cache_expiry_period) or data[:created_at] + @config.cache_expiry_period > Time.now)
43
51
  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.5
4
+ version: 0.0.6
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 00:00:00 -05:00
12
+ date: 2010-01-13 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15