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

27 lines
552 B
Ruby
Raw Normal View History

class ApiAdresse::AddressAdapter
2018-10-15 17:26:06 +02:00
def initialize(address)
@address = address
end
2018-10-15 16:33:42 +02:00
def get_suggestions
2018-10-15 17:26:06 +02:00
@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