valid_zip_codes 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/valid_zip_codes.rb +33 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d4b5a186e7f5cc08e71ca45c066063d0248499c8
|
4
|
+
data.tar.gz: 9b13b27f7576ffa138a04f17630089d99a5f0f8c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e833f0ab542a88dab60b466e73ccf6b82aac7dbb7990d124dc9258a58f4c8b65d1e1bde2b496cd7180050f854b7d7a00609d52e449ec281cdc7b1b93c3aa4959
|
7
|
+
data.tar.gz: b447d63fa661ea80e3cef6e8025946f4ea7d0334c65484c0ac0d60bef478e5acdc37369cb65692ce63aefb2cea6a8907d75c5881d099badca608c345dd68a9b2
|
data/lib/valid_zip_codes.rb
CHANGED
@@ -4,25 +4,44 @@ class ValidZipCodes
|
|
4
4
|
|
5
5
|
SUPPORTED_COUNTRIES = ["DK", "SE", "FO", "GL"]
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
# Constructor
|
8
|
+
def initialize load_all = false
|
9
|
+
if load_all
|
10
|
+
load_all_countries
|
11
|
+
else
|
12
|
+
@zips = []
|
13
|
+
end
|
9
14
|
end
|
10
15
|
|
16
|
+
# Check if zip is valid
|
11
17
|
def is_valid_zip?(country, zip)
|
18
|
+
|
12
19
|
if supports_country?(country)
|
20
|
+
# Load country if not already loaded
|
21
|
+
load_country(country) if !country_loaded?(country)
|
22
|
+
|
23
|
+
# Query for zip
|
13
24
|
country_hash(country).select{|z| z["zip"] == zip.to_s}.count > 0
|
14
25
|
else
|
15
26
|
raise "Country not supported"
|
16
27
|
end
|
28
|
+
|
17
29
|
end
|
18
30
|
|
31
|
+
# Get city name by zip
|
19
32
|
def get_city_name(country, zip)
|
33
|
+
|
20
34
|
if supports_country?(country)
|
35
|
+
# Load country if not already loaded
|
36
|
+
load_country(country) if !country_loaded?(country)
|
37
|
+
|
38
|
+
# Query for zip
|
21
39
|
sr = country_hash(country).select{|z| z["zip"] == zip.to_s}
|
22
40
|
sr.count > 0 ? sr.first["city"] : raise("Zip not found")
|
23
41
|
else
|
24
42
|
raise "Country not supported"
|
25
43
|
end
|
44
|
+
|
26
45
|
end
|
27
46
|
|
28
47
|
def supports_country?(country)
|
@@ -30,9 +49,20 @@ class ValidZipCodes
|
|
30
49
|
end
|
31
50
|
|
32
51
|
|
52
|
+
|
53
|
+
|
33
54
|
private
|
34
55
|
|
35
|
-
def
|
56
|
+
def country_loaded?(country)
|
57
|
+
@zips.each{|c| return true if c.keys[0] == country}
|
58
|
+
false
|
59
|
+
end
|
60
|
+
|
61
|
+
def load_country(country)
|
62
|
+
@zips << YAML::load_file(File.join(__dir__, "../zips/#{country}.yml")) if supports_country?(country)
|
63
|
+
end
|
64
|
+
|
65
|
+
def load_all_countries
|
36
66
|
@zips = []
|
37
67
|
SUPPORTED_COUNTRIES.each do |c|
|
38
68
|
@zips << YAML::load_file(File.join(__dir__, "../zips/#{c}.yml"))
|