Class: TimeZoneConverter::JsonDataTransformer
- Inherits:
-
Object
- Object
- TimeZoneConverter::JsonDataTransformer
- Defined in:
- lib/time_zone_converter/json_data_transformer.rb
Constant Summary collapse
- DATA_PATH =
'data/cities'
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(path: DATA_PATH) ⇒ JsonDataTransformer
constructor
A new instance of JsonDataTransformer.
Constructor Details
#initialize(path: DATA_PATH) ⇒ JsonDataTransformer
Returns a new instance of JsonDataTransformer.
8 9 10 |
# File 'lib/time_zone_converter/json_data_transformer.rb', line 8 def initialize(path: DATA_PATH) @path = path end |
Instance Method Details
#call ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/time_zone_converter/json_data_transformer.rb', line 12 def call json_new = Hash.new { |hash, key| hash[key] = {} } Dir["#{@path}/*.json"].each do |json_file| json = JSON.parse(File.read(json_file)) json.each do |item| data = item.last next unless data.is_a? Hash accentcity = data['accentcity'] if json_new[accentcity].present? puts "#{accentcity} already exists." population = data['population'] population_to_i = population.to_i || 0 previous_population_to_i = json_new[accentcity].try(:last).to_i || 0 if previous_population_to_i < population_to_i puts "Overriding #{accentcity}." puts "Population was #{previous_population_to_i}." puts "Population is #{population_to_i}." # sleep 0.1 json_new[accentcity] = [ data['latitude'], data['longitude'], population ] end else puts "#{accentcity} will be created." json_new[accentcity] = [ data['latitude'], data['longitude'], data['population'] ] end # sleep 0.05 end end puts "Storing #{json_new.keys.count} cities." File.open("#{@path}/cities.json", "w") do |f| f.write(json_new.to_json) end end |