Sha256: 8d3e909c7c0516e00776ac0047a2a5f6d748ea2810df81e534fdc1667da6aaab

Contents?: true

Size: 725 Bytes

Versions: 8

Compression:

Stored size: 725 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.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
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
jpostcode-1.0.0.20220801 lib/jpostcode.rb
jpostcode-1.0.0.20220701 lib/jpostcode.rb
jpostcode-1.0.0.20220601 lib/jpostcode.rb
jpostcode-1.0.0.20220501 lib/jpostcode.rb
jpostcode-1.0.0.20220401 lib/jpostcode.rb
jpostcode-1.0.0.20220301 lib/jpostcode.rb
jpostcode-1.0.0.20220201 lib/jpostcode.rb
jpostcode-1.0.0.20220101 lib/jpostcode.rb