Method: ZipcodeJa.find

Defined in:
lib/zipcode_ja.rb

.find(zipcode) ⇒ Object



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

def find(zipcode)
  zipcode =~ /^[0-9]{7}$/ or return

  ccc = zipcode[0, 3]
  zip_path = File.join(@here, "../data/zipcode/#{ccc}xxxx.csv")
  FileTest.file?(zip_path) or return

  CSV.foreach(File.open(zip_path)) do |row|
    if zipcode == row[2]
      return {
        :zipcode         => row[2],
        :prefecture_yomi => row[3],
        :city_yomi       => row[4],
        :town_yomi       => row[5],
        :prefecture      => row[6],
        :city            => row[7],
        :town            => row[8],
      }
    end
  end
end