Module: CostaRicaAddressUtils::CostaRica
- Included in:
- CostaRicaAddressUtils
- Defined in:
- lib/costa_rica_address_utils/costa_rica.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- JSON_FILE_PATH =
File.join(File.dirname(__FILE__), '..', '..', 'data', 'locations_dataset.json')
- NEW_JSON_FILE_PATH =
File.join(File.dirname(__FILE__), '..', '..', 'data', 'costa_rica_dataset.json')
- LOCATIONS_DATASET =
Load the JSON file and parse it into a Ruby object for general usage
JSON.parse(File.read(JSON_FILE_PATH))
- NEW_LOCATIONS_DATASET =
Should replace locations_dataset.json in the future
JSON.parse(File.read(NEW_JSON_FILE_PATH))
Instance Method Summary collapse
- #address_valid?(province:, canton:, district:) ⇒ Boolean
-
#fetch_address_data(province:, canton: nil, district: nil) ⇒ Object
Fetch the address data from provided input, return options for subaddresses on each level and zip code if full address is valid.
-
#fetch_address_from_zip(zip_code, new_dataset: false) ⇒ Object
Get one address information from a zip code.
- #fetch_address_from_zip!(zip_code, new_dataset: false) ⇒ Object
- #zip_valid?(zip_code) ⇒ Boolean
Instance Method Details
#address_valid?(province:, canton:, district:) ⇒ Boolean
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/costa_rica_address_utils/costa_rica.rb', line 85 def address_valid?(province:, canton:, district:) is_valid = true begin data = fetch_address_data(province: province, canton: canton, district: district) is_valid = !!data[:zip] # Is valid if matched to a zip code rescue CostaRicaAddressUtils::InvalidData => e is_valid = false end is_valid end |
#fetch_address_data(province:, canton: nil, district: nil) ⇒ Object
Fetch the address data from provided input, return options for subaddresses on each level and zip code if full address is valid
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/costa_rica_address_utils/costa_rica.rb', line 18 def fetch_address_data(province:, canton: nil, district: nil) raise CostaRicaAddressUtils::InvalidData, 'Province is required' if province.nil? || province.empty? province_data = LOCATIONS_DATASET[province] canton_data = !!province_data ? province_data['cantons'][canton] : nil district_data = !!canton_data ? canton_data['districts'][district] : nil = !!province_data ? province_data['cantons'].keys : [] # Cantons options, only if province is valid = !!canton_data ? canton_data['districts'].keys : [] # Districts options, only if canton is valid zip = (!!district_data && district_data['zip_code']) || nil # Zip code, only if full address is valid { zip: zip, # Names options province_options: LOCATIONS_DATASET.keys, canton_options: , district_options: } end |
#fetch_address_from_zip(zip_code, new_dataset: false) ⇒ Object
Get one address information from a zip code
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/costa_rica_address_utils/costa_rica.rb', line 39 def fetch_address_from_zip(zip_code, new_dataset: false) return nil unless zip_valid?(zip_code) zip_code_s = zip_code.to_s if new_dataset NEW_LOCATIONS_DATASET.each do |province, province_data| province_data['locationsLevel2'].each do |canton, canton_data| canton_data['locationsLevel3'].each do |district, district_data| if district_data['zipCode'] == zip_code_s return { province: province, canton: canton, district: district, zip: zip_code_s } end end end end else LOCATIONS_DATASET.each do |province, province_data| province_data['cantons'].each do |canton, canton_data| canton_data['districts'].each do |district, district_data| if district_data['zip_code'] == zip_code_s return { province: province, canton: canton, district: district, zip: zip_code_s } end end end end end nil end |
#fetch_address_from_zip!(zip_code, new_dataset: false) ⇒ Object
79 80 81 82 83 |
# File 'lib/costa_rica_address_utils/costa_rica.rb', line 79 def fetch_address_from_zip!(zip_code, new_dataset: false) raise "Zip code provided #{zip_code} is invalid. Must be a 5 digits number" unless zip_valid?(zip_code) fetch_address_from_zip(zip_code, new_dataset: new_dataset) end |
#zip_valid?(zip_code) ⇒ Boolean
97 98 99 |
# File 'lib/costa_rica_address_utils/costa_rica.rb', line 97 def zip_valid?(zip_code) !!zip_code && zip_code.to_s.length == 5 end |