Module: CostaRicaAddressUtils::Guatemala

Defined in:
lib/costa_rica_address_utils/guatemala.rb

Constant Summary collapse

JSON_FILE_PATH =

Guatemala-specific implementation

File.join(File.dirname(__FILE__), '..', '..', 'data', 'guatemala_dataset.json')
LOCATIONS_DATASET =
JSON.parse(File.read(JSON_FILE_PATH))

Class Method Summary collapse

Class Method Details

.address_valid?(department:, municipality:) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/costa_rica_address_utils/guatemala.rb', line 50

def self.address_valid?(department:, municipality:)
  is_valid = true
  begin
    data = fetch_address_data(department: department, municipality: municipality)
    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(department:, municipality:) ⇒ Object

For a given address of 2 levels, return options for each level found and zip code if full address is valid



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/costa_rica_address_utils/guatemala.rb', line 12

def self.fetch_address_data(department:, municipality:)
  raise CostaRicaAddressUtils::InvalidData, 'Department is required' if department.nil? || department.empty?

  department_data = LOCATIONS_DATASET[department]
  municipality_data = !!department_data ? department_data['locationsLevel2'][municipality] : nil

  # Municipality options(lv2), only if department(lv1) is valid
  municipality_options = !!department_data ? department_data['locationsLevel2'].keys : []
  zip = (!!municipality_data && municipality_data['zipCode']) || nil # Zip code, only if full address is valid

  {
    zip: zip,
    # Names options
    department_options: LOCATIONS_DATASET.keys,
    municipality_options: municipality_options
  }
end

.fetch_address_from_zip(zip_code) ⇒ Object

Get one address information from a zip code



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/costa_rica_address_utils/guatemala.rb', line 31

def self.fetch_address_from_zip(zip_code)
  return nil unless zip_valid?(zip_code)

  zip_code_s = zip_code.to_s
  LOCATIONS_DATASET.each do |department, department_data|
    department_data['locationsLevel2'].each do |municipality, municipality_data|
      if municipality_data['zipCode'] == zip_code_s
        return {
          department: department,
          municipality: municipality,
          zip: zip_code_s
        }
      end
    end
  end

  nil
end