Class: RateCenter::DataSource::SimpleMaps
- Inherits:
-
Object
- Object
- RateCenter::DataSource::SimpleMaps
- Defined in:
- lib/rate_center/data_source/simple_maps.rb
Defined Under Namespace
Classes: Client, ResponseParser
Constant Summary collapse
- COUNTRIES =
[ :us, :ca ].freeze
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#data_directory ⇒ Object
readonly
Returns the value of attribute data_directory.
Instance Method Summary collapse
-
#initialize(**options) ⇒ SimpleMaps
constructor
A new instance of SimpleMaps.
- #load_data!(**options) ⇒ Object
Constructor Details
#initialize(**options) ⇒ SimpleMaps
Returns a new instance of SimpleMaps.
79 80 81 |
# File 'lib/rate_center/data_source/simple_maps.rb', line 79 def initialize(**) @client = .fetch(:client) { Client.new } end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
77 78 79 |
# File 'lib/rate_center/data_source/simple_maps.rb', line 77 def client @client end |
#data_directory ⇒ Object (readonly)
Returns the value of attribute data_directory.
77 78 79 |
# File 'lib/rate_center/data_source/simple_maps.rb', line 77 def data_directory @data_directory end |
Instance Method Details
#load_data!(**options) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/rate_center/data_source/simple_maps.rb', line 83 def load_data!(**) COUNTRIES.each do |country| data_directory = .fetch(:data_directory).join(country.to_s) FileUtils.mkdir_p(data_directory) data = client.fetch_data(country:) cities_by_region = data.each_with_object(Hash.new { |h, k| h[k] = [] }) do |city, result| result[city.region_id] << city end cities_by_region.each do |region, cities| data_file = data_directory.join("#{region.downcase}.json") data = cities.sort_by { |city| [ city.city, city.county_name ] }.map do |city| { "country" => country.to_s.upcase, "region" => city.region_id, "county" => city.county_name, "name" => city.city, "lat" => city.lat, "long" => city.lng } end data_file.write(JSON.pretty_generate("cities" => data)) end end end |