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

29 lines
629 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 api
@api ||= ApiAdresse::API.new(@address)
2018-10-15 16:33:42 +02:00
end
2018-10-15 17:35:44 +02:00
def convert_api_result_to_point
result = JSON.parse(api.call)
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