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

25 lines
585 B
Ruby
Raw Normal View History

2018-10-15 16:33:42 +02:00
module ApiAdresse
# input : address
# output : point RGeo::Cartesian::PointImpl
class PointRetriever
def initialize(address)
@address = address
end
def point
2018-10-15 17:35:44 +02:00
@point ||= convert_api_result_to_point
2018-10-15 16:33:42 +02:00
end
private
2018-10-15 17:35:44 +02:00
def convert_api_result_to_point
2018-10-15 18:23:32 +02:00
result = JSON.parse(ApiAdresse::API.call(@address))
2018-10-15 16:33:42 +02:00
if result['features'].empty?
Rails.logger.error "unable to find location for address #{@address}"
return nil
end
RGeo::GeoJSON.decode(result['features'][0]['geometry'], json_parser: :json)
end
end
end