more robust normalization
This commit is contained in:
parent
758673a355
commit
5567570d74
2 changed files with 40 additions and 1 deletions
|
@ -101,7 +101,7 @@ class APIGeoService
|
|||
department_code:,
|
||||
region_name: region_name(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:
|
||||
}
|
||||
else
|
||||
|
@ -122,6 +122,15 @@ class APIGeoService
|
|||
}.merge(territory)
|
||||
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
|
||||
|
||||
def communes_by_postal_code_map
|
||||
|
|
|
@ -107,4 +107,34 @@ describe APIGeoService do
|
|||
it { expect(subject[:city_name]).to eq('Paris') }
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue