iso_country_codes 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/History.txt +25 -0
- data/Manifest.txt +15 -0
- data/README.rdoc +67 -0
- data/Rakefile +54 -0
- data/VERSION.yml +4 -0
- data/iso_country_codes.gemspec +52 -0
- data/lib/iso_country_codes/code.rb +59 -0
- data/lib/iso_country_codes/iso_3166_1.rb +1483 -0
- data/lib/iso_country_codes/iso_4217.rb +744 -0
- data/lib/iso_country_codes/iso_country_codes.rb +36 -0
- data/lib/iso_country_codes.rb +7 -0
- data/rakelib/cultivate.rake +7 -0
- data/rakelib/iso_3166_1.rake +13 -0
- data/rakelib/iso_3166_1.rb +51 -0
- data/rakelib/iso_3166_1.rb.erb +15 -0
- data/test/iso_country_codes_test.rb +91 -0
- metadata +83 -0
@@ -0,0 +1,36 @@
|
|
1
|
+
class IsoCountryCodes # :nodoc:
|
2
|
+
class UnknownCodeError < StandardError; end
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def all
|
6
|
+
Code.all
|
7
|
+
end
|
8
|
+
|
9
|
+
def find(code, opts={})
|
10
|
+
code = code.to_s.upcase
|
11
|
+
instance = nil
|
12
|
+
|
13
|
+
if code.match(/^\d{2}$/)
|
14
|
+
code = "0#{code}" # Make numeric codes three digits
|
15
|
+
end
|
16
|
+
|
17
|
+
if code.match(/^\d{3}$/)
|
18
|
+
instance = all.select { |c| c.numeric == code }.first
|
19
|
+
elsif code.match(/^[A-Z]{2}$/)
|
20
|
+
instance = all.select { |c| c.alpha2 == code }.first
|
21
|
+
elsif code.match(/^[A-Z]{3}$/)
|
22
|
+
instance = all.select { |c| c.alpha3 == code }.first
|
23
|
+
else
|
24
|
+
instance = all.select { |c| c.name.upcase == code }.first
|
25
|
+
if opts[:fuzzy]
|
26
|
+
instance = all.select { |c| c.name.match(/^#{code}/i) }.first if instance.nil?
|
27
|
+
instance = all.select { |c| c.name.match(/#{code}/i) }.first if instance.nil?
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
raise UnknownCodeError, "ISO 3166-1 code '#{code}' does not exist." if instance.nil?
|
32
|
+
|
33
|
+
instance
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# Only works on unixy type systems.
|
2
|
+
# http://blog.behindlogic.com/2008/10/auto-generate-your-manifest-and-gemspec.html
|
3
|
+
desc 'Rebuild manifest and gemspec.'
|
4
|
+
task :cultivate do
|
5
|
+
system %q{touch Manifest.txt; rake check_manifest | grep -v "(in " | patch}
|
6
|
+
system %q{rake debug_gem | grep -v "(in " > `basename \`pwd\``.gemspec}
|
7
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
namespace :codes do
|
2
|
+
desc 'Create ISO 3166-1 code classes from Wikipedia ISO 3166-1 tables.'
|
3
|
+
task :update do
|
4
|
+
dirname = File.dirname(__FILE__)
|
5
|
+
gen = File.join(dirname, %w{iso_3166_1.rb})
|
6
|
+
lib = File.expand_path(File.join(dirname, %w{.. lib iso_country_codes iso_3166_1.rb}))
|
7
|
+
require gen
|
8
|
+
modules = IsoCountryCodes::Task::UpdateCodes.get
|
9
|
+
File.open(lib, File::CREAT | File::TRUNC | File::WRONLY) do |f|
|
10
|
+
f.write modules
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end # :currency
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'erubis'
|
4
|
+
|
5
|
+
class IsoCountryCodes
|
6
|
+
module Task
|
7
|
+
module UpdateCodes
|
8
|
+
def self.get
|
9
|
+
doc = Nokogiri::HTML.parse(open('http://en.wikipedia.org/wiki/ISO_3166-1'))
|
10
|
+
codes = {}
|
11
|
+
td_map = {
|
12
|
+
:name => 1,
|
13
|
+
:alpha2 => 2,
|
14
|
+
:alpha3 => 3,
|
15
|
+
:numeric => 4
|
16
|
+
}
|
17
|
+
|
18
|
+
code_labels = td_map.keys
|
19
|
+
|
20
|
+
doc.search('table.wikitable.sortable tr').each do |row|
|
21
|
+
value_hash = {}
|
22
|
+
|
23
|
+
code_labels.each do |key|
|
24
|
+
selector = "td:nth-of-type(#{td_map[key]})"
|
25
|
+
selector << ' a' if key == :name
|
26
|
+
|
27
|
+
value = row.search(selector).text.strip
|
28
|
+
|
29
|
+
next if value == ''
|
30
|
+
|
31
|
+
value_hash[key] = value
|
32
|
+
|
33
|
+
if value_hash.length == code_labels.length - 1
|
34
|
+
value_hash.keys.each do |value_hash_key|
|
35
|
+
codes[value_hash[value_hash_key]] = value_hash if value_hash_key == :alpha3
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
to_ruby(codes) if codes
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.to_ruby(codes)
|
45
|
+
tmpl = File.read(File.join(File.dirname(__FILE__), 'iso_3166_1.rb.erb'))
|
46
|
+
eruby = Erubis::Eruby.new(tmpl)
|
47
|
+
eruby.result(:codes => codes)
|
48
|
+
end
|
49
|
+
end # UpdateCodes
|
50
|
+
end # Task
|
51
|
+
end # IsoCountryCodes
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
class IsoCountryCodes
|
4
|
+
class Code
|
5
|
+
<% codes.keys.each do |code| -%>
|
6
|
+
class <%= code.upcase %> < Code #:nodoc:
|
7
|
+
self.numeric = %q{<%= codes[code][:numeric] %>}
|
8
|
+
self.name = %q{<%= codes[code][:name] %>}
|
9
|
+
self.alpha2 = %q{<%= codes[code][:alpha2] %>}
|
10
|
+
self.alpha3 = %q{<%= codes[code][:alpha3] %>}
|
11
|
+
end
|
12
|
+
<% end -%>
|
13
|
+
end # end Code
|
14
|
+
end # IsoCountryCodes
|
15
|
+
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'iso_country_codes'
|
3
|
+
|
4
|
+
class TestIsoCountryCodes < Test::Unit::TestCase
|
5
|
+
def test_find_with_numeric_as_two_digit_string
|
6
|
+
assert_equal IsoCountryCodes::Code::AUS.instance, IsoCountryCodes.find('36')
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_find_with_numeric_as_three_digit_string
|
10
|
+
assert_equal IsoCountryCodes::Code::AUS.instance, IsoCountryCodes.find('036')
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_find_with_numeric_as_integer
|
14
|
+
assert_equal IsoCountryCodes::Code::AUS.instance, IsoCountryCodes.find(36)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_find_with_lowercase_alpha2
|
18
|
+
assert_equal IsoCountryCodes::Code::AUS.instance, IsoCountryCodes.find('au')
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_find_with_uppercase_alpha2
|
22
|
+
assert_equal IsoCountryCodes::Code::AUS.instance, IsoCountryCodes.find('AU')
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_find_with_lowercase_alpha3
|
26
|
+
assert_equal IsoCountryCodes::Code::AUS.instance, IsoCountryCodes.find('aus')
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_find_with_uppercase_alpha3
|
30
|
+
assert_equal IsoCountryCodes::Code::AUS.instance, IsoCountryCodes.find('AUS')
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_find_with_lowercase_name
|
34
|
+
assert_equal IsoCountryCodes::Code::AUS.instance, IsoCountryCodes.find('australia')
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_find_with_uppercase_name
|
38
|
+
assert_equal IsoCountryCodes::Code::AUS.instance, IsoCountryCodes.find('AUSTRALIA')
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_find_with_mixed_case_name
|
42
|
+
assert_equal IsoCountryCodes::Code::AUS.instance, IsoCountryCodes.find('Australia')
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_fuzzy_find
|
46
|
+
assert_equal IsoCountryCodes::Code::GBR.instance, IsoCountryCodes.find('united', :fuzzy => true)
|
47
|
+
assert_raises IsoCountryCodes::UnknownCodeError do
|
48
|
+
IsoCountryCodes.find('united')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_get_main_currency
|
53
|
+
assert_equal 'AUD', IsoCountryCodes.find('AUS').main_currency
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_currency_alias_method
|
57
|
+
code = IsoCountryCodes.find('AUS')
|
58
|
+
assert_equal code.main_currency, code.currency
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_get_multiple_currencies
|
62
|
+
assert_equal IsoCountryCodes.find('ATA').currencies, %w{AUD GBP}
|
63
|
+
assert_equal IsoCountryCodes.find('AUS').currencies, %w{AUD}
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_get_numeric
|
67
|
+
assert_equal '036', IsoCountryCodes::Code::AUS.numeric
|
68
|
+
assert_equal '036', IsoCountryCodes::Code::AUS.instance.numeric
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_get_alpha2
|
72
|
+
assert_equal 'AU', IsoCountryCodes::Code::AUS.alpha2
|
73
|
+
assert_equal 'AU', IsoCountryCodes::Code::AUS.instance.alpha2
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_get_alpha3
|
77
|
+
assert_equal 'AUS', IsoCountryCodes::Code::AUS.alpha3
|
78
|
+
assert_equal 'AUS', IsoCountryCodes::Code::AUS.instance.alpha3
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_get_name
|
82
|
+
assert_equal 'Australia', IsoCountryCodes::Code::AUS.name
|
83
|
+
assert_equal 'Australia', IsoCountryCodes::Code::AUS.instance.name
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_unknown_iso_code
|
87
|
+
assert_raises IsoCountryCodes::UnknownCodeError do
|
88
|
+
IsoCountryCodes.find('FOO')
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: iso_country_codes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 2
|
10
|
+
version: 0.2.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- alex
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2009-08-07 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: ISO country code and currency library
|
23
|
+
email: alexrabarts@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- History.txt
|
33
|
+
- Manifest.txt
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- VERSION.yml
|
37
|
+
- iso_country_codes.gemspec
|
38
|
+
- lib/iso_country_codes.rb
|
39
|
+
- lib/iso_country_codes/code.rb
|
40
|
+
- lib/iso_country_codes/iso_3166_1.rb
|
41
|
+
- lib/iso_country_codes/iso_4217.rb
|
42
|
+
- lib/iso_country_codes/iso_country_codes.rb
|
43
|
+
- rakelib/cultivate.rake
|
44
|
+
- rakelib/iso_3166_1.rake
|
45
|
+
- rakelib/iso_3166_1.rb
|
46
|
+
- rakelib/iso_3166_1.rb.erb
|
47
|
+
- test/iso_country_codes_test.rb
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://github.com/alexrabarts/iso_country_codes
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options:
|
54
|
+
- --charset=UTF-8
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.3.7
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: Provides ISO 3166-1 country codes/names and ISO 4217 currencies.
|
82
|
+
test_files:
|
83
|
+
- test/iso_country_codes_test.rb
|