valid_zip_codes 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 45d0c722fe3da2737779c1e5c1a981ddadd5b2ec
4
+ data.tar.gz: 613fe08522f81345dea5a96f2faf7885dc267e6c
5
+ SHA512:
6
+ metadata.gz: d199ac427891347859bf9d4331b21fc5ea3af73ed58825cc090e7e144b540e7ac5332bf8cc0aa839c33cdcec090a7066cac2d40c7e52213f461d5d805f0c40a9
7
+ data.tar.gz: 677f055a0c21ee159733c65732568d2548ee47f2fa258d6a2a11d27d124e0f30c20290f073647444a4da74f5eac641e8916d69edc16f10896a7c94e3903523bc
@@ -0,0 +1,58 @@
1
+ require 'yaml'
2
+
3
+ class ValidZipCodes
4
+
5
+ SUPPORTED_COUNTRIES = ["DK", "SE"]
6
+
7
+ def initialize
8
+ load_zips
9
+ end
10
+
11
+ def is_valid_zip?(country, zip)
12
+ if supports_country?(country)
13
+ country_hash(country).select{|z| z["zip"] == zip.to_s}.count > 0
14
+ else
15
+ raise "Country not supported"
16
+ end
17
+ end
18
+
19
+ def get_city_name(country, zip)
20
+ if supports_country?(country)
21
+ sr = country_hash(country).select{|z| z["zip"] == zip.to_s}
22
+ sr.count > 0 ? sr.first["city"] : raise("Zip not found")
23
+ else
24
+ raise "Country not supported"
25
+ end
26
+ end
27
+
28
+ def supports_country?(country)
29
+ SUPPORTED_COUNTRIES.include?(country)
30
+ end
31
+
32
+
33
+ private
34
+
35
+ def load_zips
36
+ @zips = []
37
+ SUPPORTED_COUNTRIES.each do |c|
38
+ @zips << YAML::load_file(File.join(__dir__, "../zips/#{c}.yml"))
39
+ end
40
+ end
41
+
42
+ def country_hash country
43
+ @zips.each{|c| return c[country] if c.keys[0] == country}
44
+ raise "Country not found"
45
+ end
46
+
47
+ end
48
+
49
+ validator = ValidZipCodes.new
50
+
51
+ # Check if zip code is valid
52
+ validator.is_valid_zip?("DK", 8000)
53
+
54
+ # Get the city name for zip code
55
+ validator.get_city_name("DK", 8000)
56
+
57
+ # Check if gem supports country
58
+ validator.supports_country?("DK")
@@ -0,0 +1,53 @@
1
+ require_relative "../lib/valid_zip_codes"
2
+
3
+ describe "ValidZipCodes" do
4
+
5
+ before(:each) do
6
+ @validator = ValidZipCodes.new
7
+ end
8
+
9
+ context "is_valid_zip" do
10
+ it "should return true on valid zip in DK" do
11
+ expect(@validator.is_valid_zip?("DK", 8600)).to be(true)
12
+ end
13
+
14
+ it "should throw exception if country not supported" do
15
+ expect{@validator.is_valid_zip?("US", 8000)}.to raise_error
16
+ end
17
+
18
+ it "should return true on valid zip in SE" do
19
+ expect(@validator.is_valid_zip?("SE", 18622)).to be(true)
20
+ end
21
+ end
22
+
23
+ context "get_city_name" do
24
+
25
+ it "should return correct city name for city in DK" do
26
+ expect(@validator.get_city_name("DK", 8000)).to eq("Aarhus C")
27
+ end
28
+
29
+ it "should throw exception if country not supported" do
30
+ expect{@validator.get_city_name("US", 8000)}.to raise_error
31
+ end
32
+
33
+ it "should return correct city name for city in SE" do
34
+ expect(@validator.get_city_name("SE", 18622)).to eq("Vallentuna")
35
+ end
36
+
37
+ end
38
+
39
+ context "supports_country" do
40
+
41
+ it "should return true for supported countries" do
42
+ expect(@validator.supports_country?("DK")).to be(true)
43
+ expect(@validator.supports_country?("SE")).to be(true)
44
+ end
45
+
46
+ it "should return false for unsupported countries" do
47
+ expect(@validator.supports_country?("US")).to be(false)
48
+ end
49
+
50
+ end
51
+
52
+
53
+ end