2018-10-15 16:33:42 +02:00
|
|
|
module ApiAdresse
|
|
|
|
class AddressRetriever
|
|
|
|
def initialize(address)
|
|
|
|
@address = address
|
|
|
|
end
|
|
|
|
|
|
|
|
def list
|
2018-10-15 17:35:44 +02:00
|
|
|
@list ||= convert_api_result_to_full_address
|
2018-10-15 16:33:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-10-15 17:35:44 +02:00
|
|
|
def convert_api_result_to_full_address
|
2018-10-15 18:23:32 +02:00
|
|
|
result = JSON.parse(ApiAdresse::API.call(@address, 5))
|
2018-10-15 16:33:42 +02:00
|
|
|
|
|
|
|
if result['features'].empty?
|
|
|
|
Rails.logger.error "unable to find location for address #{@address}"
|
|
|
|
return []
|
|
|
|
end
|
|
|
|
|
|
|
|
result['features'].map do |feature|
|
|
|
|
feature['properties']['label']
|
|
|
|
end
|
|
|
|
rescue TypeError, JSON::ParserError
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|