Module: JapaneseAddressParser::CsvParser

Defined in:
lib/japanese_address_parser/csv_parser.rb

Constant Summary collapse

HEADER_MAP =
{
  '都道府県コード' => :prefecture_code,
  '都道府県名' => :prefecture_name,
  '都道府県名カナ' => :prefecture_name_kana,
  '都道府県名ローマ字' => :prefecture_romaji,
  '市区町村コード' => :city_code,
  '市区町村名' => :city_name,
  '市区町村名カナ' => :city_name_kana,
  '市区町村名ローマ字' => :city_romaji,
  '大字町丁目名' => :town_name,
  '大字町丁目名カナ' => :town_name_kana,
  '大字町丁目名ローマ字' => :town_name_romaji,
  '小字・通称名' => :town_nickname,
  '緯度' => :latitude,
  '経度' => :longitude
}.freeze

Class Method Summary collapse

Class Method Details

.callObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/japanese_address_parser/csv_parser.rb', line 29

def call
  filepath = Pathname(__FILE__).dirname.join('../../geolonia-japanese-addresses/data/latest.csv')
  header_converters = proc { |h| ::JapaneseAddressParser::CsvParser::HEADER_MAP[h] }
  data = ::CSV.table(filepath, header_converters: header_converters, converters: nil)

  prefectures = {}

  data.each do |row|
    current_prefecture = _find_or_build_prefecture(prefectures.keys, row)
    prefectures[current_prefecture] ||= {}

    current_city = _find_or_build_city(prefectures[current_prefecture].keys, current_prefecture, row)
    prefectures[current_prefecture][current_city] ||= []

    stored_town = _find_town(prefectures[current_prefecture][current_city], row)
    stored_town.nil? && (prefectures[current_prefecture][current_city] << _build_town(row))
  end

  _write_csv(prefectures)
end