sundawg_country_codes 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest CHANGED
@@ -1,6 +1,11 @@
1
+ Manifest
1
2
  README
2
3
  Rakefile
3
4
  lib/countries.yml
4
5
  lib/country_iso_translater.rb
6
+ lib/usa_state_translater.rb
7
+ lib/usa_states.yml
8
+ sundawg_country_codes.gemspec
5
9
  test/country_iso_translater_test.rb
6
- Manifest
10
+ test/test_helper.rb
11
+ test/usa_translater_test.rb
data/README CHANGED
@@ -64,6 +64,11 @@ To build the unicode HTML for a symbol:
64
64
  currency = SunDawg::CountryIsoTranslater.get_iso4217_currency_by_iso3166_alpha2("UZ")
65
65
  assert_equal SunDawg::CountryIsoTranslater.build_html_unicode(currency["unicode_hex"]), "&#x43b&#x432"
66
66
 
67
+ To translate to and from USA state name and the 2-digit code:
68
+
69
+ SunDawg::USAStateTranslater.translate_name_to_code('New York')
70
+ SunDawg::USAStateTranslater.translate_code_to_name('NY')
71
+
67
72
  DATA
68
73
  ----
69
74
  All country code and names are stored in a countries.yml file that can be changed as you need. The latest codes and country names are derived from:
@@ -87,6 +92,7 @@ config.gem 'sundawg_country_codes', :lib => 'country_iso_translater'
87
92
 
88
93
  VERSION HISTORY
89
94
  ---------------
95
+ v0.0.3 - Added USA State translater and cleaned up unit tests.
90
96
  v0.0.2b - Completed all currency information, unicode HTML utility, and bug fixes. Gem version re-published as 0.0.2.
91
97
  v0.0.2 - Added ISO 4217 currency information.
92
98
  v0.0.1 - Allows basic ISO 3166 translation.
data/Rakefile CHANGED
@@ -3,7 +3,7 @@ require 'rubygems'
3
3
  require 'rake'
4
4
  require 'echoe'
5
5
 
6
- Echoe.new('sundawg_country_codes', '0.0.2') do |p|
6
+ Echoe.new('sundawg_country_codes', '0.0.3') do |p|
7
7
  p.description = "Manage ISO 3166 Country Names and Codes."
8
8
  p.url = "http://github.com/SunDawg/country_codes"
9
9
  p.author = "Christopher Sun"
