egn 1.3.3 → 1.3.4
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 +4 -4
- data/.travis.yml +3 -3
- data/README.md +37 -35
- data/lib/egn/util.rb +3 -3
- data/lib/egn/validator.rb +4 -0
- data/lib/egn/version.rb +1 -1
- data/spec/egn/validator_spec.rb +4 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8cec4b145317730625a2ecf4a0697138901a586d
|
4
|
+
data.tar.gz: 68bda640e1e48d67e1d306b172ec08d1e1f42a55
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e20300f2867147ee57fbf0a4a5208e47b2e159f75666df2d800c643fc4b1b737634cfc709c2e9754f9358242c3a8bfe7802d1f8ba31a352fc32c9b06ab843bf
|
7
|
+
data.tar.gz: 821ef286a560a0e97ddd3cfe6696725d0747be5ccb20181c266938792bf8ebcc09b84d62ebd22aedacffee88722c96b19bd3fd974550814a958bb5b0094e5b21
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -10,9 +10,9 @@ information.
|
|
10
10
|
## Installation
|
11
11
|
|
12
12
|
Add this line to your application's Gemfile:
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
```ruby
|
14
|
+
gem 'egn'
|
15
|
+
```
|
16
16
|
And then execute:
|
17
17
|
|
18
18
|
$ bundle
|
@@ -23,51 +23,53 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
## Usage
|
25
25
|
|
26
|
-
|
26
|
+
```ruby
|
27
|
+
require 'egn'
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
29
|
+
# Quickly generate a random number
|
30
|
+
Egn.generate
|
31
|
+
=> '6101047500'
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
33
|
+
# Validate a given number
|
34
|
+
Egn.validate('6101047500')
|
35
|
+
=> true
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
37
|
+
# Create an new EGN object with a random number
|
38
|
+
egn = Egn::Egn.new
|
39
|
+
=> 9212094524
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
41
|
+
# OR generate EGN for a specific date
|
42
|
+
egn = Egn::Egn.new(year: 1945, month: 5, day: 8)
|
43
|
+
=> 4505085346
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
45
|
+
# OR parse an existing one
|
46
|
+
egn = Egn.parse('6101047500')
|
47
|
+
=> 6101047500
|
47
48
|
|
48
|
-
|
49
|
-
|
49
|
+
egn.birth_date
|
50
|
+
=> #<Date: 1961-01-04 ((2437304j,0s,0n),+0s,2299161j)>
|
50
51
|
|
51
|
-
|
52
|
-
|
52
|
+
egn.year
|
53
|
+
=> 1961
|
53
54
|
|
54
|
-
|
55
|
-
|
55
|
+
egn.month
|
56
|
+
=> 1
|
56
57
|
|
57
|
-
|
58
|
-
|
58
|
+
egn.day
|
59
|
+
=> 10
|
59
60
|
|
60
|
-
|
61
|
-
|
61
|
+
egn.gender # egn.sex can also be used
|
62
|
+
=> :male
|
62
63
|
|
63
|
-
|
64
|
-
|
64
|
+
egn.gender(format: :char)
|
65
|
+
=> 'm'
|
65
66
|
|
66
|
-
|
67
|
-
|
67
|
+
egn.gender(format: :number)
|
68
|
+
=> 1
|
68
69
|
|
69
|
-
|
70
|
-
|
70
|
+
egn.valid?
|
71
|
+
=> true
|
72
|
+
```
|
71
73
|
|
72
74
|
## Contributing
|
73
75
|
|
data/lib/egn/util.rb
CHANGED
@@ -7,8 +7,8 @@ module Egn
|
|
7
7
|
# be determined by examining the month.
|
8
8
|
# The rules are as follows:
|
9
9
|
# * For people born in 1900..1999 the month does not change
|
10
|
-
# * For people born in 1800..1899 the month
|
11
|
-
# * For people born in 2000..2099 the month
|
10
|
+
# * For people born in 1800..1899 the month is increased by 20 (e.g January is 21)
|
11
|
+
# * For people born in 2000..2099 the month is increased by 40 (e.g December is 52)
|
12
12
|
def self.determine_date(year, month)
|
13
13
|
case month
|
14
14
|
when (1..12)
|
@@ -24,7 +24,7 @@ module Egn
|
|
24
24
|
[year.to_i, month]
|
25
25
|
end
|
26
26
|
|
27
|
-
# More information on the formula: http://www.grao.bg/esgraon.html
|
27
|
+
# More information on the formula: http://www.grao.bg/esgraon.html#section2
|
28
28
|
def self.egn_checksum(egn)
|
29
29
|
sum = egn.chars.map(&:to_i).zip(WEIGHTS).map { |n| n.reduce(:*) }.reduce(:+)
|
30
30
|
|
data/lib/egn/validator.rb
CHANGED
@@ -17,6 +17,10 @@ module Egn
|
|
17
17
|
def validate
|
18
18
|
return false unless @egn.length == 10
|
19
19
|
return false unless Date.valid_date?(@year, @month, @day)
|
20
|
+
|
21
|
+
# Gregorian calendar adoption in 1916 in Bulgaria
|
22
|
+
# 31/03/1916 > +13 days > 14/04/1916
|
23
|
+
return false if @year == 1916 && @month == 4 && @day <= 13
|
20
24
|
|
21
25
|
# Calculate the checksum and check if the given one is correct
|
22
26
|
checksum = Util.egn_checksum(@egn[0, 9])
|
data/lib/egn/version.rb
CHANGED
data/spec/egn/validator_spec.rb
CHANGED
@@ -10,6 +10,10 @@ describe Egn::Validator do
|
|
10
10
|
it 'fails for incorrect dates' do
|
11
11
|
expect(Egn::Validator.validate('6101347500')).to be_false
|
12
12
|
end
|
13
|
+
|
14
|
+
it 'fails for dates during gregorian calendar adoption' do
|
15
|
+
expect(Egn::Validator.validate('1604010886')).to be_false
|
16
|
+
end
|
13
17
|
|
14
18
|
it 'checks 10 000 of the generated numbers' do
|
15
19
|
Array.new(10_000) { |_| Egn.generate }.each do |egn|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: egn
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- gmitrev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-08-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
113
|
version: '0'
|
114
114
|
requirements: []
|
115
115
|
rubyforge_project:
|
116
|
-
rubygems_version: 2.
|
116
|
+
rubygems_version: 2.5.2
|
117
117
|
signing_key:
|
118
118
|
specification_version: 4
|
119
119
|
summary: An easy way to generate and validate EGN numbers and parse them for useful
|