Carto::Bano → ApiAdresse
This commit is contained in:
parent
d749d20bb3
commit
dac2019675
18 changed files with 93 additions and 99 deletions
34
app/lib/api_adresse/address_retriever.rb
Normal file
34
app/lib/api_adresse/address_retriever.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
module ApiAdresse
|
||||
# input : address
|
||||
# output : Array List label address
|
||||
class AddressRetriever
|
||||
def initialize(address)
|
||||
@address = address
|
||||
end
|
||||
|
||||
def list
|
||||
@list ||= convert_driver_result_to_full_address
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def driver
|
||||
@driver ||= ApiAdresse::Driver.new @address, 5
|
||||
end
|
||||
|
||||
def convert_driver_result_to_full_address
|
||||
result = JSON.parse(driver.call)
|
||||
|
||||
if result['features'].empty?
|
||||
Rails.logger.error "unable to find location for address #{@address}"
|
||||
return []
|
||||
end
|
||||
|
||||
result['features'].map do |feature|
|
||||
feature['properties']['label']
|
||||
end
|
||||
rescue TypeError, JSON::ParserError
|
||||
[]
|
||||
end
|
||||
end
|
||||
end
|
20
app/lib/api_adresse/driver.rb
Normal file
20
app/lib/api_adresse/driver.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
module ApiAdresse
|
||||
# input : string (address)
|
||||
# output : json
|
||||
class Driver
|
||||
def initialize(address, limit = 1)
|
||||
@address = address
|
||||
@limit = limit
|
||||
end
|
||||
|
||||
def call
|
||||
RestClient.get api_url, params: { q: @address, limit: @limit }
|
||||
rescue RestClient::ServiceUnavailable
|
||||
nil
|
||||
end
|
||||
|
||||
def api_url
|
||||
'http://api-adresse.data.gouv.fr/search'
|
||||
end
|
||||
end
|
||||
end
|
28
app/lib/api_adresse/point_retriever.rb
Normal file
28
app/lib/api_adresse/point_retriever.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
module ApiAdresse
|
||||
# input : address
|
||||
# output : point RGeo::Cartesian::PointImpl
|
||||
class PointRetriever
|
||||
def initialize(address)
|
||||
@address = address
|
||||
end
|
||||
|
||||
def point
|
||||
@point ||= convert_driver_result_to_point
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def driver
|
||||
@driver ||= ApiAdresse::Driver.new @address
|
||||
end
|
||||
|
||||
def convert_driver_result_to_point
|
||||
result = JSON.parse(driver.call)
|
||||
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
|
|
@ -1,36 +0,0 @@
|
|||
module Carto
|
||||
module Bano
|
||||
# input : address
|
||||
# output : Array List label address
|
||||
class AddressRetriever
|
||||
def initialize(address)
|
||||
@address = address
|
||||
end
|
||||
|
||||
def list
|
||||
@list ||= convert_driver_result_to_full_address
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def driver
|
||||
@driver ||= Carto::Bano::Driver.new @address, 5
|
||||
end
|
||||
|
||||
def convert_driver_result_to_full_address
|
||||
result = JSON.parse(driver.call)
|
||||
|
||||
if result['features'].empty?
|
||||
Rails.logger.error "unable to find location for address #{@address}"
|
||||
return []
|
||||
end
|
||||
|
||||
result['features'].map do |feature|
|
||||
feature['properties']['label']
|
||||
end
|
||||
rescue TypeError, JSON::ParserError
|
||||
[]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,22 +0,0 @@
|
|||
module Carto
|
||||
module Bano
|
||||
# input : string (address)
|
||||
# output : json
|
||||
class Driver
|
||||
def initialize(address, limit = 1)
|
||||
@address = address
|
||||
@limit = limit
|
||||
end
|
||||
|
||||
def call
|
||||
RestClient.get api_url, params: { q: @address, limit: @limit }
|
||||
rescue RestClient::ServiceUnavailable
|
||||
nil
|
||||
end
|
||||
|
||||
def api_url
|
||||
'http://api-adresse.data.gouv.fr/search'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,30 +0,0 @@
|
|||
module Carto
|
||||
module Bano
|
||||
# input : address
|
||||
# output : point RGeo::Cartesian::PointImpl
|
||||
class PointRetriever
|
||||
def initialize(address)
|
||||
@address = address
|
||||
end
|
||||
|
||||
def point
|
||||
@point ||= convert_driver_result_to_point
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def driver
|
||||
@driver ||= Carto::Bano::Driver.new @address
|
||||
end
|
||||
|
||||
def convert_driver_result_to_point
|
||||
result = JSON.parse(driver.call)
|
||||
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
|
||||
end
|
|
@ -2,7 +2,7 @@ module Carto
|
|||
# this class take a string in input and return a point
|
||||
class Geocodeur
|
||||
def self.convert_adresse_to_point(address)
|
||||
Carto::Bano::PointRetriever.new(address).point
|
||||
ApiAdresse::PointRetriever.new(address).point
|
||||
rescue RestClient::Exception, JSON::ParserError => e
|
||||
Rails.logger.error e.message
|
||||
nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue