egn 0.1.1 → 0.2.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.
- data/egn.gemspec +1 -0
- data/lib/egn/generator.rb +46 -0
- data/lib/egn/parser.rb +35 -0
- data/lib/egn/validator.rb +31 -0
- data/lib/egn/version.rb +1 -1
- data/lib/egn.rb +11 -98
- metadata +21 -2
data/egn.gemspec
CHANGED
@@ -0,0 +1,46 @@
|
|
1
|
+
module Egn
|
2
|
+
module Generator
|
3
|
+
|
4
|
+
PARITY_WEIGHTS = [2,4,8,5,10,9,7,3,6]
|
5
|
+
|
6
|
+
def self.egn
|
7
|
+
date = time_rand
|
8
|
+
year = date.year
|
9
|
+
mon = date.month
|
10
|
+
day = date.day
|
11
|
+
|
12
|
+
cent = year - (year % 100)
|
13
|
+
sex = Random.rand(1..2)
|
14
|
+
|
15
|
+
if cent == 1800
|
16
|
+
mon += 20
|
17
|
+
elsif cent == 2000
|
18
|
+
mon += 40
|
19
|
+
end
|
20
|
+
|
21
|
+
region = Random.rand(0..999)
|
22
|
+
|
23
|
+
if sex == 1 && (region %2 != 0)
|
24
|
+
region -= 1
|
25
|
+
elsif sex == 2 && (region % 2 == 0)
|
26
|
+
region += 1
|
27
|
+
end
|
28
|
+
|
29
|
+
final_year = year - cent
|
30
|
+
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')
|
31
|
+
|
32
|
+
return egn + egn_checksum(egn).to_s
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.egn_checksum(egn)
|
36
|
+
sum = egn.split('').map(&:to_i).zip(PARITY_WEIGHTS).map { |n| n.reduce(:*) }.reduce(:+)
|
37
|
+
|
38
|
+
rest = sum % 11
|
39
|
+
rest < 10 ? rest : 0
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.time_rand(from = 0.0, to = Time.now)
|
43
|
+
Time.at(from + rand * (to.to_f - from.to_f))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/egn/parser.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module Egn
|
2
|
+
class Parser
|
3
|
+
|
4
|
+
def initialize(egn)
|
5
|
+
return ArgumentError, 'invalid length (should == 10)' unless egn.length == 10
|
6
|
+
|
7
|
+
@year, @month, @day = egn.scan(/.{1,2}/)
|
8
|
+
@month = @month.to_i
|
9
|
+
@day = @day.to_i
|
10
|
+
|
11
|
+
case @month
|
12
|
+
when (1..12)
|
13
|
+
@year = "19#{@year}"
|
14
|
+
when (21..32)
|
15
|
+
@month -= 20
|
16
|
+
@year = "18#{@year}"
|
17
|
+
when (41..52)
|
18
|
+
@month -= 40
|
19
|
+
@year = "20#{@year}"
|
20
|
+
end
|
21
|
+
@year = @year.to_i
|
22
|
+
|
23
|
+
raise ArgumentError, "invalid date" unless Date.valid_date? @year, @month, @day
|
24
|
+
end
|
25
|
+
|
26
|
+
def birth_date
|
27
|
+
Date.parse("#{@year}-#{@month}-#{@day}")
|
28
|
+
end
|
29
|
+
|
30
|
+
def gender
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Egn
|
2
|
+
module Validator
|
3
|
+
|
4
|
+
def self.egn(egn)
|
5
|
+
return false unless egn.length == 10
|
6
|
+
|
7
|
+
year, month, day = egn.scan(/.{1,2}/)
|
8
|
+
month = month.to_i
|
9
|
+
day = day.to_i
|
10
|
+
|
11
|
+
case month
|
12
|
+
when (1..12)
|
13
|
+
year = "19#{year}"
|
14
|
+
when (21..32)
|
15
|
+
month -= 20
|
16
|
+
year = "18#{year}"
|
17
|
+
when (41..52)
|
18
|
+
month -= 40
|
19
|
+
year = "20#{year}"
|
20
|
+
end
|
21
|
+
year = year.to_i
|
22
|
+
|
23
|
+
return false unless Date.valid_date? year, month, day
|
24
|
+
|
25
|
+
checksum = Generator.egn_checksum egn[0,9]
|
26
|
+
|
27
|
+
checksum == egn[9].to_i
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
data/lib/egn/version.rb
CHANGED
data/lib/egn.rb
CHANGED
@@ -1,109 +1,22 @@
|
|
1
|
-
require "egn/version"
|
2
1
|
require "date"
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
def self.egn
|
9
|
-
date = time_rand
|
10
|
-
year = date.year
|
11
|
-
mon = date.month
|
12
|
-
day = date.day
|
13
|
-
|
14
|
-
cent = year - (year % 100)
|
15
|
-
sex = Random.rand(1..2)
|
16
|
-
|
17
|
-
if cent == 1800
|
18
|
-
mon += 20
|
19
|
-
elsif cent == 2000
|
20
|
-
mon += 40
|
21
|
-
end
|
22
|
-
|
23
|
-
region = Random.rand(0..999)
|
24
|
-
|
25
|
-
if sex == 1 && (region %2 != 0)
|
26
|
-
region -= 1
|
27
|
-
elsif sex == 2 && (region % 2 == 0)
|
28
|
-
region += 1
|
29
|
-
end
|
30
|
-
|
31
|
-
final_year = year - cent
|
32
|
-
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')
|
33
|
-
|
34
|
-
return egn + egn_checksum(egn).to_s
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.egn_checksum(egn)
|
38
|
-
sum = egn.split('').map(&:to_i).zip(PARITY_WEIGHTS).map { |n| n.reduce(:*) }.reduce(:+)
|
39
|
-
|
40
|
-
rest = sum % 11
|
41
|
-
rest < 10 ? rest : 0
|
42
|
-
end
|
3
|
+
require "egn/generator"
|
4
|
+
require "egn/parser"
|
5
|
+
require "egn/validator"
|
6
|
+
require "egn/version"
|
43
7
|
|
44
|
-
|
45
|
-
Time.at(from + rand * (to.to_f - from.to_f))
|
46
|
-
end
|
8
|
+
module Egn
|
47
9
|
|
10
|
+
def self.generate
|
11
|
+
Generator.egn
|
48
12
|
end
|
49
13
|
|
50
|
-
|
51
|
-
|
52
|
-
return false unless egn.length == 10
|
53
|
-
|
54
|
-
year, month, day = egn.scan(/.{1,2}/)
|
55
|
-
month = month.to_i
|
56
|
-
day = day.to_i
|
57
|
-
|
58
|
-
case month
|
59
|
-
when (1..12)
|
60
|
-
year = "19#{year}"
|
61
|
-
when (21..32)
|
62
|
-
month -= 20
|
63
|
-
year = "20#{year}"
|
64
|
-
when (41..52)
|
65
|
-
month -= 40
|
66
|
-
year = "18#{year}"
|
67
|
-
end
|
68
|
-
year = year.to_i
|
69
|
-
|
70
|
-
return false unless Date.valid_date? year, month, day
|
71
|
-
|
72
|
-
checksum = Generator.egn_checksum egn[0,9]
|
73
|
-
|
74
|
-
checksum == egn[9].to_i
|
75
|
-
end
|
76
|
-
|
14
|
+
def self.validate(egn)
|
15
|
+
Validator.egn(egn)
|
77
16
|
end
|
78
17
|
|
79
|
-
|
80
|
-
|
81
|
-
def initialize(egn)
|
82
|
-
return false unless egn.length == 10
|
83
|
-
|
84
|
-
@year, @month, @day = egn.scan(/.{1,2}/)
|
85
|
-
@month = @month.to_i
|
86
|
-
@day = @day.to_i
|
87
|
-
|
88
|
-
case @month
|
89
|
-
when (1..12)
|
90
|
-
@year = "19#{@year}"
|
91
|
-
when (21..32)
|
92
|
-
@month -= 20
|
93
|
-
@year = "20#{@year}"
|
94
|
-
when (41..52)
|
95
|
-
@month -= 40
|
96
|
-
@year = "18#{@year}"
|
97
|
-
end
|
98
|
-
@year = @year.to_i
|
99
|
-
|
100
|
-
raise ArgumentError, "invalid date" unless Date.valid_date? @year, @month, @day
|
101
|
-
end
|
102
|
-
|
103
|
-
def birth_date
|
104
|
-
Date.parse("#{@year}-#{@month}-#{@day}")
|
105
|
-
end
|
106
|
-
|
18
|
+
def self.parse(egn)
|
19
|
+
Parser.new(egn)
|
107
20
|
end
|
108
21
|
|
109
22
|
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.
|
4
|
+
version: 0.2.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:
|
12
|
+
date: 2014-03-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -43,6 +43,22 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '2.6'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.6'
|
46
62
|
description: Generator, validator and info parser for EGN/EIK numbers
|
47
63
|
email:
|
48
64
|
- gvmitrev@gmail.com
|
@@ -58,6 +74,9 @@ files:
|
|
58
74
|
- Rakefile
|
59
75
|
- egn.gemspec
|
60
76
|
- lib/egn.rb
|
77
|
+
- lib/egn/generator.rb
|
78
|
+
- lib/egn/parser.rb
|
79
|
+
- lib/egn/validator.rb
|
61
80
|
- lib/egn/version.rb
|
62
81
|
homepage: https://github.com/gmitrev/egn
|
63
82
|
licenses:
|