tld 0.6.1

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.
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'iso_country_codes'
3
+
4
+ class TLD
5
+ class Currency
6
+ CURRENCY_MAP = {
7
+ :eu => %w{EUR},
8
+ :gov => %w{USD},
9
+ :mil => %w{USD}
10
+ }
11
+
12
+ class << self
13
+ def find(klass)
14
+ tld = klass.tld
15
+ mapped_currency = CURRENCY_MAP[tld.to_sym]
16
+ if mapped_currency.nil?
17
+ return tld.match(/^\w\w$/) ? IsoCountryCodes.find(TLD::MAP[tld.to_sym] || tld).currencies : []
18
+ else
19
+ return mapped_currency
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,28 @@
1
+ class TLD
2
+ class GenericTld < TLD
3
+ def type
4
+ :generic
5
+ end
6
+ class BIZ < GenericTld #:nodoc:
7
+ self.tld = %q{biz}
8
+ end
9
+ class COM < GenericTld #:nodoc:
10
+ self.tld = %q{com}
11
+ end
12
+ class INFO < GenericTld #:nodoc:
13
+ self.tld = %q{info}
14
+ end
15
+ class NAME < GenericTld #:nodoc:
16
+ self.tld = %q{name}
17
+ end
18
+ class NET < GenericTld #:nodoc:
19
+ self.tld = %q{net}
20
+ end
21
+ class ORG < GenericTld #:nodoc:
22
+ self.tld = %q{org}
23
+ end
24
+ class PRO < GenericTld #:nodoc:
25
+ self.tld = %q{pro}
26
+ end
27
+ end # end GenericTld
28
+ end # end TLD
@@ -0,0 +1,10 @@
1
+ class TLD
2
+ class InfrastructureTld < TLD
3
+ def type
4
+ :infrastructure
5
+ end
6
+ class ARPA < InfrastructureTld #:nodoc:
7
+ self.tld = %q{arpa}
8
+ end
9
+ end # end InfrastructureTld
10
+ end # end TLD
data/lib/tld/name.rb ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'iso_country_codes'
3
+
4
+ class TLD
5
+ class Name
6
+ NAME_MAP = {
7
+ :aero => 'Aeroplane',
8
+ :arpa => 'Address and Routing Parameter Area (ARPA)',
9
+ :asia => 'Asia',
10
+ :bitnet => 'BITNET',
11
+ :biz => 'Business',
12
+ :cat => 'Catalan',
13
+ :com => 'Commercial',
14
+ :coop => 'Cooperative',
15
+ :csnet => 'Computer Science Network (CSNET)',
16
+ :edu => 'Education',
17
+ :eu => 'European Union',
18
+ :example => 'Example domain',
19
+ :exit => 'Preferred Tor exit node',
20
+ :gov => 'US Government',
21
+ :info => 'Information',
22
+ :int => 'Internet',
23
+ :invalid => 'Invalid domain',
24
+ :jobs => 'Jobs',
25
+ :local => 'Network discovery protocol',
26
+ :localhost => 'Localhost',
27
+ :mil => 'US Military',
28
+ :mobi => 'Mobile',
29
+ :museum => 'Museum',
30
+ :name => 'Name',
31
+ :nato => 'North Atlantic Treaty Organization (NATO)',
32
+ :net => 'Network',
33
+ :onion => 'Reachable via Tor',
34
+ :org => 'Organization',
35
+ :pro => 'Professional',
36
+ :root => 'Diagnostic marker',
37
+ :tel => 'Telnic',
38
+ :test => 'Test domain',
39
+ :travel => 'Travel',
40
+ :uucp => 'Reachable by Unix to Unix CoPy (UUCP)'
41
+ }
42
+
43
+ class << self
44
+ def find(klass)
45
+ tld = klass.tld
46
+ NAME_MAP[tld.to_sym] || IsoCountryCodes.find(TLD::MAP[tld.to_sym] || tld).name
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,28 @@
1
+ class TLD
2
+ class PseudoTld < TLD
3
+ def type
4
+ :pseudo
5
+ end
6
+ class BITNET < PseudoTld #:nodoc:
7
+ self.tld = %q{bitnet}
8
+ end
9
+ class CSNET < PseudoTld #:nodoc:
10
+ self.tld = %q{csnet}
11
+ end
12
+ class LOCAL < PseudoTld #:nodoc:
13
+ self.tld = %q{local}
14
+ end
15
+ class ROOT < PseudoTld #:nodoc:
16
+ self.tld = %q{root}
17
+ end
18
+ class UUCP < PseudoTld #:nodoc:
19
+ self.tld = %q{uucp}
20
+ end
21
+ class ONION < PseudoTld #:nodoc:
22
+ self.tld = %q{onion}
23
+ end
24
+ class EXIT < PseudoTld #:nodoc:
25
+ self.tld = %q{exit}
26
+ end
27
+ end # end PseudoTld
28
+ end # end TLD
@@ -0,0 +1,19 @@
1
+ class TLD
2
+ class ReservedTld < TLD
3
+ def type
4
+ :reserved
5
+ end
6
+ class EXAMPLE < ReservedTld #:nodoc:
7
+ self.tld = %q{example}
8
+ end
9
+ class INVALID < ReservedTld #:nodoc:
10
+ self.tld = %q{invalid}
11
+ end
12
+ class LOCALHOST < ReservedTld #:nodoc:
13
+ self.tld = %q{localhost}
14
+ end
15
+ class TEST < ReservedTld #:nodoc:
16
+ self.tld = %q{test}
17
+ end
18
+ end # end ReservedTld
19
+ end # end TLD
@@ -0,0 +1,10 @@
1
+ class TLD
2
+ class RetiredTld < TLD
3
+ def type
4
+ :retired
5
+ end
6
+ class NATO < RetiredTld #:nodoc:
7
+ self.tld = %q{nato}
8
+ end
9
+ end # end RetiredTld
10
+ end # end TLD
@@ -0,0 +1,46 @@
1
+ class TLD
2
+ class SponsoredTld < TLD
3
+ def type
4
+ :sponsored
5
+ end
6
+ class AERO < SponsoredTld #:nodoc:
7
+ self.tld = %q{aero}
8
+ end
9
+ class ASIA < SponsoredTld #:nodoc:
10
+ self.tld = %q{asia}
11
+ end
12
+ class CAT < SponsoredTld #:nodoc:
13
+ self.tld = %q{cat}
14
+ end
15
+ class COOP < SponsoredTld #:nodoc:
16
+ self.tld = %q{coop}
17
+ end
18
+ class EDU < SponsoredTld #:nodoc:
19
+ self.tld = %q{edu}
20
+ end
21
+ class GOV < SponsoredTld #:nodoc:
22
+ self.tld = %q{gov}
23
+ end
24
+ class INT < SponsoredTld #:nodoc:
25
+ self.tld = %q{int}
26
+ end
27
+ class JOBS < SponsoredTld #:nodoc:
28
+ self.tld = %q{jobs}
29
+ end
30
+ class MIL < SponsoredTld #:nodoc:
31
+ self.tld = %q{mil}
32
+ end
33
+ class MOBI < SponsoredTld #:nodoc:
34
+ self.tld = %q{mobi}
35
+ end
36
+ class MUSEUM < SponsoredTld #:nodoc:
37
+ self.tld = %q{museum}
38
+ end
39
+ class TEL < SponsoredTld #:nodoc:
40
+ self.tld = %q{tel}
41
+ end
42
+ class TRAVEL < SponsoredTld #:nodoc:
43
+ self.tld = %q{travel}
44
+ end
45
+ end # end SponsoredTld
46
+ end # end TLD
data/lib/tld/tld.rb ADDED
@@ -0,0 +1,104 @@
1
+ require 'singleton'
2
+ require 'rubygems'
3
+ require 'addressable/uri'
4
+
5
+ class TLD
6
+ MAP = {
7
+ :ac => 'sh',
8
+ :uk => 'gb',
9
+ :su => 'ru',
10
+ :tp => 'tl',
11
+ :yu => 'rs'
12
+ }
13
+
14
+ include Singleton
15
+
16
+ class UnknownTldError < StandardError; end
17
+
18
+ def to_s
19
+ tld
20
+ end
21
+
22
+ def tld
23
+ self.class.tld
24
+ end
25
+
26
+ def name
27
+ self.class.name
28
+ end
29
+
30
+ def main_currency
31
+ self.class.main_currency
32
+ end
33
+
34
+ def currency
35
+ self.class.main_currency
36
+ end
37
+
38
+ def currencies
39
+ self.class.currencies
40
+ end
41
+
42
+ def alpha3
43
+ self.class.alpha3
44
+ end
45
+
46
+ class << self
47
+ attr_accessor :tld
48
+
49
+ @@tlds = []
50
+
51
+ def inherited(tld) #:nodoc:
52
+ super
53
+ @@tlds << tld.instance unless self == TLD
54
+ end
55
+
56
+ def all
57
+ @@tlds.uniq
58
+ end
59
+
60
+ def name
61
+ Name.find(self)
62
+ end
63
+
64
+ def currencies
65
+ Currency.find(self)
66
+ end
67
+
68
+ def alpha3
69
+ Alpha3.find(self)
70
+ end
71
+
72
+ def main_currency
73
+ currencies.first
74
+ end
75
+
76
+ alias_method :currency, :main_currency
77
+
78
+ def find(str)
79
+ host = normalized_host(str)
80
+ host = str.downcase if host == ''
81
+ last = host.match(/\./) ? host.split('.').last : host # Take the last one of foo.bar.baz
82
+ instance = all.select { |t| t.tld == last }.first
83
+
84
+ raise UnknownTldError, "TLD '#{str}' unkown." if instance.nil?
85
+
86
+ instance
87
+ end
88
+
89
+ def has_valid_tld?(str)
90
+ str = normalized_host(str)
91
+ str.match(/\./) ? self.valid?(str.split('.').last) : self.valid?(str)
92
+ end
93
+
94
+ def valid?(tld)
95
+ !!all.select { |t| t.to_s == tld.downcase }.first
96
+ end
97
+
98
+ private
99
+
100
+ def normalized_host(str)
101
+ Addressable::URI.heuristic_parse(str).normalized_host.to_s rescue str
102
+ end
103
+ end
104
+ end
data/test/tld_test.rb ADDED
@@ -0,0 +1,87 @@
1
+ require 'test/unit'
2
+ require 'tld'
3
+ require 'shoulda'
4
+
5
+ class TestTld < Test::Unit::TestCase
6
+ should 'find TLD by exact match' do
7
+ assert_equal 'au', TLD.find('au').tld
8
+ end
9
+
10
+ should 'find TLD by exact match with dot' do
11
+ assert_equal 'au', TLD.find('.au').tld
12
+ end
13
+
14
+ should 'find TLD with case' do
15
+ assert_equal 'au', TLD.find('AU').tld
16
+ end
17
+
18
+ should 'find TLD by hostname' do
19
+ assert_equal 'au', TLD.find('foo.bar.au').tld
20
+ end
21
+
22
+ should 'find TLD by url' do
23
+ assert_equal 'au', TLD.find('http://foo.bar.au/baz').tld
24
+ end
25
+
26
+ should 'get TLD currency' do
27
+ assert_equal 'AUD', TLD.find('au').currency
28
+ end
29
+
30
+ should 'get mapped TLD currency' do
31
+ assert_equal 'EUR', TLD.find('eu').currency
32
+ assert_equal 'GBP', TLD.find('uk').currency
33
+ end
34
+
35
+ should 'get TLD name' do
36
+ assert_equal 'Australia', TLD.find('au').name
37
+ assert_equal 'Business', TLD.find('biz').name
38
+ end
39
+
40
+ should 'get iso alpha3 code' do
41
+ assert_equal 'AUS', TLD.find('au').alpha3
42
+ assert_equal 'GBR', TLD.find('uk').alpha3
43
+ end
44
+
45
+ should 'get TLD as a string' do
46
+ tld = TLD.find('au')
47
+
48
+ assert_equal 'au', tld.to_s
49
+ assert_equal tld.tld, tld.to_s
50
+ end
51
+
52
+ should 'com should map to empty currency (not Comoros)' do
53
+ assert_equal [], TLD.find('com').currencies
54
+ assert_nil TLD.find('com').currency
55
+ end
56
+
57
+ should 'raise exception when TLD is unknown' do
58
+ assert_raises TLD::UnknownTldError do
59
+ TLD.find('foo')
60
+ end
61
+ end
62
+
63
+ should 'confirm that TLD is valid' do
64
+ assert TLD.valid?('au')
65
+ assert TLD.valid?('AU')
66
+ end
67
+
68
+ should 'confirm that TLD is invalid' do
69
+ assert_equal false, TLD.valid?('not-a-tld')
70
+ end
71
+
72
+ should 'confirm that hostname has a valid TLD' do
73
+ assert TLD.has_valid_tld?('foo.com.au')
74
+ assert TLD.has_valid_tld?('http://foo.com.au/bar')
75
+ end
76
+
77
+ should 'confirm that hostname does not have a valid TLD' do
78
+ assert_equal false, TLD.has_valid_tld?('foo.bar')
79
+ assert_equal false, TLD.has_valid_tld?('http://foo.bar')
80
+ end
81
+
82
+ should 'raise UnknownTldError if string cannot be parsed by Addressable::URI' do
83
+ assert_raises TLD::UnknownTldError do
84
+ TLD.find('foo:')
85
+ end
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tld
3
+ version: !ruby/object:Gem::Version
4
+ hash: 5
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 6
9
+ - 1
10
+ version: 0.6.1
11
+ platform: ruby
12
+ authors:
13
+ - alex
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-05-20 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: iso_country_codes
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 19
30
+ segments:
31
+ - 0
32
+ - 2
33
+ - 2
34
+ version: 0.2.2
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: addressable
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ description: Top-level domain library
52
+ email: alexrabarts@gmail.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - README.rdoc
59
+ files:
60
+ - History.txt
61
+ - Manifest.txt
62
+ - README.rdoc
63
+ - VERSION.yml
64
+ - lib/tld/alpha3.rb
65
+ - lib/tld/cc_tld.rb
66
+ - lib/tld/currency.rb
67
+ - lib/tld/generic_tld.rb
68
+ - lib/tld/infrastructure_tld.rb
69
+ - lib/tld/name.rb
70
+ - lib/tld/pseudo_tld.rb
71
+ - lib/tld/reserved_tld.rb
72
+ - lib/tld/retired_tld.rb
73
+ - lib/tld/sponsored_tld.rb
74
+ - lib/tld/tld.rb
75
+ - lib/tld.rb
76
+ - test/tld_test.rb
77
+ has_rdoc: true
78
+ homepage: http://github.com/alexrabarts/tld
79
+ licenses: []
80
+
81
+ post_install_message:
82
+ rdoc_options:
83
+ - --inline-source
84
+ - --charset=UTF-8
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ requirements: []
106
+
107
+ rubyforge_project:
108
+ rubygems_version: 1.3.7
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Provides meta information for Internet Top Level Domains (TLDs).
112
+ test_files: []
113
+