iso_country_codes 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b068a38c7d7b521e7405175c90656f0639fc1967
4
- data.tar.gz: d221055e2da8108c8c1335ca3441f375c09939e5
3
+ metadata.gz: d04d03b9d8eaf22ad3a697f58bfd6297c1a6ca96
4
+ data.tar.gz: f8a169a08884eb555deef316727213cacfb2f8d5
5
5
  SHA512:
6
- metadata.gz: fb391ef4e86acec128fc4ca4ab5fffce502136d52636fe7913e280d19d4d9f9cbd40141a5e40d7d78708cebffc3da02315cd93b478e32c7d0386cc8d111f3499
7
- data.tar.gz: 141b333bd255b15fb3d14dd7761cef87a5a86dc08b715017d4bfb16763fd90cbf88f62d349f5edfe67adb7a7bbb599f7c4c0e053bc31e2d80cec0b5ec25934c1
6
+ metadata.gz: 681e85eb7097630b81c383809f02da955f5924295c45b520ffdb50a294292b10a9cc6847560a9f9ce12c8900570da553f120ad040e7636a75c8c69f8a60175ab
7
+ data.tar.gz: 46ef6cf0d7bfd5c35933c5759c683b0554093dbf76884c6ec4e20545a09f44b0053a878dc864ff47bdb51e579eaed57de8c742faa264b02df42f96894b25745c
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.6.0 / 2014-10-23
2
+
3
+ * Added ability to raise custom fallbacks and return default values
4
+
1
5
  === 0.5.0 / 2014-10-13
2
6
 
3
7
  * Added continent codes
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.6.0
@@ -11,7 +11,10 @@ class IsoCountryCodes # :nodoc:
11
11
  Code.all
12
12
  end
13
13
 
14
- def find(code)
14
+ DEFAULT_FALLBACK = lambda { |error| raise UnknownCodeError, error }
15
+
16
+ def find(code, &fallback)
17
+ fallback ||= DEFAULT_FALLBACK
15
18
  code = code.to_s.upcase
16
19
  instance = nil
17
20
 
@@ -27,35 +30,41 @@ class IsoCountryCodes # :nodoc:
27
30
  instance = all.select { |c| c.alpha3 == code }.first
28
31
  end
29
32
 
30
- raise UnknownCodeError, "No ISO 3166-1 code could be found for '#{code}'." if instance.nil?
33
+ return fallback.call "No ISO 3166-1 code could be found for '#{code}'." if instance.nil?
31
34
 
32
35
  instance
33
36
  end
34
37
 
35
- def search_by_name(str)
38
+ def search_by_name(str, &fallback)
39
+ fallback ||= DEFAULT_FALLBACK
40
+
36
41
  instances = all.select { |c| c.name.upcase == str.upcase }
