demarches-normaliennes/app/lib/api_adresse/address_retriever.rb

27 lines
543 B
Ruby
Raw Normal View History

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