convert code_insee to departement

This commit is contained in:
Christophe Robillard 2023-10-26 11:34:55 +02:00
parent 2b061dc2aa
commit 42d3052c4f
2 changed files with 23 additions and 0 deletions

14
app/lib/code_insee.rb Normal file
View file

@ -0,0 +1,14 @@
class CodeInsee
def initialize(code_insee)
@code_insee = code_insee
end
def to_departement
dept = @code_insee.strip.first(2)
if dept < "97"
dept
else
@code_insee.strip.first(3)
end
end
end

View file

@ -0,0 +1,9 @@
describe CodeInsee do
it 'converts to departement' do
expect(CodeInsee.new('75002').to_departement).to eq('75')
expect(CodeInsee.new('2B033').to_departement).to eq('2B')
expect(CodeInsee.new('01345').to_departement).to eq('01')
expect(CodeInsee.new('97100').to_departement).to eq('971')
expect(CodeInsee.new('98735').to_departement).to eq('987')
end
end