Class: TaxJp::Addresses::DbBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/tax_jp/addresses/db_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(db_path = nil) ⇒ DbBuilder

Returns a new instance of DbBuilder.



5
6
7
# File 'lib/tax_jp/addresses/db_builder.rb', line 5

def initialize(db_path = nil)
  @db_path = db_path || TaxJp::Address::DB_PATH
end

Instance Method Details

#run(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tax_jp/addresses/db_builder.rb', line 9

def run(options = {})
  with_database(options) do |db|
    puts
    prefecture_code = nil
    CSV.foreach(File.join('data', '住所', 'addresses.csv')) do |line|
      if prefecture_code != line[0][0..1]
        prefecture_code = line[0][0..1]
        puts prefecture_code
      end

      zip_code = line[2]
      prefecture_name = line[6]
      city = line[7]
      section = line[8] == '以下に掲載がない場合' ? '' : line[8]

      section = section.gsub(/(.*/, '')
      if section.end_with?('地割)') or section.end_with?('地割')
        next
      end

      row = [zip_code, prefecture_code, prefecture_name, city, section]
      db.execute(insert_sql, row)
    end
    puts
  end
end