fix(dossier): find communes outside of departement
This commit is contained in:
parent
d3848ee7af
commit
98caf41a36
2 changed files with 19 additions and 35 deletions
|
@ -35,7 +35,7 @@ class APIGeoService
|
|||
end
|
||||
|
||||
def departements
|
||||
[{ code: '99', name: 'Etranger' }] + get_from_api_geo('departements?zone=metro,drom,com').sort_by { _1[:code] }
|
||||
[{ code: '99', name: 'Etranger' }] + get_from_api_geo(:departements).sort_by { _1[:code] }
|
||||
end
|
||||
|
||||
def departement_name(code)
|
||||
|
@ -48,7 +48,7 @@ class APIGeoService
|
|||
end
|
||||
|
||||
def epcis(departement_code)
|
||||
get_from_api_geo("epcis?codeDepartement=#{departement_code}").sort_by { I18n.transliterate(_1[:name]) }
|
||||
get_from_api_geo("epcis-#{departement_code}").sort_by { I18n.transliterate(_1[:name]) }
|
||||
end
|
||||
|
||||
def epci_name(departement_code, code)
|
||||
|
@ -60,25 +60,12 @@ class APIGeoService
|
|||
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(' ')) }
|
||||
get_from_api_geo("communes-#{departement_code}").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
|
||||
communes_by_postal_code_map.fetch(postal_code, [])
|
||||
.sort_by { I18n.transliterate([_1[:name], _1[:postal_code]].join(' ')) }
|
||||
end
|
||||
|
||||
def commune_name(departement_code, code)
|
||||
|
@ -91,28 +78,23 @@ class APIGeoService
|
|||
|
||||
private
|
||||
|
||||
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).flat_map do |result|
|
||||
item = {
|
||||
name: result[:nom].tr("'", '’'),
|
||||
code: result[:code],
|
||||
epci_code: result[:codeEpci],
|
||||
departement_code: result[:codeDepartement]
|
||||
}.compact
|
||||
def communes_by_postal_code_map
|
||||
Rails.cache.fetch('api_geo_communes', expires_in: 1.week) do
|
||||
departements
|
||||
.filter { _1[:code] != '99' }
|
||||
.flat_map { communes(_1[:code]) }
|
||||
.group_by { _1[:postal_code] }
|
||||
end
|
||||
end
|
||||
|
||||
if result[:codesPostaux].present?
|
||||
result[:codesPostaux].map { item.merge(postal_code: _1) }
|
||||
else
|
||||
[item]
|
||||
end
|
||||
end
|
||||
def get_from_api_geo(scope)
|
||||
Rails.cache.fetch("api_geo_#{scope}", expires_in: 1.week) do
|
||||
JSON.parse(Rails.root.join('lib', 'data', 'api_geo', "#{scope}.json").read, symbolize_names: true)
|
||||
end
|
||||
end
|
||||
|
||||
def countries_index_fr
|
||||
Rails.cache.fetch('countries_index_fr', expires_in: 1.year) do
|
||||
Rails.cache.fetch('countries_index_fr', expires_in: 1.week) do
|
||||
countries(locale: 'FR').index_by { I18n.transliterate(_1[:name]).upcase }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -53,11 +53,13 @@ describe APIGeoService do
|
|||
|
||||
describe 'communes_by_postal_code', vcr: { cassette_name: 'api_geo_communes' } do
|
||||
it 'return results' do
|
||||
expect(APIGeoService.communes_by_postal_code('01500').size).to eq(8)
|
||||
expect(APIGeoService.communes_by_postal_code('75019').size).to eq(2)
|
||||
expect(APIGeoService.communes_by_postal_code('69005').size).to eq(2)
|
||||
expect(APIGeoService.communes_by_postal_code('13006').size).to eq(2)
|
||||
expect(APIGeoService.communes_by_postal_code('73480').size).to eq(3)
|
||||
expect(APIGeoService.communes_by_postal_code('20000').first[:code]).to eq('2A004')
|
||||
expect(APIGeoService.communes_by_postal_code('37160').size).to eq(7)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue