feat(apigeo): add communes_by_postal_code method

This commit is contained in:
Paul Chavard 2023-03-21 14:42:59 +01:00
parent 9d26a38997
commit a4e96d5256
3 changed files with 91 additions and 37 deletions

View file

@ -47,25 +47,6 @@ class APIGeoService
departements.find { _1[:name] == name }&.dig(:code)
end
def communes(departement_code)
get_from_api_geo(
"communes?codeDepartement=#{departement_code}",
additional_keys: { postal_codes: :codesPostaux }
).sort_by { I18n.transliterate(_1[:name]) }
end
def commune_name(departement_code, code)
communes(departement_code).find { _1[:code] == code }&.dig(:name)
end
def commune_code(departement_code, name)
communes(departement_code).find { _1[:name] == name }&.dig(:code)
end
def commune_postal_codes(departement_code, code)
communes(departement_code).find { _1[:code] == code }&.dig(:postal_codes)
end
def epcis(departement_code)
get_from_api_geo("epcis?codeDepartement=#{departement_code}").sort_by { I18n.transliterate(_1[:name]) }
end
@ -78,17 +59,54 @@ class APIGeoService
epcis(departement_code).find { _1[:name] == name }&.dig(:code)
end
def communes(departement_code)
get_from_api_geo("communes?codeDepartement=#{departement_code}&type=commune-actuelle,arrondissement-municipal").sort_by { I18n.transliterate([_1[:name], _1[:postal_code]].join(' ')) }
end
def communes_by_postal_code(postal_code)
if postal_code.size > 3
metro_code = postal_code[0..1]
drom_com_code = postal_code[0..2]
if metro_code == '20'
communes('2A') + communes('2B')
elsif metro_code == '97' || metro_code == '98'
departement_name(drom_com_code) ? communes(drom_com_code) : []
else
departement_name(metro_code) ? communes(metro_code) : []
end
.filter { _1[:postal_code] == postal_code }
.sort_by { I18n.transliterate([_1[:name], _1[:postal_code]].join(' ')) }
else
[]
end
end
def commune_name(departement_code, code)
communes(departement_code).find { _1[:code] == code }&.dig(:name)
end
def commune_code(departement_code, name)
communes(departement_code).find { _1[:name] == name }&.dig(:code)
end
private
def get_from_api_geo(scope, additional_keys: {})
def get_from_api_geo(scope)
Rails.cache.fetch("api_geo_#{scope}", expires_in: 1.year) do
response = Typhoeus.get("#{API_GEO_URL}/#{scope}")
JSON.parse(response.body)
.map(&:symbolize_keys)
.map do |result|
data = { name: result[:nom].tr("'", ''), code: result[:code] }
additional_keys.each { |key, value| data = data.merge(key => result[value]) }
data
JSON.parse(response.body).map(&:symbolize_keys).flat_map do |result|
item = {
name: result[:nom].tr("'", ''),
code: result[:code],
epci_code: result[:codeEpci],
departement_code: result[:codeDepartement]
}.compact
if result[:codesPostaux].present?
result[:codesPostaux].map { item.merge(postal_code: _1) }
else
[item]
end
end
end
end

File diff suppressed because one or more lines are too long

View file

@ -43,29 +43,24 @@ describe APIGeoService do
end
end
describe 'communes', vcr: { cassette_name: 'api_geo_communes' } do
describe 'communes', vcr: { cassette_name: 'api_geo_communes_01' } do
it 'return sorted results' do
expect(APIGeoService.communes('01').size).to eq(393)
expect(APIGeoService.communes('01').first).to eq(code: '01004', name: 'Ambérieu-en-Bugey', postal_codes: ['01500'])
expect(APIGeoService.communes('01').last).to eq(code: '01457', name: 'Vonnas', postal_codes: ['01540'])
expect(APIGeoService.communes('01').size).to eq(399)
expect(APIGeoService.communes('01').first).to eq(code: '01004', name: 'Ambérieu-en-Bugey', postal_code: '01500', departement_code: '01', epci_code: '240100883')
expect(APIGeoService.communes('01').last).to eq(code: '01457', name: 'Vonnas', postal_code: '01540', departement_code: '01', epci_code: '200070555')
end
end
describe 'commune_name', vcr: { cassette_name: 'api_geo_communes' } do
describe 'commune_name', vcr: { cassette_name: 'api_geo_communes_01' } do
subject { APIGeoService.commune_name('01', '01457') }
it { is_expected.to eq('Vonnas') }
end
describe 'commune_code', vcr: { cassette_name: 'api_geo_communes' } do
describe 'commune_code', vcr: { cassette_name: 'api_geo_communes_01' } do
subject { APIGeoService.commune_code('01', 'Vonnas') }
it { is_expected.to eq('01457') }
end
describe 'commune_postal_codes', vcr: { cassette_name: 'api_geo_communes' } do
subject { APIGeoService.commune_postal_codes('01', '01457') }
it { is_expected.to eq(['01540']) }
end
describe 'epcis', vcr: { cassette_name: 'api_geo_epcis' } do
it 'return sorted results' do
expect(APIGeoService.epcis('01').size).to eq(17)