From 517c0ad3fc85e6e0b416004563af2e66c0402651 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Mon, 15 Oct 2018 17:43:08 +0200 Subject: [PATCH 1/9] =?UTF-8?q?ApiAdresse::AddressRetriever=20=E2=86=92=20?= =?UTF-8?q?ApiAdresse::AddressAdapter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/ban/search_controller.rb | 2 +- .../api_adresse/{address_retriever.rb => address_adapter.rb} | 2 +- .../{address_retriever_spec.rb => address_adapter_spec.rb} | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename app/lib/api_adresse/{address_retriever.rb => address_adapter.rb} (93%) rename spec/lib/api_adresse/{address_retriever_spec.rb => address_adapter_spec.rb} (96%) diff --git a/app/controllers/ban/search_controller.rb b/app/controllers/ban/search_controller.rb index 1476ec9fe..dac17982b 100644 --- a/app/controllers/ban/search_controller.rb +++ b/app/controllers/ban/search_controller.rb @@ -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).list.map do |value| { label: value } end.to_json diff --git a/app/lib/api_adresse/address_retriever.rb b/app/lib/api_adresse/address_adapter.rb similarity index 93% rename from app/lib/api_adresse/address_retriever.rb rename to app/lib/api_adresse/address_adapter.rb index 735fe5bad..ce1ccffd5 100644 --- a/app/lib/api_adresse/address_retriever.rb +++ b/app/lib/api_adresse/address_adapter.rb @@ -1,4 +1,4 @@ -class ApiAdresse::AddressRetriever +class ApiAdresse::AddressAdapter def initialize(address) @address = address end diff --git a/spec/lib/api_adresse/address_retriever_spec.rb b/spec/lib/api_adresse/address_adapter_spec.rb similarity index 96% rename from spec/lib/api_adresse/address_retriever_spec.rb rename to spec/lib/api_adresse/address_adapter_spec.rb index 0aada2445..bea5369e6 100644 --- a/spec/lib/api_adresse/address_retriever_spec.rb +++ b/spec/lib/api_adresse/address_adapter_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe ApiAdresse::AddressRetriever do +describe ApiAdresse::AddressAdapter do describe '#list' do let(:request) { 'Paris' } let(:response) { File.open('spec/support/files/api_adresse/search_results.json') } From 7af41b35a273dacb540c518e24b626dac3331fbd Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Mon, 15 Oct 2018 21:53:52 +0200 Subject: [PATCH 2/9] =?UTF-8?q?ApiAdresse::AddressAdapter#list=20=E2=86=92?= =?UTF-8?q?=20#get=5Fsuggestions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/ban/search_controller.rb | 2 +- app/lib/api_adresse/address_adapter.rb | 2 +- spec/lib/api_adresse/address_adapter_spec.rb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/ban/search_controller.rb b/app/controllers/ban/search_controller.rb index dac17982b..5f3031865 100644 --- a/app/controllers/ban/search_controller.rb +++ b/app/controllers/ban/search_controller.rb @@ -2,7 +2,7 @@ class Ban::SearchController < ApplicationController def get request = params[:request] - json = ApiAdresse::AddressAdapter.new(request).list.map do |value| + json = ApiAdresse::AddressAdapter.new(request).get_suggestions.map do |value| { label: value } end.to_json diff --git a/app/lib/api_adresse/address_adapter.rb b/app/lib/api_adresse/address_adapter.rb index ce1ccffd5..747899137 100644 --- a/app/lib/api_adresse/address_adapter.rb +++ b/app/lib/api_adresse/address_adapter.rb @@ -3,7 +3,7 @@ class ApiAdresse::AddressAdapter @address = address end - def list + def get_suggestions @list ||= convert_api_result_to_full_address end diff --git a/spec/lib/api_adresse/address_adapter_spec.rb b/spec/lib/api_adresse/address_adapter_spec.rb index bea5369e6..f94a3caa1 100644 --- a/spec/lib/api_adresse/address_adapter_spec.rb +++ b/spec/lib/api_adresse/address_adapter_spec.rb @@ -1,12 +1,12 @@ require 'spec_helper' describe ApiAdresse::AddressAdapter do - describe '#list' 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") From 81d2b27160447a3bfc7f68c822560274e9a40799 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Mon, 15 Oct 2018 23:21:22 +0200 Subject: [PATCH 3/9] Refactor ApiAdresse::AddressAdapter --- app/lib/api_adresse/adapter.rb | 33 ++++++++++++++++++++++++++ app/lib/api_adresse/address_adapter.rb | 19 ++++----------- 2 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 app/lib/api_adresse/adapter.rb diff --git a/app/lib/api_adresse/adapter.rb b/app/lib/api_adresse/adapter.rb new file mode 100644 index 000000000..cd012357b --- /dev/null +++ b/app/lib/api_adresse/adapter.rb @@ -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 diff --git a/app/lib/api_adresse/address_adapter.rb b/app/lib/api_adresse/address_adapter.rb index 747899137..68e737cfe 100644 --- a/app/lib/api_adresse/address_adapter.rb +++ b/app/lib/api_adresse/address_adapter.rb @@ -1,26 +1,17 @@ -class ApiAdresse::AddressAdapter +class ApiAdresse::AddressAdapter < ApiAdresse::Adapter def initialize(address) - @address = address + super(address, 5, []) end def get_suggestions - @list ||= convert_api_result_to_full_address + handle_result 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| + def process_features + features.map do |feature| feature['properties']['label'] end - rescue TypeError, JSON::ParserError - [] end end From af716fa3c641a71d9f1ae034654f0919bbd53b1d Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Mon, 15 Oct 2018 18:08:13 +0200 Subject: [PATCH 4/9] =?UTF-8?q?ApiAdresse::PointRetriever=20=E2=86=92=20Ap?= =?UTF-8?q?iAdresse::PointAdapter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/lib/api_adresse/geocodeur.rb | 2 +- app/lib/api_adresse/{point_retriever.rb => point_adapter.rb} | 2 +- spec/lib/api_adresse/geocodeur_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename app/lib/api_adresse/{point_retriever.rb => point_adapter.rb} (93%) diff --git a/app/lib/api_adresse/geocodeur.rb b/app/lib/api_adresse/geocodeur.rb index 8c252998d..d2cf40607 100644 --- a/app/lib/api_adresse/geocodeur.rb +++ b/app/lib/api_adresse/geocodeur.rb @@ -1,6 +1,6 @@ class ApiAdresse::Geocodeur def self.convert_adresse_to_point(address) - ApiAdresse::PointRetriever.new(address).point + ApiAdresse::PointAdapter.new(address).point rescue RestClient::Exception, JSON::ParserError => e Rails.logger.error(e.message) nil diff --git a/app/lib/api_adresse/point_retriever.rb b/app/lib/api_adresse/point_adapter.rb similarity index 93% rename from app/lib/api_adresse/point_retriever.rb rename to app/lib/api_adresse/point_adapter.rb index bd0b41c89..2cab33304 100644 --- a/app/lib/api_adresse/point_retriever.rb +++ b/app/lib/api_adresse/point_adapter.rb @@ -1,4 +1,4 @@ -class ApiAdresse::PointRetriever +class ApiAdresse::PointAdapter def initialize(address) @address = address end diff --git a/spec/lib/api_adresse/geocodeur_spec.rb b/spec/lib/api_adresse/geocodeur_spec.rb index eba4d9e6a..5f8e0ec34 100644 --- a/spec/lib/api_adresse/geocodeur_spec.rb +++ b/spec/lib/api_adresse/geocodeur_spec.rb @@ -16,7 +16,7 @@ describe ApiAdresse::Geocodeur do end context 'when JSON::ParserError' do before do - allow_any_instance_of(ApiAdresse::PointRetriever).to receive(:point).and_raise(JSON::ParserError) + allow_any_instance_of(ApiAdresse::PointAdapter).to receive(:point).and_raise(JSON::ParserError) end it 'return nil' do expect(described_class.convert_adresse_to_point(address)).to be_nil From 145880555e1291d2a61ff9f86619b568b216e0e0 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Mon, 15 Oct 2018 22:37:59 +0200 Subject: [PATCH 5/9] =?UTF-8?q?ApiAdresse::PointAdapter#point=20=E2=86=92?= =?UTF-8?q?=20#geocode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/lib/api_adresse/geocodeur.rb | 2 +- app/lib/api_adresse/point_adapter.rb | 2 +- spec/lib/api_adresse/geocodeur_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/lib/api_adresse/geocodeur.rb b/app/lib/api_adresse/geocodeur.rb index d2cf40607..8e2dc19c9 100644 --- a/app/lib/api_adresse/geocodeur.rb +++ b/app/lib/api_adresse/geocodeur.rb @@ -1,6 +1,6 @@ class ApiAdresse::Geocodeur def self.convert_adresse_to_point(address) - ApiAdresse::PointAdapter.new(address).point + ApiAdresse::PointAdapter.new(address).geocode rescue RestClient::Exception, JSON::ParserError => e Rails.logger.error(e.message) nil diff --git a/app/lib/api_adresse/point_adapter.rb b/app/lib/api_adresse/point_adapter.rb index 2cab33304..a8caa1e16 100644 --- a/app/lib/api_adresse/point_adapter.rb +++ b/app/lib/api_adresse/point_adapter.rb @@ -3,7 +3,7 @@ class ApiAdresse::PointAdapter @address = address end - def point + def geocode @point ||= convert_api_result_to_point end diff --git a/spec/lib/api_adresse/geocodeur_spec.rb b/spec/lib/api_adresse/geocodeur_spec.rb index 5f8e0ec34..6094caed0 100644 --- a/spec/lib/api_adresse/geocodeur_spec.rb +++ b/spec/lib/api_adresse/geocodeur_spec.rb @@ -16,7 +16,7 @@ describe ApiAdresse::Geocodeur do end context 'when JSON::ParserError' do before do - allow_any_instance_of(ApiAdresse::PointAdapter).to receive(:point).and_raise(JSON::ParserError) + allow_any_instance_of(ApiAdresse::PointAdapter).to receive(:geocode).and_raise(JSON::ParserError) end it 'return nil' do expect(described_class.convert_adresse_to_point(address)).to be_nil From d68aca545a362754d555ded956afe3400a0008df Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Mon, 15 Oct 2018 21:59:13 +0200 Subject: [PATCH 6/9] Refactor ApiAddresse::PointAdapter --- app/lib/api_adresse/point_adapter.rb | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/app/lib/api_adresse/point_adapter.rb b/app/lib/api_adresse/point_adapter.rb index a8caa1e16..ea54079e6 100644 --- a/app/lib/api_adresse/point_adapter.rb +++ b/app/lib/api_adresse/point_adapter.rb @@ -1,22 +1,15 @@ -class ApiAdresse::PointAdapter +class ApiAdresse::PointAdapter < ApiAdresse::Adapter def initialize(address) - @address = address + super(address, 1, nil) end def geocode - @point ||= convert_api_result_to_point + handle_result 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) + def process_features + RGeo::GeoJSON.decode(features[0]['geometry'], json_parser: :json) end end From 22088b167def62c08fb3ca06c9bf0aef634f0742 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Mon, 15 Oct 2018 21:48:17 +0200 Subject: [PATCH 7/9] Remove ApiAdresse::Geocodeur --- app/controllers/ban/search_controller.rb | 4 +++- app/lib/api_adresse/geocodeur.rb | 8 ------- app/models/dossier.rb | 2 +- spec/lib/api_adresse/geocodeur_spec.rb | 26 --------------------- spec/lib/api_adresse/point_adapter_spec.rb | 27 ++++++++++++++++++++++ 5 files changed, 31 insertions(+), 36 deletions(-) delete mode 100644 app/lib/api_adresse/geocodeur.rb delete mode 100644 spec/lib/api_adresse/geocodeur_spec.rb create mode 100644 spec/lib/api_adresse/point_adapter_spec.rb diff --git a/app/controllers/ban/search_controller.rb b/app/controllers/ban/search_controller.rb index 5f3031865..e45a4177e 100644 --- a/app/controllers/ban/search_controller.rb +++ b/app/controllers/ban/search_controller.rb @@ -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 diff --git a/app/lib/api_adresse/geocodeur.rb b/app/lib/api_adresse/geocodeur.rb deleted file mode 100644 index 8e2dc19c9..000000000 --- a/app/lib/api_adresse/geocodeur.rb +++ /dev/null @@ -1,8 +0,0 @@ -class ApiAdresse::Geocodeur - def self.convert_adresse_to_point(address) - ApiAdresse::PointAdapter.new(address).geocode - rescue RestClient::Exception, JSON::ParserError => e - Rails.logger.error(e.message) - nil - end -end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index c7678231b..dbbdaf26f 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -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" diff --git a/spec/lib/api_adresse/geocodeur_spec.rb b/spec/lib/api_adresse/geocodeur_spec.rb deleted file mode 100644 index 6094caed0..000000000 --- a/spec/lib/api_adresse/geocodeur_spec.rb +++ /dev/null @@ -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::PointAdapter).to receive(:geocode).and_raise(JSON::ParserError) - end - it 'return nil' do - expect(described_class.convert_adresse_to_point(address)).to be_nil - end - end - end -end diff --git a/spec/lib/api_adresse/point_adapter_spec.rb b/spec/lib/api_adresse/point_adapter_spec.rb new file mode 100644 index 000000000..52841543b --- /dev/null +++ b/spec/lib/api_adresse/point_adapter_spec.rb @@ -0,0 +1,27 @@ +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 From f66f0f96683df9a3098b8de2d04b7e3aa198bf8b Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Mon, 15 Oct 2018 18:16:16 +0200 Subject: [PATCH 8/9] Remove two useless initialize methods --- app/lib/api_carto/api.rb | 3 --- app/lib/api_entreprise/api.rb | 3 --- 2 files changed, 6 deletions(-) diff --git a/app/lib/api_carto/api.rb b/app/lib/api_carto/api.rb index 230fed038..817aee9a8 100644 --- a/app/lib/api_carto/api.rb +++ b/app/lib/api_carto/api.rb @@ -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) diff --git a/app/lib/api_entreprise/api.rb b/app/lib/api_entreprise/api.rb index 0abc968da..32fe52114 100644 --- a/app/lib/api_entreprise/api.rb +++ b/app/lib/api_entreprise/api.rb @@ -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 From 0018326451b6365523f9a89fc29b33f4c9934186 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Mon, 15 Oct 2018 22:23:50 +0200 Subject: [PATCH 9/9] Add some spacing to specs --- spec/lib/api_adresse/point_adapter_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/lib/api_adresse/point_adapter_spec.rb b/spec/lib/api_adresse/point_adapter_spec.rb index 52841543b..9f21c8dd5 100644 --- a/spec/lib/api_adresse/point_adapter_spec.rb +++ b/spec/lib/api_adresse/point_adapter_spec.rb @@ -7,18 +7,22 @@ describe ApiAdresse::PointAdapter 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