Use geocoder

This commit is contained in:
Paul Chavard 2020-01-14 19:00:17 +01:00
parent 14295db9ad
commit 4edc7b00cf
6 changed files with 43 additions and 3 deletions

View file

@ -29,6 +29,7 @@ gem 'flipper-active_record'
gem 'flipper-ui'
gem 'fog-openstack'
gem 'font-awesome-rails'
gem 'geocoder'
gem 'gon'
gem 'graphiql-rails'
gem 'graphql'

View file

@ -232,6 +232,7 @@ GEM
font-awesome-rails (4.7.0.4)
railties (>= 3.2, < 6.0)
formatador (0.2.5)
geocoder (1.6.0)
globalid (0.4.2)
activesupport (>= 4.2.0)
gon (6.2.1)
@ -743,6 +744,7 @@ DEPENDENCIES
flipper-ui
fog-openstack
font-awesome-rails
geocoder
gon
graphiql-rails
graphql

View file

@ -343,7 +343,7 @@ class Dossier < ApplicationRecord
def geo_position
if etablissement.present?
point = ApiAdresse::PointAdapter.new(etablissement.geo_adresse).geocode
point = Geocoder.search(etablissement.geo_adresse).first
end
lon = "2.428462"
@ -351,8 +351,7 @@ class Dossier < ApplicationRecord
zoom = "13"
if point.present?
lon = point.x.to_s
lat = point.y.to_s
lat, lon = point.coordinates.map(&:to_s)
end
{ lon: lon, lat: lat, zoom: zoom }

View file

@ -0,0 +1 @@
Geocoder.configure(lookup: :ban_data_gouv_fr)

View file

@ -1084,4 +1084,39 @@ describe Dossier do
expect { expired_brouillon.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
describe '#geo_position' do
let(:lat) { "46.538192" }
let(:lon) { "2.428462" }
let(:zoom) { "13" }
let(:etablissement_geo_adresse_lat) { "40.7143528" }
let(:etablissement_geo_adresse_lon) { "-74.0059731" }
let(:result) { { lat: lat, lon: lon, zoom: zoom } }
let(:dossier) { create(:dossier) }
it 'should geolocate' do
expect(dossier.geo_position).to eq(result)
end
context 'with etablissement' do
before do
Geocoder::Lookup::Test.add_stub(
dossier.etablissement.geo_adresse, [
{
'coordinates' => [etablissement_geo_adresse_lat.to_f, etablissement_geo_adresse_lon.to_f]
}
]
)
end
let(:dossier) { create(:dossier, :with_entreprise) }
let(:result) { { lat: etablissement_geo_adresse_lat, lon: etablissement_geo_adresse_lon, zoom: zoom } }
it 'should geolocate' do
expect(dossier.geo_position).to eq(result)
end
end
end
end

View file

@ -152,6 +152,8 @@ RSpec.configure do |config|
ActionMailer::Base.deliveries.clear
ActiveStorage::Current.host = 'http://test.host'
Geocoder.configure(lookup: :test)
}
RSpec::Matchers.define :have_same_attributes_as do |expected, options|