Add lib API SGMAP Cartography Cadastre

Adapt lib API SGMAP Cartography Quartier Prioritaire at the new API architecture
This commit is contained in:
Xavier J 2016-01-12 17:24:42 +01:00
parent 312c1ee7a7
commit d48c600476
11 changed files with 1403 additions and 44 deletions

View file

@ -1,4 +1,4 @@
class CARTO::SGMAP::QuartiersPrioritaires::API
class CARTO::SGMAP::API
def initialize
end
@ -7,6 +7,11 @@ class CARTO::SGMAP::QuartiersPrioritaires::API
call(base_url + endpoint, {geojson: geojson.to_s})
end
def self.search_cadastre(geojson)
endpoint = "/cadastre/geometrie"
call(base_url + endpoint, {geojson: geojson.to_s})
end
private
def self.call(url, params = {})

View file

@ -0,0 +1,22 @@
class CARTO::SGMAP::Cadastre::Adapter
def initialize(coordinates)
@coordinates = GeojsonService.to_json_polygon(coordinates)
end
def data_source
@data_source ||= JSON.parse(CARTO::SGMAP::API.search_cadastre(@coordinates), symbolize_names: true)
end
def to_params
params = []
data_source[:features].each do |feature|
tmp = feature[:properties]
tmp[:geometry] = feature[:geometry]
params << tmp
end
params
end
end

View file

@ -4,7 +4,7 @@ class CARTO::SGMAP::QuartiersPrioritaires::Adapter
end
def data_source
@data_source ||= JSON.parse(CARTO::SGMAP::QuartiersPrioritaires::API.search_qp(@coordinates), symbolize_names: true)
@data_source ||= JSON.parse(CARTO::SGMAP::API.search_qp(@coordinates), symbolize_names: true)
end
def to_params

View file

@ -0,0 +1,79 @@
require 'spec_helper'
describe CARTO::SGMAP::API do
describe '.search_qp' do
subject { described_class.search_qp(geojson) }
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 'when geojson is empty' do
let(:geojson) { '' }
let(:status) { 404 }
let(:body) { '' }
it 'raises RestClient::ResourceNotFound' do
expect { subject }.to raise_error(RestClient::ResourceNotFound)
end
end
context 'when geojson exist' do
let(:geojson) { File.read('spec/support/files/geojson/request_qp.json') }
let(:status) { 200 }
let(:body) { 'toto' }
it 'returns response body' do
expect(subject).to eq(body)
end
context 'when geojson is at format JSON' do
let(:geojson) { JSON.parse(File.read('spec/support/files/geojson/request_qp.json')) }
it 'returns response body' do
expect(subject).to eq(body)
end
end
end
end
describe '.search_cadastre' do
subject { described_class.search_cadastre(geojson) }
before do
stub_request(:post, "https://apicarto.sgmap.fr/cadastre/geometrie").
with(:body => /.*/,
:headers => {'Content-Type'=>'application/json'}).
to_return(status: status, body: body)
end
context 'when geojson is empty' do
let(:geojson) { '' }
let(:status) { 404 }
let(:body) { '' }
it 'raises RestClient::ResourceNotFound' do
expect { subject }.to raise_error(RestClient::ResourceNotFound)
end
end
context 'when geojson exist' do
let(:geojson) { File.read('spec/support/files/geojson/request_cadastre.json') }
let(:status) { 200 }
let(:body) { 'toto' }
it 'returns response body' do
expect(subject).to eq(body)
end
context 'when geojson is at format JSON' do
let(:geojson) { JSON.parse(File.read('spec/support/files/geojson/request_cadastre.json')) }
it 'returns response body' do
expect(subject).to eq(body)
end
end
end
end
end

View file

@ -0,0 +1,45 @@
require 'spec_helper'
describe CARTO::SGMAP::Cadastre::Adapter do
subject { described_class.new(coordinates).to_params }
before do
stub_request(:post, "https://apicarto.sgmap.fr/cadastre/geometrie").
with(:body => /.*/,
:headers => {'Content-Type' => 'application/json'}).
to_return(status: status, body: body)
end
context 'coordinates are filled' do
let(:coordinates) { '[[2.252728, 43.27151][2.323223, 32.835332]]' }
let(:status) { 200 }
let(:body) { File.read('spec/support/files/geojson/response_cadastre.json') }
it { expect(subject).to be_a_instance_of(Array) }
it { expect(subject.size).to eq 16 }
describe 'Attributes' do
subject { super().first }
it { expect(subject[:surface_intersection]).to eq('0.0202') }
it { expect(subject[:surface_parcelle]).to eq(220.0664659755941) }
it { expect(subject[:numero]).to eq('0082') }
it { expect(subject[:feuille]).to eq(1) }
it { expect(subject[:section]).to eq('0J') }
it { expect(subject[:code_dep]).to eq('94') }
it { expect(subject[:nom_com]).to eq('Maisons-Alfort') }
it { expect(subject[:code_com]).to eq('046') }
it { expect(subject[:code_arr]).to eq('000') }
it { expect(subject[:geometry]).to eq({type: "MultiPolygon", coordinates: [[[[2.4362443, 48.8092078], [2.436384, 48.8092043], [2.4363802, 48.8091414]]]] })}
end
end
context 'coordinates are empty' do
let(:coordinates) { '' }
let(:status) { 404 }
let(:body) { '' }
it { expect { subject }.to raise_error(RestClient::ResourceNotFound) }
end
end

View file

@ -13,7 +13,7 @@ describe CARTO::SGMAP::QuartiersPrioritaires::Adapter do
context 'coordinates are filled' do
let(:coordinates) { '[[2.252728, 43.27151][2.323223, 32.835332]]' }
let(:status) { 200 }
let(:body) { File.read('spec/support/files/geojson/response.json') }
let(:body) { File.read('spec/support/files/geojson/response_qp.json') }
it { expect(subject).to be_a_instance_of(Hash) }

View file

@ -1,41 +0,0 @@
require 'spec_helper'
describe CARTO::SGMAP::QuartiersPrioritaires::API do
describe '.search_qp' do
subject { described_class.search_qp(geojson) }
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 'when geojson is empty' do
let(:geojson) { '' }
let(:status) { 404 }
let(:body) { '' }
it 'raises RestClient::ResourceNotFound' do
expect { subject }.to raise_error(RestClient::ResourceNotFound)
end
end
context 'when geojson exist' do
let(:geojson) { File.read('spec/support/files/geojson/request.json') }
let(:status) { 200 }
let(:body) { 'toto' }
it 'returns response body' do
expect(subject).to eq(body)
end
context 'when geojson is at format JSON' do
let(:geojson) { JSON.parse(File.read('spec/support/files/geojson/request.json')) }
it 'returns response body' do
expect(subject).to eq(body)
end
end
end
end
end

View file

@ -0,0 +1,32 @@
{
"geom": {
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
2.4361109733581543,
48.80952020827361
],
[
2.4361109733581543,
48.809025611747614
],
[
2.43720531463623,
48.809025611747614
],
[
2.4369263648986816,
48.809675651888504
],
[
2.4361109733581543,
48.80952020827361
]
]
]
}
}
}

File diff suppressed because it is too large Load diff