Class: JpAddress::Zipcode

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/jp_address/zipcode.rb

Constant Summary collapse

MASTER_CSV_PATH =
'tmp/ken_all.csv'

Class Method Summary collapse

Class Method Details

.download_master_file_from_japanpostObject



6
7
8
9
10
11
# File 'app/models/jp_address/zipcode.rb', line 6

def self.download_master_file_from_japanpost
  _setup_directory
  zip = _save_zip(_request_to_japanpost.body)
  _save_csv(zip.path)
  :success if File.exist?(MASTER_CSV_PATH)
end

.load_master_data(csv_path = MASTER_CSV_PATH) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/jp_address/zipcode.rb', line 49

def self.load_master_data(csv_path = MASTER_CSV_PATH)
  download_master_file_from_japanpost if !File.exist?(csv_path)
  _clear_table
  CSV.foreach(csv_path, encoding:'SJIS:UTF-8') do |row|
    connection.execute(
      sanitize_sql(
        ["insert into jp_address_zipcodes (zip, prefecture, city, town)
          values ('%s', '%s', '%s', '%s')", row[2], row[6], row[7], _remove_needless_words(row[8])
        ]
      )
    )
  end
  _remove_csv
end