Carto::Bano → ApiAdresse
This commit is contained in:
parent
d749d20bb3
commit
dac2019675
18 changed files with 93 additions and 99 deletions
|
@ -2,7 +2,7 @@ class Ban::SearchController < ApplicationController
|
|||
def get
|
||||
request = params[:request]
|
||||
|
||||
json = Carto::Bano::AddressRetriever.new(request).list.map do |value|
|
||||
json = ApiAdresse::AddressRetriever.new(request).list.map do |value|
|
||||
{ label: value }
|
||||
end.to_json
|
||||
|
||||
|
|
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
|
||||
|
|
|
@ -8,14 +8,14 @@ describe Ban::SearchController, type: :controller do
|
|||
subject
|
||||
end
|
||||
|
||||
context 'when request return result', vcr: { cassette_name: 'bano_search_paris' } do
|
||||
context 'when request return result', vcr: { cassette_name: 'api_adresse_search_paris_2' } do
|
||||
let (:request) { 'Paris' }
|
||||
|
||||
it { expect(response.status).to eq 200 }
|
||||
it { expect(response.body).to eq '[{"label":"Paris"},{"label":"Paris 63120 Courpière"},{"label":"PARIS (Vaillac) 46240 Cœur de Causse"},{"label":"Paris 40500 Saint-Sever"},{"label":"Paris Buton 37140 Bourgueil"}]' }
|
||||
end
|
||||
|
||||
context 'when request return nothing', vcr: { cassette_name: 'bano_search_nothing' } do
|
||||
context 'when request return nothing', vcr: { cassette_name: 'api_adresse_search_nothing_2' } do
|
||||
let (:request) { 'je recherche pas grand chose' }
|
||||
|
||||
it { expect(response.status).to eq 200 }
|
||||
|
@ -31,13 +31,13 @@ describe Ban::SearchController, type: :controller do
|
|||
subject
|
||||
end
|
||||
|
||||
context 'when request return result', vcr: { cassette_name: 'ban_search_paris' } do
|
||||
context 'when request return result', vcr: { cassette_name: 'api_adresse_search_paris' } do
|
||||
let(:request) { 'Paris' }
|
||||
|
||||
it { expect(response.body).to eq ({ lon: '2.3469', lat: '48.8589', zoom: '14', dossier_id: dossier_id }).to_json }
|
||||
end
|
||||
|
||||
context 'when request return nothing', vcr: { cassette_name: 'ban_search_nothing' } do
|
||||
context 'when request return nothing', vcr: { cassette_name: 'api_adresse_search_nothing' } do
|
||||
let(:request) { 'je recherche pas grand chose' }
|
||||
|
||||
it { expect(response.body).to eq ({ lon: nil, lat: nil, zoom: '14', dossier_id: dossier_id }).to_json }
|
||||
|
|
|
@ -61,7 +61,7 @@ feature 'As a User I wanna create a dossier' do
|
|||
end
|
||||
end
|
||||
|
||||
scenario 'Identification through siret', vcr: { cassette_name: 'search_ban_paris' }, js: true do
|
||||
scenario 'Identification through siret', vcr: { cassette_name: 'api_adresse_search_paris_3' }, js: true do
|
||||
login_as user, scope: :user
|
||||
visit commencer_path(procedure_path: procedure_with_siret.path)
|
||||
expect(page).to have_current_path(users_dossier_path(procedure_with_siret.dossiers.last.id.to_s))
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Carto::Bano::AddressRetriever do
|
||||
describe ApiAdresse::AddressRetriever do
|
||||
describe '#list' do
|
||||
let(:request) { 'Paris' }
|
||||
let(:response) { File.open('spec/support/files/ban_address_search.json') }
|
|
@ -2,13 +2,13 @@ require 'spec_helper'
|
|||
|
||||
describe Carto::Geocodeur do
|
||||
let(:address) { '50 av des champs elysees' }
|
||||
describe '.convert_adresse_to_point', vcr: { cassette_name: 'bano_octo' } do
|
||||
describe '.convert_adresse_to_point', vcr: { cassette_name: 'api_adresse_octo' } do
|
||||
it 'return a point' do
|
||||
expect(described_class.convert_adresse_to_point(address).class).to eq(RGeo::Cartesian::PointImpl)
|
||||
end
|
||||
context 'when RestClient::Exception' do
|
||||
before do
|
||||
allow_any_instance_of(Carto::Bano::Driver).to receive(:call).and_raise(RestClient::Exception)
|
||||
allow_any_instance_of(ApiAdresse::Driver).to receive(:call).and_raise(RestClient::Exception)
|
||||
end
|
||||
it 'return nil' do
|
||||
expect(described_class.convert_adresse_to_point(address)).to be_nil
|
||||
|
@ -16,7 +16,7 @@ describe Carto::Geocodeur do
|
|||
end
|
||||
context 'when JSON::ParserError' do
|
||||
before do
|
||||
allow_any_instance_of(Carto::Bano::PointRetriever).to receive(:point).and_raise(JSON::ParserError)
|
||||
allow_any_instance_of(ApiAdresse::PointRetriever).to receive(:point).and_raise(JSON::ParserError)
|
||||
end
|
||||
it 'return nil' do
|
||||
expect(described_class.convert_adresse_to_point(address)).to be_nil
|
||||
|
|
Loading…
Reference in a new issue