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