costa_rica_address_utils 0.4.0 → 0.5.0
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/CHANGELOG.md +8 -1
- data/Gemfile.lock +1 -1
- data/data/costa_rica_dataset.json +2956 -1
- data/data/guatemala_dataset.json +2674 -1
- data/data/locations_dataset.json +2956 -1
- data/lib/costa_rica_address_utils/costa_rica.rb +33 -13
- data/lib/costa_rica_address_utils/version.rb +1 -1
- metadata +2 -2
|
@@ -5,9 +5,11 @@ require 'json'
|
|
|
5
5
|
module CostaRicaAddressUtils
|
|
6
6
|
module CostaRica
|
|
7
7
|
JSON_FILE_PATH = File.join(File.dirname(__FILE__), '..', '..', 'data', 'locations_dataset.json')
|
|
8
|
+
NEW_JSON_FILE_PATH = File.join(File.dirname(__FILE__), '..', '..', 'data', 'costa_rica_dataset.json')
|
|
8
9
|
|
|
9
10
|
# Load the JSON file and parse it into a Ruby object for general usage
|
|
10
11
|
LOCATIONS_DATASET = JSON.parse(File.read(JSON_FILE_PATH))
|
|
12
|
+
NEW_LOCATIONS_DATASET = JSON.parse(File.read(NEW_JSON_FILE_PATH)) # Should replace locations_dataset.json in the future
|
|
11
13
|
|
|
12
14
|
class Error < StandardError; end
|
|
13
15
|
# Your code goes here...
|
|
@@ -34,20 +36,38 @@ module CostaRicaAddressUtils
|
|
|
34
36
|
end
|
|
35
37
|
|
|
36
38
|
# Get one address information from a zip code
|
|
37
|
-
def fetch_address_from_zip(zip_code)
|
|
39
|
+
def fetch_address_from_zip(zip_code, new_dataset: false)
|
|
38
40
|
return nil unless zip_valid?(zip_code)
|
|
39
41
|
|
|
40
42
|
zip_code_s = zip_code.to_s
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
43
|
+
|
|
44
|
+
if new_dataset
|
|
45
|
+
NEW_LOCATIONS_DATASET.each do |province, province_data|
|
|
46
|
+
province_data['locationsLevel2'].each do |canton, canton_data|
|
|
47
|
+
canton_data['locationsLevel3'].each do |district, district_data|
|
|
48
|
+
if district_data['zipCode'] == zip_code_s
|
|
49
|
+
return {
|
|
50
|
+
province: province,
|
|
51
|
+
canton: canton,
|
|
52
|
+
district: district,
|
|
53
|
+
zip: zip_code_s
|
|
54
|
+
}
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
else
|
|
60
|
+
LOCATIONS_DATASET.each do |province, province_data|
|
|
61
|
+
province_data['cantons'].each do |canton, canton_data|
|
|
62
|
+
canton_data['districts'].each do |district, district_data|
|
|
63
|
+
if district_data['zip_code'] == zip_code_s
|
|
64
|
+
return {
|
|
65
|
+
province: province,
|
|
66
|
+
canton: canton,
|
|
67
|
+
district: district,
|
|
68
|
+
zip: zip_code_s
|
|
69
|
+
}
|
|
70
|
+
end
|
|
51
71
|
end
|
|
52
72
|
end
|
|
53
73
|
end
|
|
@@ -56,10 +76,10 @@ module CostaRicaAddressUtils
|
|
|
56
76
|
nil
|
|
57
77
|
end
|
|
58
78
|
|
|
59
|
-
def fetch_address_from_zip!(zip_code)
|
|
79
|
+
def fetch_address_from_zip!(zip_code, new_dataset: false)
|
|
60
80
|
raise "Zip code provided #{zip_code} is invalid. Must be a 5 digits number" unless zip_valid?(zip_code)
|
|
61
81
|
|
|
62
|
-
fetch_address_from_zip(zip_code)
|
|
82
|
+
fetch_address_from_zip(zip_code, new_dataset: new_dataset)
|
|
63
83
|
end
|
|
64
84
|
|
|
65
85
|
def address_valid?(province:, canton:, district:)
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: costa_rica_address_utils
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- vicvans20
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2025-
|
|
11
|
+
date: 2025-05-12 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: byebug
|