@@ -0,0 +1,27 @@
1
+ require 'yaml'
2
+
3
+ module SunDawg
4
+ module USAStateTranslater
5
+ # allows client application to override YAML hash
6
+ FILE = File.expand_path(File.join(File.dirname(__FILE__), 'usa_states.yml')) unless defined?(FILE)
7
+ USA_STATES = YAML.load_file(FILE) unless defined?(USA_STATES)
8
+
9
+ # O(N) translation from state name to 2-digit code
10
+ def self.translate_name_to_code(name)
11
+ USA_STATES.each_pair { |key, value|
12
+ return key if value["name"] == name
13
+ }
14
+ raise NoStateError.new("[#{name}] IS NOT VALID")
15
+ end
16
+
17
+ # O(1) translation of 2-digit code to name
18
+ def self.translate_code_to_name(code)
19
+ state = USA_STATES[code]
20
+ raise NoStateError.new("[#{code}] IS NOT VALID") if state.nil?
21
+ state["name"]
22
+ end
23
+
24
+ class NoStateError < StandardError
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,100 @@
1
+ AL:
2
+ name: Alabama
3
+ AK:
4
+ name: Alaska
5
+ AZ:
6
+ name: Arizona
7
+ AR:
8
+ name: Arkansas
9
+ CA:
10
+ name: California
11
+ CO:
12
+ name: Colorado
13
+ CT:
14
+ name: Connecticut
15
+ DE:
16
+ name: Delaware
17
+ FL:
18
+ name: Florida
19
+ GA:
20
+ name: Georgia
21
+ HI:
22
+ name: Hawaii
23
+ ID:
24
+ name: Idaho
25
+ IL:
26
+ name: Illinois
27
+ IN:
28
+ name: Indiana
29
+ IA:
30
+ name: Iowa
31
+ KS:
32
+ name: Kansas
33
+ KY:
34
+ name: Kentucky
35
+ LA:
36
+ name: Louisiana
37
+ ME:
38
+ name: Maine
39
+ MD:
40
+ name: Maryland
41
+ MA:
42
+ name: Massachusetts
43
+ MI:
44
+ name: Michigan
45
+ MN:
46
+ name: Minnesota
47
+ MS:
48
+ name: Mississippi
49
+ MO:
50
+ name: Missouri
51
+ MT:
52
+ name: Montana
53
+ NE:
54
+ name: Nebraska
55
+ NV:
56
+ name: Nevada
57
+ NH:
58
+ name: New Hampshire
59
+ NJ:
60
+ name: New Jersey
61
+ NM:
62
+ name: New Mexico
63
+ NY:
64
+ name: New York
65
+ NC:
66
+ name: North Carolina
67
+ ND:
68
+ name: North Dakota
69
+ OH:
70
+ name: Ohio
71
+ OK:
72
+ name: Oklahoma
73
+ OR:
74
+ name: Oregon
75
+ PA:
76
+ name: Pennsylvania
77
+ RI:
78
+ name: Rhode Island
79
+ SC:
80
+ name: South Carolina
81
+ SD:
82
+ name: South Dakota
83
+ TN:
84
+ name: Tennessee
85
+ TX:
86
+ name: Texas
87
+ UT:
88
+ name: Utah
89
+ VT:
90
+ name: Vermont
91
+ VA:
92
+ name: Virginia
93
+ WA:
94
+ name: Washington
95
+ WV:
96
+ name: West Virginia
97
+ WI:
98
+ name: Wisconsin
99
+ WY:
100
+ name: Wyoming
@@ -2,22 +2,22 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{sundawg_country_codes}
5
- s.version = "0.0.2"
5
+ s.version = "0.0.3"
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-12-04}
9
+ s.date = %q{2010-01-11}
10
10
  s.description = %q{Manage ISO 3166 Country Names and Codes.}
11
11
  s.email = %q{christopher.sun@gmail.com}
12
- s.extra_rdoc_files = ["README", "lib/countries.yml", "lib/country_iso_translater.rb"]
13
- s.files = ["README", "Rakefile", "lib/countries.yml", "lib/country_iso_translater.rb", "test/country_iso_translater_test.rb", "Manifest", "sundawg_country_codes.gemspec", "test/test_helper.rb"]
12
+ s.extra_rdoc_files = ["README", "lib/countries.yml", "lib/country_iso_translater.rb", "lib/usa_state_translater.rb", "lib/usa_states.yml"]
13
+ s.files = ["Manifest", "README", "Rakefile", "lib/countries.yml", "lib/country_iso_translater.rb", "lib/usa_state_translater.rb", "lib/usa_states.yml", "sundawg_country_codes.gemspec", "test/country_iso_translater_test.rb", "test/test_helper.rb", "test/usa_translater_test.rb"]
14
14
  s.homepage = %q{http://github.com/SunDawg/country_codes}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Sundawg_country_codes", "--main", "README"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{sundawg_country_codes}
18
18
  s.rubygems_version = %q{1.3.5}
19
19
  s.summary = %q{Manage ISO 3166 Country Names and Codes.}
20
- s.test_files = ["test/country_iso_translater_test.rb", "test/test_helper.rb"]
20
+ s.test_files = ["test/country_iso_translater_test.rb", "test/test_helper.rb", "test/usa_translater_test.rb"]
21
21
 
22
22
  if s.respond_to? :specification_version then
23
23
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -90,44 +90,24 @@ class CountryIsoTranslaterTest < Test::Unit::TestCase
90
90
  end
91
91
 
92
92
  def test_error_on_invalid_currency
93
- begin
93
+ assert_raise SunDawg::CountryIsoTranslater::NoCountryError do
94
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
95
  end
101
96
 
102
- begin
97
+ assert_raise SunDawg::CountryIsoTranslater::NoCurrencyError do
103
98
  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
99
  end
110
100
  end
111
101
 
112
102
  def test_error_on_invalid_country
113
- begin
103
+ assert_raise SunDawg::CountryIsoTranslater::NoCountryError do
114
104
  client.translate_iso3166_name_to_alpha2("Candyland")
115
- fail "Expected client::NoCountryError"
116
- rescue client::NoCountryError => e
117
- assert_equal e.to_s, "[Candyland] IS NOT VALID"
118
- rescue => e
119
- fail "Expected client::NoCountryError"
120
105
  end
121
106
  end
122
107
 
123
108
  def test_error_on_invalid_alpha2
124
- begin
109
+ assert_raise SunDawg::CountryIsoTranslater::NoCountryError do
125
110
  client.translate_iso3166_alpha2_to_name("XX")
126
- fail "Expected client::NoCountryError"
127
- rescue client::NoCountryError => e
128
- assert_equal e.to_s, "[XX] IS NOT VALID"
129
- rescue => e
130
- fail "Expected client::NoCountryError"
131
111
  end
132
112
  end
133
113
 
@@ -0,0 +1,44 @@
1
+ require 'test_helper'
2
+
3
+ # allows override of FILE
4
+ module SunDawg
5
+ module USAStateTranslater
6
+ FILE = "lib/usa_states.yml"
7
+ end
8
+ end
9
+
10
+ require 'usa_state_translater'
11
+
12
+ class USAStateTranslaterTest < Test::Unit::TestCase
13
+ def test_code_to_name
14
+ assert_equal 'California', client.translate_code_to_name('CA')
15
+ assert_equal 'New York', client.translate_code_to_name('NY')
16
+ end
17
+
18
+ def test_name_to_code
19
+ assert_equal 'TX', client.translate_name_to_code('Texas')
20
+ assert_equal 'UT', client.translate_name_to_code('Utah')
21
+ end
22
+
23
+ def test_data_integrity
24
+ assert_equal 50, SunDawg::USAStateTranslater::USA_STATES.size
25
+ end
26
+
27
+ def test_error_on_invalid_name
28
+ assert_raise SunDawg::USAStateTranslater::NoStateError do
29
+ client.translate_name_to_code('Petertopia')
30
+ end
31
+ end
32
+
33
+ def test_error_on_invalid_state
34
+ assert_raise SunDawg::USAStateTranslater::NoStateError do
35
+ client.translate_code_to_name('XX')
36
+ end
37
+ end
38
+
39
+ protected
40
+
41
+ def client
42
+ SunDawg::USAStateTranslater
43
+ end
44
+ end
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.2
4
+ version: 0.0.3
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-04 00:00:00 -05:00
12
+ date: 2010-01-11 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,14 +23,20 @@ extra_rdoc_files:
23
23
  - README
24
24
  - lib/countries.yml
25
25
  - lib/country_iso_translater.rb
26
+ - lib/usa_state_translater.rb
27
+ - lib/usa_states.yml
26
28
  files:
29
+ - Manifest
27
30
  - README
28
31
  - Rakefile
29
32
  - lib/countries.yml
30
33
  - lib/country_iso_translater.rb
31
- - test/country_iso_translater_test.rb
32
- - Manifest
34
+ - lib/usa_state_translater.rb
35
+ - lib/usa_states.yml
33
36
  - sundawg_country_codes.gemspec
37
+ - test/country_iso_translater_test.rb
38
+ - test/test_helper.rb
39
+ - test/usa_translater_test.rb
34
40
  has_rdoc: true
35
41
  homepage: http://github.com/SunDawg/country_codes
36
42
  licenses: []
@@ -67,3 +73,4 @@ summary: Manage ISO 3166 Country Names and Codes.
67
73
  test_files:
68
74
  - test/country_iso_translater_test.rb
69
75
  - test/test_helper.rb
76
+ - test/usa_translater_test.rb