Created
March 3, 2015 16:23
-
-
Save voidius/06eb1fb98c7571a8565a to your computer and use it in GitHub Desktop.
Birth date from personal identity number parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# person code format: ddmmyy-cxxxx | |
# where | |
# dd - is for birth day | |
# mm - is for birth month | |
# yy - is for birth year | |
# c - is for birth century (0 - 18xx, 1 - 19xx, 2 - 20xx) | |
# xxxx - is a checksum generated by some secret algorithm | |
# | |
# http://www.lvportals.lv/visi/skaidrojumi/248652-personas-kods-ir-tava-identitate-sarga-to/ | |
class BirthDateFromPersonCodeParser | |
attr_reader :person_code, :year, :month, :day, :checksum, :century | |
def initialize(person_code) | |
@person_code = person_code.gsub '-', '' | |
parse | |
end | |
def birth_date | |
@birth_date ||= Date.new @year.to_i, @month.to_i, @day.to_i | |
end | |
private | |
def parse | |
captured = /^(?<d>\d{2})(?<m>\d{2})(?<y>\d{2})(?<c>[012])(?<checksum>[0-9]{4})$/.match(person_code) | |
store captured | |
end | |
def store(captured) | |
@day = captured[:d] | |
@month = captured[:m] | |
@century = case captured[:c] | |
when '0' then "18" | |
when '1' then "19" | |
when '2' then "20" | |
end | |
@year = "#{@century}#{captured[:y]}" | |
@checksum = captured[:checksum] | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
require 'spec_helper' | |
describe BirthDateFromPersonCodeParser do | |
subject { BirthDateFromPersonCodeParser.new person_code } | |
let(:person_code) { '23045617899' } | |
context 'when given code is dash-formatted' do | |
let(:person_code) { '230456-17899' } | |
it 'removes dash' do | |
expect( subject.person_code ).to eq '23045617899' | |
end | |
end | |
it 'accepts code without dash' do | |
expect( subject.person_code ).to eq '23045617899' | |
end | |
it 'correctly parses day from given person code' do | |
expect( subject.day ).to eq '23' | |
end | |
it 'correctly parses month from given person code' do | |
expect( subject.month ).to eq '04' | |
end | |
it 'correctly builds birth date object' do | |
expect( subject.birth_date ).to eq Date.new(1956, 4, 23) | |
end | |
context 'when birth date is in 19 century' do | |
let(:person_code) { '230415-07899' } | |
it 'correctly parses year from given person code' do | |
expect( subject.year ).to eq '1815' | |
end | |
it 'correctly builds birth date object' do | |
expect( subject.birth_date ).to eq Date.new(1815, 4, 23) | |
end | |
end | |
context 'when birth date is in 20 century' do | |
let(:person_code) { '230415-17899' } | |
it 'correctly parses year from given person code' do | |
expect( subject.year ).to eq '1915' | |
end | |
it 'correctly builds birth date object' do | |
expect( subject.birth_date ).to eq Date.new(1915, 4, 23) | |
end | |
end | |
context 'when birth date is in 21 century' do | |
let(:person_code) { '230415-27899' } | |
it 'correctly parses year from given person code' do | |
expect( subject.year ).to eq '2015' | |
end | |
it 'correctly builds birth date object' do | |
expect( subject.birth_date ).to eq Date.new(2015, 4, 23) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment