egn 1.2.1 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/egn/generator.rb +66 -32
- data/lib/egn/validator.rb +13 -8
- data/lib/egn/version.rb +1 -1
- data/spec/egn/generator_spec.rb +12 -4
- data/spec/egn/validator_spec.rb +1 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cee82c8e708ea7a658400ed8e8bf7e092a5863a4
|
4
|
+
data.tar.gz: 674f9a620c7e20e250ede85e182bbbdb08368c39
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aef0b00b4fb230d1ce5a43fa05b6522bb16d58915a514505d03f6233fabe3b24bf4faaecdf05331a9415305757b8271e3cd761992ad0003185a2cea3efa15886
|
7
|
+
data.tar.gz: b81ae27bfc6cc0029947e7db3106fc27a27f9cd26c384396603aea361c67a614509c5c6d378b5244dd3f0b334d5101d7b4d4d853a22d22bcacf721a49ed5bfda
|
data/lib/egn/generator.rb
CHANGED
@@ -9,61 +9,95 @@ module Egn
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def initialize(options={})
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
validate!(options)
|
13
|
+
set_defaults!(options)
|
14
|
+
process!
|
15
15
|
end
|
16
16
|
|
17
17
|
# The generated EGN will be completely random if no opitons are given.
|
18
18
|
# options is a hash that may have the following keys: :year, :month and :date
|
19
19
|
def generate
|
20
|
-
|
20
|
+
# YY MM DD REST
|
21
|
+
egn = format(options[:year]) + format(options[:month]) + format(options[:day]) + format(options[:region], 3)
|
22
|
+
|
23
|
+
egn + Util.egn_checksum(egn).to_s
|
24
|
+
end
|
21
25
|
|
22
|
-
|
23
|
-
options[:month].to_s.rjust(2, '0') +
|
24
|
-
options[:day].to_s.rjust(2,'0') +
|
25
|
-
options[:region].to_s.rjust(3,'0')
|
26
|
+
private
|
26
27
|
|
27
|
-
|
28
|
+
def set_defaults!(options)
|
29
|
+
@options = {}
|
30
|
+
|
31
|
+
until Date.valid_date?(@options[:year].to_i, @options[:month].to_i, @options[:day].to_i)
|
32
|
+
@options = defaults.merge(options)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
# Little helper
|
38
|
+
def format(val, pre=2)
|
39
|
+
val.to_s.rjust(pre, '0')
|
28
40
|
end
|
29
41
|
|
30
42
|
# Check if the options contain a date that is valid and be turned into an EGN
|
31
43
|
def validate!(options)
|
32
|
-
raise ArgumentError, "Year out of bounds"
|
33
|
-
raise ArgumentError, "Month out of bounds"
|
34
|
-
raise ArgumentError, "Day out of bounds"
|
35
|
-
raise ArgumentError, "
|
44
|
+
raise ArgumentError, "Year out of bounds" if options[:year] && !(1800..2099).include?(options[:year])
|
45
|
+
raise ArgumentError, "Month out of bounds" if options[:month] && !(1..12).include?(options[:month])
|
46
|
+
raise ArgumentError, "Day out of bounds" if options[:day] && !(1..31).include?(options[:day])
|
47
|
+
raise ArgumentError, "Sex should be one of #{sexes}" if options[:sex] && !sexes.include?(options[:sex])
|
36
48
|
end
|
37
49
|
|
50
|
+
# Random defaults
|
38
51
|
def defaults
|
39
|
-
date = Util.time_rand
|
52
|
+
date = -> { Util.time_rand }.call
|
40
53
|
{
|
41
|
-
year:
|
42
|
-
month:
|
43
|
-
day:
|
44
|
-
sex:
|
54
|
+
year: date.year,
|
55
|
+
month: date.month,
|
56
|
+
day: date.day,
|
57
|
+
sex: sexes.sample,
|
58
|
+
region: Random.rand(0..999)
|
45
59
|
}
|
46
60
|
end
|
47
61
|
|
48
|
-
def
|
62
|
+
def process!
|
49
63
|
# Get random century, region and sex
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
options[:
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
64
|
+
century = determine_century(options[:year])
|
65
|
+
|
66
|
+
options[:month] += month_delta(century)
|
67
|
+
|
68
|
+
options[:region] += region_delta(options[:sex], options[:region])
|
69
|
+
|
70
|
+
options[:year] = options[:year] - century
|
71
|
+
end
|
72
|
+
|
73
|
+
# Recalculate region based on sex
|
74
|
+
def region_delta(sex, region)
|
75
|
+
if sex == :male && region.odd?
|
76
|
+
-1
|
77
|
+
elsif sex == :female && region.even?
|
78
|
+
1
|
79
|
+
else
|
80
|
+
0
|
62
81
|
end
|
82
|
+
end
|
63
83
|
|
64
|
-
|
84
|
+
def month_delta(century)
|
85
|
+
if century == 1800
|
86
|
+
20
|
87
|
+
elsif century == 2000
|
88
|
+
40
|
89
|
+
else
|
90
|
+
0
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def determine_century(year)
|
95
|
+
year - (year % 100)
|
65
96
|
end
|
66
97
|
|
98
|
+
def sexes
|
99
|
+
[:male, :female]
|
100
|
+
end
|
67
101
|
|
68
102
|
end
|
69
103
|
end
|
data/lib/egn/validator.rb
CHANGED
@@ -9,21 +9,26 @@ module Egn
|
|
9
9
|
|
10
10
|
def initialize(egn)
|
11
11
|
@egn = egn
|
12
|
+
|
13
|
+
@year, @month, @day = explode_date(egn)
|
12
14
|
end
|
13
15
|
|
14
16
|
# Checks if a given EGN is valid
|
15
17
|
def validate
|
16
|
-
return false unless egn.length == 10
|
18
|
+
return false unless @egn.length == 10
|
19
|
+
return false unless Date.valid_date?(@year, @month, @day)
|
17
20
|
|
18
|
-
#
|
19
|
-
|
20
|
-
|
21
|
+
# Calculate the checksum and check if the given one is correct
|
22
|
+
checksum = Util.egn_checksum(@egn[0,9])
|
23
|
+
checksum == @egn[9].to_i
|
24
|
+
end
|
21
25
|
|
22
|
-
|
26
|
+
private
|
23
27
|
|
24
|
-
|
25
|
-
|
26
|
-
|
28
|
+
def explode_date(egn)
|
29
|
+
year, month, day = egn.scan(/.{1,2}/).map(&:to_i)
|
30
|
+
year, month = Util.determine_date(year, month)
|
31
|
+
[year, month, day]
|
27
32
|
end
|
28
33
|
|
29
34
|
end
|
data/lib/egn/version.rb
CHANGED
data/spec/egn/generator_spec.rb
CHANGED
@@ -43,10 +43,18 @@ describe Egn::Generator do
|
|
43
43
|
end
|
44
44
|
|
45
45
|
it "generates a new EGN with the given day" do
|
46
|
-
number = Egn::Generator.generate(day:
|
46
|
+
number = Egn::Generator.generate(day: 29)
|
47
47
|
egn = Egn::Egn.new(number)
|
48
48
|
|
49
|
-
expect(egn.day).to eq(
|
49
|
+
expect(egn.day).to eq(29)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "doesn't generate invalid EGN's for day 29 (in case of february)" do
|
53
|
+
Array.new(10_000) { |i| Egn.generate(day: 29) }.each do |egn|
|
54
|
+
result = Egn::Validator.validate(egn)
|
55
|
+
expect(result).to be_true, "Failed for #{egn}"
|
56
|
+
end
|
57
|
+
|
50
58
|
end
|
51
59
|
|
52
60
|
it "generates female EGNs" do
|
@@ -65,9 +73,9 @@ describe Egn::Generator do
|
|
65
73
|
|
66
74
|
it "validates the options" do
|
67
75
|
|
68
|
-
options = {year: 1960, month: 6, day: 3, sex: :male}
|
76
|
+
options = {year: 1960, month: 6, day: 3, sex: :male, region: 333}
|
69
77
|
|
70
|
-
Egn::Generator.any_instance.should_receive(:validate!)
|
78
|
+
Egn::Generator.any_instance.should_receive(:validate!)
|
71
79
|
|
72
80
|
Egn::Generator.generate(options)
|
73
81
|
end
|
data/spec/egn/validator_spec.rb
CHANGED
@@ -17,8 +17,7 @@ describe Egn::Validator do
|
|
17
17
|
it "checks 10 000 of the generated numbers" do
|
18
18
|
Array.new(10_000) { |i| Egn.generate }.each do |egn|
|
19
19
|
result = Egn::Validator.validate(egn)
|
20
|
-
|
21
|
-
expect(result).to be_true
|
20
|
+
expect(result).to be_true, "Failed for #{egn}"
|
22
21
|
end
|
23
22
|
end
|
24
23
|
|