add adapter for sgmap api carto

This commit is contained in:
Xavier J 2015-11-23 18:43:07 +01:00
parent edc4f7dcd9
commit 5fd78b3fcb
2 changed files with 62 additions and 0 deletions

View 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

View 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