Module: Jipcode
- Defined in:
- lib/jipcode.rb,
lib/jipcode/version.rb,
lib/jipcode/japan_post.rb
Defined Under Namespace
Modules: JapanPost
Constant Summary collapse
- ZIPCODE_PATH =
"#{File.dirname(__FILE__)}/../zipcode/latest".freeze
- PREFECTURE_CODE =
YAML.load_file("#{File.dirname(__FILE__)}/../prefecture_code.yml").freeze
- VERSION =
'3.1.4'
Class Method Summary collapse
- .basic_address_from(address_param) ⇒ Object
- .extended_address_from(address_param, opt = {}) ⇒ Object
- .locate(zipcode, opt = {}) ⇒ Object
Class Method Details
.basic_address_from(address_param) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/jipcode.rb', line 27 def basic_address_from(address_param) { zipcode: address_param[0], prefecture: address_param[1], city: address_param[2], town: address_param[3] } end |
.extended_address_from(address_param, opt = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/jipcode.rb', line 36 def extended_address_from(address_param, opt={}) address = basic_address_from(address_param) address[:prefecture_code] = PREFECTURE_CODE.invert[address_param[1]] if opt[:prefecture_code] if opt[:kana] then address[:prefecture_kana] = address_param[4] address[:city_kana] = address_param[5] address[:town_kana] = address_param[6] || "" end address end |
.locate(zipcode, opt = {}) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/jipcode.rb', line 9 def locate(zipcode, opt={}) # 数字7桁以外の入力は受け付けない return [] unless zipcode&.match?(/\A\d{7}?\z/) # 上3桁にマッチするファイルが存在しなければ該当なし path = "#{ZIPCODE_PATH}/#{zipcode[0..2]}.csv" return [] unless File.exist?(path) addresses_array = CSV.read(path).select { |address| address[0] == zipcode } if opt.empty? # optが空の場合、直接basic_address_fromを呼んで不要な判定を避ける。 addresses_array.map { |address_param| basic_address_from(address_param) } else addresses_array.map { |address_param| extended_address_from(address_param, opt) } end end |