sundawg_country_codes 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +44 -0
- data/Rakefile +1 -1
- data/lib/countries.yml +790 -26
- data/lib/country_iso_translater.rb +23 -0
- data/sundawg_country_codes.gemspec +2 -2
- data/test/country_iso_translater_test.rb +83 -0
- metadata +2 -2
@@ -43,7 +43,30 @@ module SunDawg
|
|
43
43
|
country["name"]
|
44
44
|
end
|
45
45
|
|
46
|
+
# O(1) find for iso 4217 currency information
|
47
|
+
def self.get_iso4217_currency_by_iso3166_alpha2(code)
|
48
|
+
country = COUNTRIES[code]
|
49
|
+
raise NoCountryError.new("[#{code}] IS NOT VALID") if country.nil?
|
50
|
+
raise NoCurrencyError.new("[#{code}] HAS NO ISO4217 CURRENCY") if country["currency_iso4217"].nil?
|
51
|
+
country["currency_iso4217"]
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.build_html_unicode(unicode_hex)
|
55
|
+
s = ""
|
56
|
+
if unicode_hex.class == Fixnum
|
57
|
+
s = "&#x#{unicode_hex.to_s(16)}"
|
58
|
+
elsif unicode_hex.class == Array
|
59
|
+
unicode_hex.each { |i|
|
60
|
+
s += "&#x#{i.to_s(16)}"
|
61
|
+
}
|
62
|
+
end
|
63
|
+
s
|
64
|
+
end
|
65
|
+
|
46
66
|
class NoCountryError < StandardError
|
47
67
|
end
|
68
|
+
|
69
|
+
class NoCurrencyError < StandardError
|
70
|
+
end
|
48
71
|
end
|
49
72
|
end
|
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{sundawg_country_codes}
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.2"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Christopher Sun"]
|
9
|
-
s.date = %q{2009-
|
9
|
+
s.date = %q{2009-12-04}
|
10
10
|
s.description = %q{Manage ISO 3166 Country Names and Codes.}
|
11
11
|
s.email = %q{christopher.sun@gmail.com}
|
12
12
|
s.extra_rdoc_files = ["README", "lib/countries.yml", "lib/country_iso_translater.rb"]
|
@@ -35,6 +35,80 @@ class CountryIsoTranslaterTest < Test::Unit::TestCase
|
|
35
35
|
assert_equal client.translate_standard("China", "name", "alpha2"), "CN"
|
36
36
|
end
|
37
37
|
|
38
|
+
def test_get_iso4217_currency_by_iso3166_alpha2
|
39
|
+
currency = client.get_iso4217_currency_by_iso3166_alpha2("US")
|
40
|
+
assert_equal currency["code"], "USD"
|
41
|
+
assert_equal currency["symbol"], "$"
|
42
|
+
assert_equal currency["name"], "Dollars"
|
43
|
+
assert_equal currency["unicode_hex"], 36
|
44
|
+
assert_equal currency["unicode_hex"].to_s(16), "24"
|
45
|
+
|
46
|
+
currency = client.get_iso4217_currency_by_iso3166_alpha2("AF")
|
47
|
+
assert_equal currency["code"], "AFN"
|
48
|
+
assert_equal currency["symbol"], "؋"
|
49
|
+
assert_equal currency["name"], "Afghanis"
|
50
|
+
assert_equal currency["unicode_hex"], 1547
|
51
|
+
|
52
|
+
currency = client.get_iso4217_currency_by_iso3166_alpha2("AL")
|
53
|
+
assert_equal currency["unicode_hex"].class, Array
|
54
|
+
assert_equal currency["unicode_hex"][0], 76
|
55
|
+
|
56
|
+
currency = client.get_iso4217_currency_by_iso3166_alpha2("AT")
|
57
|
+
assert_equal currency["code"], "EUR"
|
58
|
+
|
59
|
+
currency = client.get_iso4217_currency_by_iso3166_alpha2("NZ")
|
60
|
+
assert_equal currency["code"], "NZD"
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_data_integrity
|
64
|
+
SunDawg::CountryIsoTranslater::COUNTRIES.each_pair { |k, v|
|
65
|
+
# check country codes
|
66
|
+
assert_not_nil v["alpha2"]
|
67
|
+
assert_not_nil v["alpha3"]
|
68
|
+
assert_not_nil v["name"]
|
69
|
+
|
70
|
+
# check currency code and name
|
71
|
+
unless v["currency_iso4217"].nil?
|
72
|
+
assert_not_nil v["currency_iso4217"]["code"]
|
73
|
+
assert_not_nil v["currency_iso4217"]["name"]
|
74
|
+
unless v["currency_iso4217"]["alt_currency"].nil?
|
75
|
+
assert_not_nil v["currency_iso4217"]["alt_currency"]["code"]
|
76
|
+
assert_not_nil v["currency_iso4217"]["alt_currency"]["name"]
|
77
|
+
end
|
78
|
+
|
79
|
+
# if symbol is present, make sure unicode_hex is present
|
80
|
+
unless v["currency_iso4217"]["symbol"].nil?
|
81
|
+
assert_not_nil v["currency_iso4217"]["unicode_hex"]
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# check to make sure currency_iso4217 is spelled correctly
|
86
|
+
v.each_pair { |k2, v2|
|
87
|
+
assert_equal k2, "currency_iso4217" if (k2[0..1] == "c")
|
88
|
+
}
|
89
|
+
}
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_error_on_invalid_currency
|
93
|
+
begin
|
94
|
+
client.get_iso4217_currency_by_iso3166_alpha2("XX")
|
95
|
+
fail "Expected client::NoCountryError"
|
96
|
+
rescue client::NoCountryError => e
|
97
|
+
assert_equal e.to_s, "[XX] IS NOT VALID"
|
98
|
+
rescue => e
|
99
|
+
fail "Expected client::NoCountryError"
|
100
|
+
end
|
101
|
+
|
102
|
+
begin
|
103
|
+
client.get_iso4217_currency_by_iso3166_alpha2("AX")
|
104
|
+
fail "Expected client::NoCurrencyError"
|
105
|
+
rescue client::NoCurrencyError => e
|
106
|
+
assert_equal e.to_s, "[AX] HAS NO ISO4217 CURRENCY"
|
107
|
+
rescue => e
|
108
|
+
fail "Expected client::NoCurrencyError"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
38
112
|
def test_error_on_invalid_country
|
39
113
|
begin
|
40
114
|
client.translate_iso3166_name_to_alpha2("Candyland")
|
@@ -57,6 +131,15 @@ class CountryIsoTranslaterTest < Test::Unit::TestCase
|
|
57
131
|
end
|
58
132
|
end
|
59
133
|
|
134
|
+
def test_build_html_unicode
|
135
|
+
currency = client.get_iso4217_currency_by_iso3166_alpha2("US")
|
136
|
+
assert_equal client.build_html_unicode(currency["unicode_hex"]), "$"
|
137
|
+
currency = client.get_iso4217_currency_by_iso3166_alpha2("TH")
|
138
|
+
assert_equal client.build_html_unicode(currency["unicode_hex"]), "฿"
|
139
|
+
currency = client.get_iso4217_currency_by_iso3166_alpha2("UZ")
|
140
|
+
assert_equal client.build_html_unicode(currency["unicode_hex"]), "лв"
|
141
|
+
end
|
142
|
+
|
60
143
|
protected
|
61
144
|
|
62
145
|
def client
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sundawg_country_codes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christopher Sun
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-12-04 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|