egn 1.2.1 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5fd9fc77598bf5d291ffa9860726c2059f4b4b44
4
- data.tar.gz: 4d530a9ff62d70d59b07afef8b732c75349502f9
3
+ metadata.gz: cee82c8e708ea7a658400ed8e8bf7e092a5863a4
4
+ data.tar.gz: 674f9a620c7e20e250ede85e182bbbdb08368c39
5
5
  SHA512:
6
- metadata.gz: b446790995f21809637b851ed25c4c457e0015323fef4af309c78fe8e921254ed3699ec73e933c6d5e3cf059a5c26e91eacd5682ce8b0426949da1a46ea7e380
7
- data.tar.gz: 222960890a720fa8083eaca487e9fa6ef1a596cd6f132a7b74d8b54e2ebd3a88b48f22cb0d8f50d40a4b1c214f973ca1047c71d9ef2917a4473247e90eebafb5
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
- @options = defaults.merge(options)
13
-
14
- validate!(@options)
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
- randomize_options
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
- egn = options[:year].to_s.rjust(2, '0') +
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
- return egn + Util.egn_checksum(egn).to_s
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" unless (1800..2099).include?(options[:year])
33
- raise ArgumentError, "Month out of bounds" unless (1..12).include?(options[:month])
34
- raise ArgumentError, "Day out of bounds" unless (1..31).include?(options[:day])
35
- raise ArgumentError, "Invalid sex; valid values: [:male, :female]" unless [:male, :female].include?(options[:sex])
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: date.year,
42
- month: date.month,
43
- day: date.day,
44
- sex: [:male, :female].sample
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 randomize_options
62
+ def process!
49
63
  # Get random century, region and sex
50
- options[:century] = options[:year] - (options[:year] % 100)
51
- options[:region] = Random.rand(0..999)
52
-
53
- # Recalculate month based on the century
54
- options[:month] += 20 if options[:century] == 1800
55
- options[:month] += 40 if options[:century] == 2000
56
-
57
- # Recalculate region based on sex
58
- if options[:sex] == :male && options[:region].odd?
59
- options[:region] -= 1
60
- elsif options[:sex] == :female && options[:region].even?
61
- options[:region] += 1
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
- options[:year] = options[:year] - options[:century]
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
- # Extract the correct year and month
19
- year, month, day = egn.scan(/.{1,2}/).map(&:to_i)
20
- year, month = Util.determine_date(year, month)
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
- return false unless Date.valid_date? year, month, day
26
+ private
23
27
 
24
- # Calculate the checksum and check if the given one is correct
25
- checksum = Util.egn_checksum egn[0,9]
26
- checksum == egn[9].to_i
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
@@ -1,3 +1,3 @@
1
1
  module Egn
2
- VERSION = "1.2.1"
2
+ VERSION = "1.2.2"
3
3
  end
@@ -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: 15)
46
+ number = Egn::Generator.generate(day: 29)
47
47
  egn = Egn::Egn.new(number)
48
48
 
49
- expect(egn.day).to eq(15)
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!).with(options)
78
+ Egn::Generator.any_instance.should_receive(:validate!)
71
79
 
72
80
  Egn::Generator.generate(options)
73
81
  end
@@ -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
- puts egn unless result
21
- expect(result).to be_true
20
+ expect(result).to be_true, "Failed for #{egn}"
22
21
  end
23
22
  end
24
23
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: egn
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - gmitrev