diff --git a/app/lib/code_insee.rb b/app/lib/code_insee.rb new file mode 100644 index 000000000..358af2279 --- /dev/null +++ b/app/lib/code_insee.rb @@ -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 diff --git a/spec/lib/code_insee_spec.rb b/spec/lib/code_insee_spec.rb new file mode 100644 index 000000000..817aac94d --- /dev/null +++ b/spec/lib/code_insee_spec.rb @@ -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