egn 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/lib/egn.rb +88 -24
- data/lib/egn/version.rb +1 -1
- metadata +2 -2
data/.gitignore
CHANGED
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
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
34
|
+
def self.egn_checksum(egn)
|
35
|
+
sum = egn.split('').map(&:to_i).zip(PARITY_WEIGHTS).map { |n| n.reduce(:*) }.reduce(:+)
|
20
36
|
|
21
|
-
|
22
|
-
|
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
|
-
|
34
|
-
|
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
|
data/lib/egn/version.rb
CHANGED
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
|
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-
|
12
|
+
date: 2013-10-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|