Class: RateCenter::DataSource::SimpleMaps::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rate_center/data_source/simple_maps.rb

Constant Summary collapse

HOST =
"https://simplemaps.com/".freeze
DATA_FILES =
{
  us: OpenStruct.new(
    path: "static/data/us-cities/1.79/basic/simplemaps_uscities_basicv1.79.zip",
    database_filename: "uscities.csv",
  ),
  ca: OpenStruct.new(
    path: "static/data/canada-cities/1.8/basic/simplemaps_canadacities_basicv1.8.zip",
    database_filename: "canadacities.csv"
  )
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Client

Returns a new instance of Client.



52
53
54
55
56
# File 'lib/rate_center/data_source/simple_maps.rb', line 52

def initialize(**options)
  @host = options.fetch(:host, HOST)
  @http_client = options.fetch(:http_client) { default_http_client }
  @response_parser = options.fetch(:response_parser) { ResponseParser.new }
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



50
51
52
# File 'lib/rate_center/data_source/simple_maps.rb', line 50

def host
  @host
end

#http_clientObject (readonly)

Returns the value of attribute http_client.



50
51
52
# File 'lib/rate_center/data_source/simple_maps.rb', line 50

def http_client
  @http_client
end

#response_parserObject (readonly)

Returns the value of attribute response_parser.



50
51
52
# File 'lib/rate_center/data_source/simple_maps.rb', line 50

def response_parser
  @response_parser
end

Instance Method Details

#fetch_data(**options) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/rate_center/data_source/simple_maps.rb', line 58

def fetch_data(**options)
  path = options.fetch(:path) { DATA_FILES.fetch(options.fetch(:country)).path }
  database_filename = options.fetch(:database_filename) { DATA_FILES.fetch(options.fetch(:country)).database_filename }
  uri = URI(path)
  response = http_client.get(uri)
  response_parser.parse(response.body, database_filename:)
end