public-suffix-list 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,7 +11,17 @@ method of finding the highest level at which a domain may be
11
11
  registered for a particular top-level domain (the policies differ with
12
12
  each registry)...". Public Suffix List is also a small Ruby library
13
13
  designed to make the Public Suffix List (http://publicsuffix.org/)
14
- easier to use.
14
+ easier to use. Public Suffix List will transparently download the
15
+ latest list of top-level domains, parse the list, and optionally cache
16
+ the parsed data.
17
+
18
+ The public API is as simple as I could make it. Instantiating the
19
+ PublicSuffixList class downloads, parses, and caches the data. There
20
+ are three instance methods:
21
+
22
+ [*tld*(domain)] return the top-level domain name
23
+ [*cdn*(domain)] return the canonical domain name
24
+ [*split*(domain)] split the domain name into parts
15
25
 
16
26
  == Features
17
27
 
@@ -23,24 +33,32 @@ easier to use.
23
33
 
24
34
  require "public_suffix_list"
25
35
 
26
- # downloads and parses the latest data file and returns "com"
36
+ # Downloads and parses the latest data file and returns "com".
27
37
  PublicSuffixList.new.tld("foobar.com")
28
38
 
29
- # downloads and parses the latest data file and returns "foobar.com"
39
+ # Downloads and parses the latest data file and returns "foobar.com".
30
40
  PublicSuffixList.new.cdn("foobar.com")
31
41
 
32
- # downloads and parses the latest data file, caches it in /tmp, returns ["abc", "xyz", "co.uk"]
42
+ # Downloads and parses the latest data file, caches it in /tmp,
43
+ # and returns ["abc", "xyz", "co.uk"].
33
44
  PublicSuffixList.new(:cache_dir => "/tmp").split("abc.xyz.co.uk")
34
45
 
35
- # loads the cached data in /tmp and returns ["test", "nhs", "uk"]
36
- PublicSuffixList.new(:cache_dir => "/tmp").split("test.nhs.uk")
46
+ # Loads the cached data in /tmp if it is less than 100 seconds old,
47
+ # downloads, parses, and caches it if it is older, and returns
48
+ # ["test", "nhs", "uk"].
49
+ PublicSuffixList.new(:cache_dir => "/tmp", :cache_expiry_period => 100).split("test.nhs.uk")
37
50
 
38
- # you don't have to instantiate PublicSuffixList every time you use it, of course...
51
+ # You don't have to instantiate PublicSuffixList every time you
52
+ # use it, of course...
39
53
  p = PublicSuffixList.new
40
54
  p.split("fee.fi.fo.com") # => ["fee.fi", "fo", "com"]
41
55
  p.cdn("fee.fi.fo.com") # => "fo.com", "cdn" is "canonical domain name"
42
56
  p.tld("fee.fi.fo.com") # => "com", "tld" is "top-level domain"
43
57
 
58
+ # You can even use other data files, both local and remote
59
+ # (as long as they conform to the Public Suffix List file format).
60
+ PublicSuffixList.new(:url => "spec/test.dat")
61
+
44
62
  == Requirements
45
63
 
46
64
  None that I am aware of.
@@ -6,7 +6,7 @@ require "public_suffix_list/parser.rb"
6
6
 
7
7
  class PublicSuffixList
8
8
 
9
- VERSION = "0.0.6"
9
+ VERSION = "0.1.0"
10
10
 
11
11
  def self.config
12
12
  @@config ||= Config.new
@@ -20,12 +20,16 @@ class PublicSuffixList
20
20
 
21
21
  attr_accessor :cache_dir
22
22
  attr_accessor :cache_expiry_period
23
- attr_accessor :effective_tld_names_url
23
+ attr_accessor :url
24
+
25
+ # effective_tld_names_url is deprecated
26
+ alias_method :effective_tld_names_url, :url
27
+ alias_method :effective_tld_names_url=, :url=
24
28
 
25
29
  def initialize
26
30
  @cache_dir = nil
27
31
  @cache_expiry_period = 30 * 24 * 60 * 60
28
- @effective_tld_names_url = "http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/src/effective_tld_names.dat?raw=1"
32
+ @url = "http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/src/effective_tld_names.dat?raw=1"
29
33
  end
30
34
 
31
35
  end
@@ -72,7 +76,7 @@ class PublicSuffixList
72
76
  private
73
77
 
74
78
  def fetch
75
- @rules = Parser.parse(open(@config.effective_tld_names_url))
79
+ @rules = Parser.parse(open(@config.url))
76
80
  end
77
81
 
78
82
  def cache
@@ -80,7 +84,7 @@ class PublicSuffixList
80
84
  end
81
85
 
82
86
  def uncache
83
- rules = @cache_file[:rules] unless @cache_file.expired?
87
+ @rules = @cache_file[:rules] unless @cache_file.expired?
84
88
  end
85
89
 
86
90
  def match(domain, rules)
@@ -11,7 +11,7 @@ class PublicSuffixList
11
11
  end
12
12
 
13
13
  def file
14
- File.join(@config.cache_dir, URI.parse(@config.effective_tld_names_url).path.split("/").last + ".cache") if cache?
14
+ File.join(@config.cache_dir, URI.parse(@config.url).path.split("/").last + ".cache") if cache?
15
15
  end
16
16
 
17
17
  def exist?
@@ -2,30 +2,40 @@ require 'lib/public_suffix_list'
2
2
 
3
3
  describe PublicSuffixList do
4
4
 
5
- before do
6
- @public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat")
7
- @public_suffix_list.cache_file.should be nil
5
+ it "should accept both effective_tld_names_url and url" do
6
+ public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat")
7
+ public_suffix_list.config.effective_tld_names_url.should == "spec/test.dat"
8
+ public_suffix_list.config.url.should == "spec/test.dat"
9
+ public_suffix_list = PublicSuffixList.new(:url => "spec/test.dat")
10
+ public_suffix_list.config.effective_tld_names_url.should == "spec/test.dat"
11
+ public_suffix_list.config.url.should == "spec/test.dat"
8
12
  end
9
13
 
10
14
  it "should calculate tld and cdn correctly" do
11
- @public_suffix_list.cdn("foo.bar.com").should == "bar.com"
12
- @public_suffix_list.tld("foo.bar.com").should == "com"
13
- @public_suffix_list.cdn("foobar.com").should == "foobar.com"
14
- @public_suffix_list.tld("foobar.com").should == "com"
15
+ public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat")
16
+ public_suffix_list.cache_file.should be nil
17
+ public_suffix_list.cdn("foo.bar.com").should == "bar.com"
18
+ public_suffix_list.tld("foo.bar.com").should == "com"
19
+ public_suffix_list.cdn("foobar.com").should == "foobar.com"
20
+ public_suffix_list.tld("foobar.com").should == "com"
15
21
  end
16
22
 
17
23
  it "should not assume the root name is a top level domain" do
18
- @public_suffix_list.tld("com").should == "com"
19
- @public_suffix_list.tld("bar.foo").should == "bar.foo"
20
- @public_suffix_list.tld("baz.foo").should == "baz.foo"
21
- @public_suffix_list.tld("qux.foo").should == ""
22
- @public_suffix_list.tld("foo").should == ""
24
+ public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat")
25
+ public_suffix_list.cache_file.should be nil
26
+ public_suffix_list.tld("com").should == "com"
27
+ public_suffix_list.tld("bar.foo").should == "bar.foo"
28
+ public_suffix_list.tld("baz.foo").should == "baz.foo"
29
+ public_suffix_list.tld("qux.foo").should == ""
30
+ public_suffix_list.tld("foo").should == ""
23
31
  end
24
32
 
25
33
  it "should handle edge cases correctly" do
26
- @public_suffix_list.split("").should == ["", "", ""]
27
- @public_suffix_list.cdn("").should == ""
28
- @public_suffix_list.tld("").should == ""
34
+ public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat")
35
+ public_suffix_list.cache_file.should be nil
36
+ public_suffix_list.split("").should == ["", "", ""]
37
+ public_suffix_list.cdn("").should == ""
38
+ public_suffix_list.tld("").should == ""
29
39
  end
30
40
 
31
41
  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.6
4
+ version: 0.1.0
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-13 00:00:00 -05:00
12
+ date: 2010-01-15 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15