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

21 lines
475 B
Ruby
Raw Normal View History

2018-10-15 17:26:06 +02:00
class ApiAdresse::PointRetriever
def initialize(address)
@address = address
end
2018-10-15 16:33:42 +02:00
2018-10-15 17:26:06 +02:00
def point
@point ||= convert_api_result_to_point
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_point
result = JSON.parse(ApiAdresse::API.call(@address))
if result['features'].empty?
Rails.logger.error "unable to find location for address #{@address}"
return nil
2018-10-15 16:33:42 +02:00
end
2018-10-15 17:26:06 +02:00
RGeo::GeoJSON.decode(result['features'][0]['geometry'], json_parser: :json)
2018-10-15 16:33:42 +02:00
end
end