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

23 lines
477 B
Ruby
Raw Normal View History

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