add adapter for sgmap api carto
This commit is contained in:
parent
edc4f7dcd9
commit
5fd78b3fcb
2 changed files with 62 additions and 0 deletions
26
lib/carto/sgmap/quartier_prioritaire_adapter.rb
Normal file
26
lib/carto/sgmap/quartier_prioritaire_adapter.rb
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
class CARTO::SGMAP::QuartierPrioritaireAdapter
|
||||||
|
def initialize(coordinates)
|
||||||
|
@coordinates = GeojsonService.to_polygon(coordinates)
|
||||||
|
end
|
||||||
|
|
||||||
|
def data_source
|
||||||
|
@data_source ||= JSON.parse(CARTO::SGMAP::API.search_qp(@coordinates), symbolize_names: true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_params
|
||||||
|
params = {}
|
||||||
|
|
||||||
|
data_source[:features].each_with_index do |feature, index|
|
||||||
|
params[index] = feature[:properties]
|
||||||
|
params[index][:geometry] = feature[:geometry].to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
params
|
||||||
|
end
|
||||||
|
|
||||||
|
def properties_to_fetch
|
||||||
|
[:code,
|
||||||
|
:nom,
|
||||||
|
:commune]
|
||||||
|
end
|
||||||
|
end
|
36
spec/lib/carto/sgmap/quartier_prioritaire_adapter_spec.rb
Normal file
36
spec/lib/carto/sgmap/quartier_prioritaire_adapter_spec.rb
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe CARTO::SGMAP::QuartierPrioritaireAdapter do
|
||||||
|
subject { described_class.new(coordinates).to_params }
|
||||||
|
|
||||||
|
before do
|
||||||
|
stub_request(:post, "https://apicarto.sgmap.fr/quartiers-prioritaires/search").
|
||||||
|
with(:body => /.*/,
|
||||||
|
:headers => {'Content-Type' => 'application/json'}).
|
||||||
|
to_return(status: status, body: body)
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'coordinates ard informed' do
|
||||||
|
let(:coordinates) { '' }
|
||||||
|
let(:status) { 200 }
|
||||||
|
let(:body) { File.read('spec/support/files/geojson/response.json') }
|
||||||
|
|
||||||
|
it { expect(subject).to be_a_instance_of(Hash) }
|
||||||
|
|
||||||
|
context 'Attributs' do
|
||||||
|
it { expect(subject[0][:code]).to eq('QP057019') }
|
||||||
|
it { expect(subject[0][:nom]).to eq('Hauts De Vallières') }
|
||||||
|
it { expect(subject[0][:commune]).to eq('Metz') }
|
||||||
|
|
||||||
|
it { expect(subject[0][:geometry]).to eq('{:type=>"MultiPolygon", :coordinates=>[[[[6.2136923480551, 49.1342109827851], [6.21416055031881, 49.1338823553928]]]]}') }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'coordinates are empty' do
|
||||||
|
let(:coordinates) { '' }
|
||||||
|
let(:status) { 404 }
|
||||||
|
let(:body) { '' }
|
||||||
|
|
||||||
|
it { expect { subject }.to raise_error(RestClient::ResourceNotFound) }
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue