egn 0.0.1 → 0.1.0

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.
Files changed (4) hide show
  1. data/.gitignore +1 -0
  2. data/lib/egn.rb +88 -24
  3. data/lib/egn/version.rb +1 -1
  4. metadata +2 -2
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ repl.rb
data/lib/egn.rb CHANGED
@@ -1,39 +1,103 @@
1
- require "egn/version"
1
+ # require "egn/version"
2
2
 
3
3
  module Egn
4
4
  PARITY_WEIGHTS = [2,4,8,5,10,9,7,3,6]
5
5
 
6
- def self.generate
7
- year = Random.rand(1800..2000)
8
- mon = Random.rand(1..12)
9
- day = Random.rand(1..31)
10
- cent = year - (year % 100)
11
- sex = Random.rand(1..2)
12
-
13
- if cent == 1800
14
- mon += 20
15
- elsif cent == 2000
16
- mon += 40
6
+ module Generator
7
+ def self.egn
8
+ year = Random.rand(1800..2099)
9
+ mon = Random.rand(1..12)
10
+ day = Random.rand(1..31)
11
+ cent = year - (year % 100)
12
+ sex = Random.rand(1..2)
13
+
14
+ if cent == 1800
15
+ mon += 20
16
+ elsif cent == 2000
17
+ mon += 40
18
+ end
19
+
20
+ region = Random.rand(0..999)
21
+
22
+ if sex == 1 && (region %2 != 0)
23
+ region -= 1
24
+ elsif sex == 2 && (region % 2 == 0)
25
+ region += 1
26
+ end
27
+
28
+ final_year = year - cent
29
+ egn = final_year.to_s.rjust(2, '0') + mon.to_s.rjust(2, '0') + day.to_s.rjust(2,'0') + region.to_s.rjust(3,'0')
30
+
31
+ return egn + egn_checksum(egn).to_s
17
32
  end
18
33
 
19
- region = Random.rand(0..999)
34
+ def self.egn_checksum(egn)
35
+ sum = egn.split('').map(&:to_i).zip(PARITY_WEIGHTS).map { |n| n.reduce(:*) }.reduce(:+)
20
36
 
21
- if sex == 1 && (region %2 != 0)
22
- region -= 1
23
- elsif sex == 2 && (region % 2 == 0)
24
- region += 1
37
+ rest = sum % 11
38
+ rest < 10 ? rest : 0
25
39
  end
26
40
 
27
- final_year = year - cent
28
- egn = final_year.to_s.rjust(2, '0') + mon.to_s.rjust(2, '0') + day.to_s.rjust(2,'0') + region.to_s.rjust(3,'0')
29
41
 
30
- return egn + gen_checksum(egn).to_s
31
42
  end
32
43
 
33
- def self.gen_checksum(egn)
34
- sum = egn.split('').map(&:to_i).zip(PARITY_WEIGHTS).map { |n| n.reduce(:*) }.reduce(:+)
44
+ module Validator
45
+ def self.egn(egn)
46
+ return false unless egn.length == 10
47
+
48
+ year, month, day = egn.scan(/.{1,2}/)
49
+ month = month.to_i
50
+ day = day.to_i
51
+
52
+ case month
53
+ when (1..12)
54
+ year = "19#{year}"
55
+ when (21..32)
56
+ month -= 20
57
+ year = "20#{year}"
58
+ when (41..52)
59
+ month -= 40
60
+ year = "18#{year}"
61
+ end
62
+ year = year.to_i
63
+
64
+ return false unless Date.valid_date? year, month, day
65
+
66
+ checksum = Generator.egn_checksum egn[0,9]
67
+
68
+ checksum == egn[9].to_i
69
+ end
70
+
71
+ end
72
+
73
+ class Parser
74
+
75
+ def initialize(egn)
76
+ return false unless egn.length == 10
77
+
78
+ @year, @month, @day = egn.scan(/.{1,2}/)
79
+ @month = @month.to_i
80
+ @day = @day.to_i
81
+
82
+ case @month
83
+ when (1..12)
84
+ @year = "19#{@year}"
85
+ when (21..32)
86
+ @month -= 20
87
+ @year = "20#{@year}"
88
+ when (41..52)
89
+ @month -= 40
90
+ @year = "18#{@year}"
91
+ end
92
+ @year = @year.to_i
93
+
94
+ raise ArgumentError, "invalid date" unless Date.valid_date? @year, @month, @day
95
+ end
96
+
97
+ def birth_date
98
+ Date.parse("#{@year}-#{@month}-#{@day}")
99
+ end
35
100
 
36
- rest = sum % 11
37
- rest < 10 ? rest : 0
38
101
  end
102
+
39
103
  end
@@ -1,3 +1,3 @@
1
1
  module Egn
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
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: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-14 00:00:00.000000000 Z
12
+ date: 2013-10-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler