Module: Jpostcode

Defined in:
lib/jpostcode.rb,
lib/jpostcode/address.rb,
lib/jpostcode/version.rb

Defined Under Namespace

Classes: Address

Constant Summary collapse

DATA_DIR =
"#{File.dirname(__FILE__)}/../jpostcode-data/data/json/"
VERSION =
'1.0.0.20241201'

Class Method Summary collapse

Class Method Details

.find(raw_zip_code) ⇒ Object



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

def find(raw_zip_code)
  zip_code = raw_zip_code.to_s.delete('-')
  return nil unless zip_code.match?(/^\d{7,7}$/)

  json_file = "#{DATA_DIR}#{zip_code.slice(0, 3)}.json"
  return nil unless File.exist?(json_file)

  data = JSON.parse(File.read(json_file))
  address_data = data[zip_code.slice(3, 4)]
  return nil if address_data.nil?

  if address_data.instance_of?(Array)
    address_data.map { |a| Jpostcode::Address.new(a) }
  else
    Jpostcode::Address.new(address_data)
  end
end