From 378e15e083b9c8c354e1da374d87ce30b8005dbf Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Fri, 15 Mar 2024 22:33:26 +0100 Subject: [PATCH] fix(api_geo): include Paris, Lyon and Marseille code INSEE in local data --- app/services/api_geo_service.rb | 1 + lib/tasks/api_geo_data.rake | 6 ++---- spec/services/api_geo_service_spec.rb | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/services/api_geo_service.rb b/app/services/api_geo_service.rb index 987d01e51..2cdd72a18 100644 --- a/app/services/api_geo_service.rb +++ b/app/services/api_geo_service.rb @@ -70,6 +70,7 @@ class APIGeoService def communes_by_postal_code(postal_code) communes_by_postal_code_map.fetch(postal_code, []) + .filter { !_1[:code].in?(['75056', '13055', '69123']) } .sort_by { I18n.transliterate([_1[:name], _1[:postal_code]].join(' ')) } end diff --git a/lib/tasks/api_geo_data.rake b/lib/tasks/api_geo_data.rake index 8876db864..e123f6361 100644 --- a/lib/tasks/api_geo_data.rake +++ b/lib/tasks/api_geo_data.rake @@ -24,9 +24,7 @@ namespace :api_geo_data do data = [] PATH.join("#{filename}.json").open('w') do |f| response = Typhoeus.get("#{API_GEO_URL}/#{query}") - json = JSON.parse(response.body).map(&:symbolize_keys).filter do |result| - !result[:code].in?(['75056', '13055', '69123']) - end.flat_map do |result| + json = JSON.parse(response.body).map(&:symbolize_keys).flat_map do |result| item = { name: result[:nom].tr("'", '’'), code: result[:code], @@ -42,7 +40,7 @@ namespace :api_geo_data do end end data = json - f << JSON.dump(json) + f << JSON.pretty_generate(json.sort_by { _1[:code] }) end data end diff --git a/spec/services/api_geo_service_spec.rb b/spec/services/api_geo_service_spec.rb index fa7826e06..9a4daf1df 100644 --- a/spec/services/api_geo_service_spec.rb +++ b/spec/services/api_geo_service_spec.rb @@ -59,6 +59,21 @@ describe APIGeoService do describe 'commune_name' do subject { APIGeoService.commune_name('01', '01457') } it { is_expected.to eq('Vonnas') } + + context 'Paris' do + subject { APIGeoService.commune_name('75', '75056') } + it { is_expected.to eq('Paris') } + end + + context 'Lyon' do + subject { APIGeoService.commune_name('69', '69123') } + it { is_expected.to eq('Lyon') } + end + + context 'Marseille' do + subject { APIGeoService.commune_name('13', '13055') } + it { is_expected.to eq('Marseille') } + end end describe 'commune_code' do