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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/valid_zip_codes.rb +33 -3
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b5062e5ab5f279a849244cd7fb5ec4a6a307c77e
4
- data.tar.gz: 073e4e0e1fa9623f4cca59fd1dd30637fb3c0279
3
+ metadata.gz: d4b5a186e7f5cc08e71ca45c066063d0248499c8
4
+ data.tar.gz: 9b13b27f7576ffa138a04f17630089d99a5f0f8c
5
5
  SHA512:
6
- metadata.gz: f8fedec9b50c43286d9008e629f8eb77c1f07b08f4d9ac487246bbb4e58744537bbe9dc3a19e0451aa50216feeb33a9bcc58951e4b604f80c2f8dd5c52be9985
7
- data.tar.gz: 2e3d3d617d867bedb565c486c4821ed70675bb99512106c3cb54181ec7922fb56b0fcfe97710839da08ee772970986defe1d4e1b4e49422ccdcca7c35e914d82
6
+ metadata.gz: e833f0ab542a88dab60b466e73ccf6b82aac7dbb7990d124dc9258a58f4c8b65d1e1bde2b496cd7180050f854b7d7a00609d52e449ec281cdc7b1b93c3aa4959
7
+ data.tar.gz: b447d63fa661ea80e3cef6e8025946f4ea7d0334c65484c0ac0d60bef478e5acdc37369cb65692ce63aefb2cea6a8907d75c5881d099badca608c345dd68a9b2
@@ -4,25 +4,44 @@ class ValidZipCodes
4
4
 
5
5
  SUPPORTED_COUNTRIES = ["DK", "SE", "FO", "GL"]
6
6
 
7
- def initialize
8
- load_zips
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 load_zips
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"))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valid_zip_codes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mathias Salomonsson