more robust normalization

This commit is contained in:
simon lehericey 2024-04-10 10:47:21 +02:00
parent 758673a355
commit 5567570d74
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
2 changed files with 40 additions and 1 deletions

View file

@ -101,7 +101,7 @@ class APIGeoService
department_code:, department_code:,
region_name: region_name(region_code), region_name: region_name(region_code),
region_code:, region_code:,
city_name: commune_name(department_code, city_code) || properties['city'], # fallback to city name if commune not found city_name: safely_normalize_city_name(department_code, city_code, properties['city']),
city_code: city_code:
} }
else else
@ -122,6 +122,15 @@ class APIGeoService
}.merge(territory) }.merge(territory)
end end
def safely_normalize_city_name(department_code, city_code, fallback)
return fallback if department_code.nil? || city_code.nil?
commune_name(department_code, city_code) || fallback
rescue StandardError
fallback
end
private private
def communes_by_postal_code_map def communes_by_postal_code_map

View file

@ -107,4 +107,34 @@ describe APIGeoService do
it { expect(subject[:city_name]).to eq('Paris') } it { expect(subject[:city_name]).to eq('Paris') }
end end
end end
describe 'safely_normalize_city_name' do
let(:department_code) { '75' }
let(:city_code) { '75056' }
let(:fallback) { 'Paris' }
subject { APIGeoService.safely_normalize_city_name(department_code, city_code, fallback) }
context 'nominal' do
it { is_expected.to eq('Paris') }
end
context 'without department' do
let(:department_code) { nil }
it { is_expected.to eq('Paris') }
end
context 'without city_code' do
let(:city_code) { nil }
it { is_expected.to eq('Paris') }
end
context 'with a wrong department' do
let(:department_code) { 'wrong' }
it { is_expected.to eq('Paris') }
end
end
end end