Skip to content

Fix years around 2k #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/egn/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def format(val, pre = 2)

# Check if the options contain a date that is valid and be turned into an EGN
def validate!(options)
raise ArgumentError, 'Year out of bounds' if options[:year] && !(1800..2099).include?(options[:year])
raise ArgumentError, 'Year out of bounds' if options[:year] && !Util.year_in_range?(options[:year])
raise ArgumentError, 'Month out of bounds' if options[:month] && !(1..12).include?(options[:month])
raise ArgumentError, 'Day out of bounds' if options[:day] && !(1..31).include?(options[:day])
raise ArgumentError, "Gender should be one of #{genders}" if options[:gender] && !genders.include?(options[:gender])
Expand Down
10 changes: 7 additions & 3 deletions lib/egn/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ module Util
def self.determine_date(year, month)
case month
when (1..12)
year = "19#{year}"
year = "19%02d" % [ year ]
when (21..32)
month -= 20
year = "18#{year}"
year = "18%02d" % [ year ]
when (41..52)
month -= 40
year = "20#{year}"
year = "20%02d" % [ year ]
end

[year.to_i, month]
Expand All @@ -36,5 +36,9 @@ def self.egn_checksum(egn)
def self.time_rand(from = 0.0, to = Time.now)
Time.at(from + rand * (to.to_f - from.to_f))
end

def self.year_in_range?(year)
(1800..2099).include?(year)
end
end
end
1 change: 1 addition & 0 deletions lib/egn/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def initialize(egn)
def validate
return false unless @egn.length == 10
return false unless Date.valid_date?(@year, @month, @day)
return false unless Util.year_in_range?(@year)

# Calculate the checksum and check if the given one is correct
checksum = Util.egn_checksum(@egn[0, 9])
Expand Down
7 changes: 6 additions & 1 deletion spec/egn/egn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@
end
end

describe 'validating'
describe 'validating' do
egn = Egn.parse('0150247881')
it "year must be in this decade #{egn}" do
expect(egn.year).to eq 2001
end
end

describe 'parsing'

Expand Down
2 changes: 1 addition & 1 deletion spec/egn/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
it "doesn't generate invalid EGN's for day 29 (in case of february)" do
Array.new(10_000) { |_i| Egn.generate(day: 29) }.each do |egn|
result = Egn::Validator.validate(egn)
expect(result).to be_true, "Failed for #{egn}"
expect(result).to be_truthy, "Failed for #{egn}"
end
end

Expand Down
13 changes: 9 additions & 4 deletions spec/egn/validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
describe Egn::Validator do
describe '#validate' do
it 'fails for strings with incorrect size' do
expect(Egn::Validator.validate('123456789')).to be_false
expect(Egn::Validator.validate('12345678901')).to be_false
expect(Egn::Validator.validate('123456789')).to be_falsey
expect(Egn::Validator.validate('12345678901')).to be_falsey
end

it 'fails for incorrect dates' do
expect(Egn::Validator.validate('6101347500')).to be_false
expect(Egn::Validator.validate('6101347500')).to be_falsey
end

it 'fails if year is out of range' do
expect(Egn::Util.year_in_range?(1990)).to be_truthy
expect(Egn::Util.year_in_range?(201)).to be_falsey
end

it 'checks 10 000 of the generated numbers' do
Array.new(10_000) { |_| Egn.generate }.each do |egn|
result = Egn::Validator.validate(egn)
expect(result).to be_true, "Failed for #{egn}"
expect(result).to be_truthy, "Failed for #{egn}"
end
end
end
Expand Down