37
42
  instances = all.select { |c| c.name.match(/^#{str}/i) } if instances.empty?
38
43
  instances = all.select { |c| c.name.match(/#{str}/i) } if instances.empty?
39
44
 
40
- raise UnknownCodeError, "No ISO 3166-1 codes could be found searching with name '#{str}'." if instances.empty?
45
+ return fallback.call "No ISO 3166-1 codes could be found searching with name '#{str}'." if instances.empty?
41
46
 
42
47
  instances
43
48
  end
44
49
 
45
- def search_by_calling_code(code)
50
+ def search_by_calling_code(code, &fallback)
51
+ fallback ||= DEFAULT_FALLBACK
52
+
46
53
  code = "+#{code.to_i}" # Normalize code
47
54
  instances = all.select { |c| c.calling == code }
48
55
 
49
- raise UnknownCodeError, "No ISO 3166-1 codes could be found searching with calling code '#{code}'." if instances.empty?
56
+ return fallback.call "No ISO 3166-1 codes could be found searching with calling code '#{code}'." if instances.empty?
50
57
 
51
58
  instances
52
59
  end
53
60
 
54
- def search_by_calling(code) # Alias of search_by_calling_code
55
- search_by_calling_code code
61
+ def search_by_calling(code, &fallback) # Alias of search_by_calling_code
62
+ search_by_calling_code code, &fallback
56
63
  end
57
64
 
58
- def search_by_currency(code)
65
+ def search_by_currency(code, &fallback)
66
+ fallback ||= DEFAULT_FALLBACK
67
+
59
68
  code = code.to_str.upcase
60
69
  instances = all.select { |c|
61
70
  c.currencies.select { |currency|
@@ -63,7 +72,7 @@ class IsoCountryCodes # :nodoc:
63
72
  }.empty?
64
73
  }
65
74
 
66
- raise UnknownCodeError, "No ISO 3166-1 codes could be found searching with currency '#{code}'." if instances.empty?
75
+ return fallback.call "No ISO 3166-1 codes could be found searching with currency '#{code}'." if instances.empty?
67
76
 
68
77
  instances
69
78
  end
@@ -28,6 +28,17 @@ class TestIsoCountryCodes < Test::Unit::TestCase
28
28
  end
29
29
  end
30
30
 
31
+ def test_find_with_unknown_alpha2_code_and_raise_custom_exception
32
+ assert_raise ArgumentError do
33
+ IsoCountryCodes.find('xx') { |error| raise ArgumentError }
34
+ end
35
+ end
36
+
37
+ def test_find_with_unknown_alpha2_code_and_return_custom_value
38
+ assert_equal IsoCountryCodes.find('xx') { IsoCountryCodes::Code::AUS.instance },
39
+ IsoCountryCodes::Code::AUS.instance
40
+ end
41
+
31
42
  def test_find_with_lowercase_alpha3
32
43
  assert_equal IsoCountryCodes::Code::AUS.instance, IsoCountryCodes.find('aus')
33
44
  end
@@ -71,6 +82,16 @@ class TestIsoCountryCodes < Test::Unit::TestCase
71
82
  end
72
83
  end
73
84
 
85
+ def test_search_by_name_unknown_country_and_raise_custom_exception
86
+ assert_raise ArgumentError do
87
+ IsoCountryCodes.search_by_name('unknown country') {|error| raise ArgumentError }
88
+ end
89
+ end
90
+
91
+ def test_search_by_name_unknown_country_and_return_custom_value
92
+ assert_equal IsoCountryCodes.search_by_name('unknown country') {[]}, []
93
+ end
94
+
74
95
  def test_search_by_name_exact_match
75
96
  assert_equal(
76
97
  [IsoCountryCodes::Code::CCK.instance],
@@ -108,6 +129,16 @@ class TestIsoCountryCodes < Test::Unit::TestCase
108
129
  end
109
130
  end
110
131
 
132
+ def test_search_by_currency_invalid_value_and_raise_custom_exception
133
+ assert_raise ArgumentError do
134
+ IsoCountryCodes.search_by_currency('USS') { |error| raise ArgumentError }
135
+ end
136
+ end
137
+
138
+ def test_search_by_currency_invalid_value_and_return_custom_value
139
+ assert_equal IsoCountryCodes.search_by_currency('USS') {[]}, []
140
+ end
141
+
111
142
  def test_search_by_calling_code
112
143
  assert_equal [IsoCountryCodes::Code::ZAF.instance], IsoCountryCodes.search_by_calling_code('+27')
113
144
  assert_equal([
@@ -123,6 +154,16 @@ class TestIsoCountryCodes < Test::Unit::TestCase
123
154
  end
124
155
  end
125
156
 
157
+ def test_search_by_calling_code_invalid_value_and_raise_custom_exception
158
+ assert_raise ArgumentError do
159
+ IsoCountryCodes.search_by_calling_code('00'){ |error| raise ArgumentError }
160
+ end
161
+ end
162
+
163
+ def test_search_by_calling_code_invalid_value_and_return_custom_value
164
+ assert_equal IsoCountryCodes.search_by_calling_code('00') {[]}, []
165
+ end
166
+
126
167
  def test_get_main_currency
127
168
  assert_equal 'AUD', IsoCountryCodes.find('AUS').main_currency
128
169
  end
@@ -186,4 +227,14 @@ class TestIsoCountryCodes < Test::Unit::TestCase
186
227
  IsoCountryCodes.find('FOO')
187
228
  end
188
229
  end
230
+
231
+ def test_unknown_iso_code_and_return_custom_value
232
+ assert_equal IsoCountryCodes.find('FOO') { [] }, []
233
+ end
234
+
235
+ def test_unknown_iso_code_and_raise_custom_error
236
+ assert_raise ArgumentError do
237
+ IsoCountryCodes.find('FOO') { |error| raise ArgumentError }
238
+ end
239
+ end
189
240
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iso_country_codes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Rabarts
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-13 00:00:00.000000000 Z
11
+ date: 2014-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler