Class: JpZipCode::Updater
- Inherits:
-
Object
- Object
- JpZipCode::Updater
- Defined in:
- lib/jp_zip_code/updater.rb
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(dry_run = true) ⇒ Updater
constructor
A new instance of Updater.
- #merge(jp_data, roman_data) ⇒ Object
- #update_file(zip_code_data) ⇒ Object
Constructor Details
Instance Method Details
#execute ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/jp_zip_code/updater.rb', line 11 def execute jp_data = @jp_filer.fetch_data roman_data = @roman_filer.fetch_data merged = merge(jp_data, roman_data) update_file(merged) [@jp_filer, @roman_filer].map(&:clean) true rescue StandardError => e puts "update failed. reason: #{e.}" false end |
#merge(jp_data, roman_data) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/jp_zip_code/updater.rb', line 24 def merge(jp_data, roman_data) jp_data.each_with_object({}) do |(zip_code, data), merged| d = data.dup roman = roman_data[zip_code] d[:pref_roman] = roman ? roman[:pref_roman] : nil d[:city_roman] = roman ? roman[:city_roman] : nil d[:town_roman] = roman ? roman[:town_roman] : nil merged[zip_code] = d end end |
#update_file(zip_code_data) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/jp_zip_code/updater.rb', line 35 def update_file(zip_code_data) unless @dry_run (0..9999).each do |index| top_four = format('%04d', index) d = zip_code_data.select { |data| data.start_with?(top_four) } unless d.empty? File.open("#{File.dirname(__FILE__)}/../../data/zip_code/#{top_four}.json", 'w') do |file| file.puts JSON.generate(d) end end print '.' if index % 100 == 0 end end puts "\nupdate complete." end |