Sha256: 1211ad0164f2729c80ce81732de2ea7b440354650d4e3cb03ccdfec0952e1e26

Contents?: true

Size: 757 Bytes

Versions: 1

Compression:

Stored size: 757 Bytes

Contents

# frozen_string_literal: true
require 'jpostcode/version'
require 'jpostcode/address'
require 'json'

module Jpostcode
  DATA_DIR = File.dirname(__FILE__) + '/../jpostcode-data/data/json/'

  module_function

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

    json_file = DATA_DIR + zip_code.slice(0, 3) + '.json'
    return nil unless File.exist?(json_file)

    data = JSON.parse(File.open(json_file).read)
    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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jpostcode-ait-0.1.1 lib/jpostcode.rb