alexrabarts-iso_country_codes 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/Manifest.txt +1 -1
- data/README.txt +3 -4
- data/iso_country_codes.gemspec +1 -1
- data/lib/iso_country_codes/iso_country_codes.rb +6 -2
- data/test/test_iso_country_codes.rb +7 -0
- metadata +1 -1
data/History.txt
CHANGED
data/Manifest.txt
CHANGED
data/README.txt
CHANGED
@@ -8,10 +8,6 @@ Provides ISO codes and names for countries.
|
|
8
8
|
|
9
9
|
* Search by name or code (alpha-2, alpha-3 or numeric)
|
10
10
|
|
11
|
-
== PROBLEMS:
|
12
|
-
|
13
|
-
* Name search requires an exact match (although it is case-insensitive)
|
14
|
-
|
15
11
|
== SYNOPSIS:
|
16
12
|
|
17
13
|
# Finding an ISO code returns the country name and other code formats
|
@@ -29,6 +25,9 @@ Provides ISO codes and names for countries.
|
|
29
25
|
# Codes can be found by country name
|
30
26
|
IsoCountryCodes.find('australia')
|
31
27
|
|
28
|
+
# Inexact matches can also be found
|
29
|
+
IsoCountryCodes.find('united', :fuzzy => true).name # => 'United Kingdom'
|
30
|
+
|
32
31
|
== INSTALL:
|
33
32
|
|
34
33
|
* Via git:
|
data/iso_country_codes.gemspec
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class IsoCountryCodes # :nodoc:
|
2
|
-
VERSION = '0.1.
|
2
|
+
VERSION = '0.1.2'
|
3
3
|
|
4
4
|
class UnknownCodeError < StandardError; end
|
5
5
|
|
@@ -8,7 +8,7 @@ class IsoCountryCodes # :nodoc:
|
|
8
8
|
Code.all
|
9
9
|
end
|
10
10
|
|
11
|
-
def find(code)
|
11
|
+
def find(code, opts={})
|
12
12
|
code = code.to_s.upcase
|
13
13
|
instance = nil
|
14
14
|
|
@@ -24,6 +24,10 @@ class IsoCountryCodes # :nodoc:
|
|
24
24
|
instance = all.select { |c| c.alpha3 == code }.first
|
25
25
|
else
|
26
26
|
instance = all.select { |c| c.name.upcase == code }.first
|
27
|
+
if opts[:fuzzy]
|
28
|
+
instance = all.select { |c| c.name.match(/^#{code}/i) }.first if instance.nil?
|
29
|
+
instance = all.select { |c| c.name.match(/#{code}/i) }.first if instance.nil?
|
30
|
+
end
|
27
31
|
end
|
28
32
|
|
29
33
|
raise UnknownCodeError, "ISO 3166-1 code '#{code}' does not exist." if instance.nil?
|
@@ -42,6 +42,13 @@ class TestIsoCountryCodes < Test::Unit::TestCase
|
|
42
42
|
assert_equal IsoCountryCodes::Code::AUS.instance, IsoCountryCodes.find('Australia')
|
43
43
|
end
|
44
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
|
+
|
45
52
|
def test_get_numeric
|
46
53
|
assert_equal '036', IsoCountryCodes::Code::AUS.numeric
|
47
54
|
assert_equal '036', IsoCountryCodes::Code::AUS.instance.numeric
|