kch-dominion 0.0.2

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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright © 2010 Caio Chassot
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,41 @@
1
+ # Dominion
2
+
3
+ Dominion is a library to extract information from a domain name.
4
+
5
+ We're basically interested in two particulars:
6
+
7
+ * _The de facto TLD_: this is the part of the domain a NIC would be responsible
8
+ for. It may be an honest-to-god TLD, or a ccTLD, and more importantly, a ccSLD.
9
+
10
+ * _The base domain_: That is the domain that is registered with the NIC, clear of
11
+ any subdomains.
12
+
13
+ To resolve this we rely on the Mozilla Foundation's Public Suffix List, which can
14
+ be found at <http://publicsuffix.org/list/>.
15
+
16
+ See <http://publicsuffix.org/> to learn more.
17
+
18
+ A copy of the list file is provided with this library in `var/tlds.dat`, and loaded
19
+ automatically when you `require 'dominion'`.
20
+
21
+
22
+ # Usage
23
+
24
+ require 'dominion'
25
+
26
+ d = Dominion::DomainName.new(".name")
27
+ d.tld? # => true
28
+ d.tld # => "name"
29
+ d.base # => "name"
30
+
31
+ d = Dominion::DomainName.new("foo.bar.co.uk")
32
+ d.tld? # => false
33
+ d.tld # => "co.uk"
34
+ d.base # => "bar.co.uk"
35
+
36
+
37
+ # Colophon
38
+
39
+ Copyright © 2010 Caio Chassot
40
+ Released under the MIT license
41
+ <http://github.com/kch/dominion>
data/lib/dominion.rb ADDED
@@ -0,0 +1,4 @@
1
+ require File.join(File.dirname(__FILE__), "dominion/domain_suffix_rule")
2
+ require File.join(File.dirname(__FILE__), "dominion/domain_name")
3
+
4
+ Dominion::DomainName.load_rules_from_file(File.join(File.dirname(__FILE__), '../var/tlds.dat'))
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ module Dominion
5
+ class DomainName
6
+ attr_reader :labels, :rule
7
+
8
+ # returns the current rule list or takes a new rule list; rules must be DomainSuffixRule instances.
9
+ def self.rules(rules = nil)
10
+ @rules = rules if rules
11
+ @rules
12
+ end
13
+
14
+ # takes the path to a rule file in the format defined in http://publicsuffix.org/format/
15
+ def self.load_rules_from_file(path)
16
+ rules open(path).lines.reject { |s| s =~ %r[\A\s*(//.*)?\Z\n?] }.map { |s| DomainSuffixRule.new(s) }.sort << DomainSuffixRule.new("*")
17
+ end
18
+
19
+ # takes a domain string as argument; i.e. the hostname part of a URL
20
+ def initialize(s)
21
+ raise "No DomainSuffixRule rules loaded" unless self.class.rules
22
+ @labels = s.strip.gsub(/\A\.|\.\z/, '').split(".").reverse
23
+ @rule = self.class.rules.find { |rule| rule =~ self } or raise "Domain #{s.inspect} didn't match any rule."
24
+ end
25
+
26
+ # is this a TLD, ccTLD, ccSLD, etc.?
27
+ def tld?
28
+ labels.length == rule.length
29
+ end
30
+
31
+ # returns the TLD part of the domain
32
+ def tld
33
+ domain(0)
34
+ end
35
+
36
+ # returns the base domain; i.e. the registered domain
37
+ def base
38
+ domain(1)
39
+ end
40
+
41
+ # return the domain with the specified number of extra labels over the TLD. Used internally.
42
+ def domain(non_tld_label_count = 1)
43
+ labels[0, rule.length + non_tld_label_count].reverse.join(".")
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ module Dominion
5
+ class DomainSuffixRule
6
+ attr_reader :labels, :exception, :length
7
+
8
+ def initialize(s)
9
+ @string = s.strip.gsub(/\A\.|\.\z/, '')
10
+ @labels = @string.split(/\A!|\./).reverse
11
+ @exception = @labels.last.empty? and @labels.pop
12
+ @length = @labels.length - (@exception ? 1 : 0)
13
+ end
14
+
15
+ # match against a domain name. the domain must be an instance of DomainName
16
+ def =~(domain)
17
+ labels.zip(domain.labels).all? { |r, d| ["*", d].include? r }
18
+ end
19
+
20
+ def inspect
21
+ @string
22
+ end
23
+
24
+
25
+ protected
26
+
27
+ # these are used internally for sorting a list of rules so that the first match is the correct match
28
+ # according to the algorithm defined in http://publicsuffix.org/format/
29
+
30
+ def comparable
31
+ [@exception ? 0 : 1, -@length, @string]
32
+ end
33
+
34
+ def <=> other
35
+ comparable <=> other.comparable
36
+ end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kch-dominion
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Caio Chassot
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-17 00:00:00 -02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: dev@caiochassot.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - MIT-LICENSE
31
+ - README.markdown
32
+ - lib/dominion/domain_name.rb
33
+ - lib/dominion/domain_suffix_rule.rb
34
+ - lib/dominion.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/kch/dominion
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.7
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Extract TLD information from a domain name.
67
+ test_files: []
68
+