Merge pull request #2836 from betagouv/carto-lib-3

Carto lib 3
This commit is contained in:
gregoirenovel 2018-10-16 09:38:16 +02:00 committed by GitHub
commit b4911b8a70
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 104 additions and 94 deletions

View file

@ -2,7 +2,7 @@ class Ban::SearchController < ApplicationController
def get
request = params[:request]
json = ApiAdresse::AddressRetriever.new(request).list.map do |value|
json = ApiAdresse::AddressAdapter.new(request).get_suggestions.map do |value|
{ label: value }
end.to_json
@ -10,7 +10,9 @@ class Ban::SearchController < ApplicationController
end
def get_address_point
point = ApiAdresse::Geocodeur.convert_adresse_to_point(params[:request])
request = params[:request]
point = ApiAdresse::PointAdapter.new(request).geocode
if point.present?
lon = point.x.to_s

View file

@ -0,0 +1,33 @@
class ApiAdresse::Adapter
private
def initialize(address, limit, blank_return)
@address = address
@limit = limit
@blank_return = blank_return
end
def features
@features ||= get_features
end
def get_features
response = ApiAdresse::API.call(@address, @limit)
result = JSON.parse(response)
result['features']
rescue RestClient::Exception, JSON::ParserError, TypeError
@blank_return
end
def handle_result
if features.present?
process_features
else
@blank_return
end
end
def process_features
raise NoMethodError
end
end

View file

@ -0,0 +1,17 @@
class ApiAdresse::AddressAdapter < ApiAdresse::Adapter
def initialize(address)
super(address, 5, [])
end
def get_suggestions
handle_result
end
private
def process_features
features.map do |feature|
feature['properties']['label']
end
end
end

View file

@ -1,26 +0,0 @@
class ApiAdresse::AddressRetriever
def initialize(address)
@address = address
end
def list
@list ||= convert_api_result_to_full_address
end
private
def convert_api_result_to_full_address
result = JSON.parse(ApiAdresse::API.call(@address, 5))
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

View file

@ -1,8 +0,0 @@
class ApiAdresse::Geocodeur
def self.convert_adresse_to_point(address)
ApiAdresse::PointRetriever.new(address).point
rescue RestClient::Exception, JSON::ParserError => e
Rails.logger.error(e.message)
nil
end
end

View file

@ -0,0 +1,15 @@
class ApiAdresse::PointAdapter < ApiAdresse::Adapter
def initialize(address)
super(address, 1, nil)
end
def geocode
handle_result
end
private
def process_features
RGeo::GeoJSON.decode(features[0]['geometry'], json_parser: :json)
end
end

View file

@ -1,22 +0,0 @@
class ApiAdresse::PointRetriever
def initialize(address)
@address = address
end
def point
@point ||= convert_api_result_to_point
end
private
def convert_api_result_to_point
result = JSON.parse(ApiAdresse::API.call(@address))
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

View file

@ -1,7 +1,4 @@
class ApiCarto::API
def initialize
end
def self.search_qp(geojson)
url = [API_CARTO_URL, "quartiers-prioritaires", "search"].join("/")
call(url, geojson)

View file

@ -6,9 +6,6 @@ class ApiEntreprise::API
TIMEOUT = 15
def initialize
end
def self.entreprise(siren, procedure_id)
call(ENTREPRISE_RESOURCE_NAME, siren, procedure_id)
end

View file

@ -243,7 +243,7 @@ class Dossier < ApplicationRecord
def geo_position
if etablissement.present?
point = ApiAdresse::Geocodeur.convert_adresse_to_point(etablissement.geo_adresse)
point = ApiAdresse::PointAdapter.new(etablissement.geo_adresse).geocode
end
lon = "2.428462"

View file

@ -1,12 +1,12 @@
require 'spec_helper'
describe ApiAdresse::AddressRetriever do
describe '#list' do
describe ApiAdresse::AddressAdapter do
describe '#get_suggestions' do
let(:request) { 'Paris' }
let(:response) { File.open('spec/support/files/api_adresse/search_results.json') }
let(:status) { 200 }
subject { described_class.new(request).list }
subject { described_class.new(request).get_suggestions }
before do
stub_request(:get, "https://api-adresse.data.gouv.fr/search?&q=#{request}&limit=5")

View file

@ -1,26 +0,0 @@
require 'spec_helper'
describe ApiAdresse::Geocodeur do
let(:address) { '50 av des champs elysees' }
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(ApiAdresse::API).to receive(:call).and_raise(RestClient::Exception)
end
it 'return nil' do
expect(described_class.convert_adresse_to_point(address)).to be_nil
end
end
context 'when JSON::ParserError' do
before do
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
end
end
end
end

View file

@ -0,0 +1,31 @@
require 'spec_helper'
describe ApiAdresse::PointAdapter do
let(:address) { '50 av des champs elysees' }
describe '.geocode', vcr: { cassette_name: 'api_adresse_octo' } do
it 'return a point' do
expect(described_class.new(address).geocode.class).to eq(RGeo::Cartesian::PointImpl)
end
context 'when RestClient::Exception' do
before do
allow(ApiAdresse::API).to receive(:call).and_raise(RestClient::Exception)
end
it 'return nil' do
expect(described_class.new(address).geocode).to be_nil
end
end
context 'when JSON::ParserError' do
before do
allow(JSON).to receive(:parse).and_raise(JSON::ParserError)
end
it 'return nil' do
expect(described_class.new(address).geocode).to be_nil
end
end
end
end