egn 0.4.0 → 1.0.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/lib/egn/generator.rb +48 -29
- data/lib/egn/validator.rb +11 -1
- data/lib/egn/version.rb +1 -1
- data/spec/egn/generator_spec.rb +1 -1
- metadata +4 -4
data/lib/egn/generator.rb
CHANGED
@@ -1,49 +1,68 @@
|
|
1
1
|
# Generates a random valid EGN
|
2
2
|
module Egn
|
3
|
-
|
3
|
+
class Generator
|
4
|
+
attr_reader :options
|
4
5
|
|
5
|
-
#
|
6
|
-
# options is a hash that may have the following keys: :year, :month and :date
|
6
|
+
# Convinience method
|
7
7
|
def self.generate(options={})
|
8
|
-
|
9
|
-
|
10
|
-
options = {
|
11
|
-
year: date.year,
|
12
|
-
month: date.month,
|
13
|
-
day: date.day
|
14
|
-
}.merge(options)
|
15
|
-
|
16
|
-
validate!(options)
|
17
|
-
|
18
|
-
century = options[:year] - (options[:year] % 100)
|
19
|
-
sex = Random.rand(1..2)
|
8
|
+
Generator.new(options).generate
|
9
|
+
end
|
20
10
|
|
21
|
-
|
22
|
-
|
23
|
-
elsif century == 2000
|
24
|
-
options[:month] += 40
|
25
|
-
end
|
11
|
+
def initialize(options={})
|
12
|
+
@options = defaults.merge(options)
|
26
13
|
|
27
|
-
|
14
|
+
validate!(@options)
|
15
|
+
end
|
28
16
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
17
|
+
# The generated EGN will be completely random if no opitons are given.
|
18
|
+
# options is a hash that may have the following keys: :year, :month and :date
|
19
|
+
def generate
|
20
|
+
randomize_options
|
34
21
|
|
35
|
-
|
36
|
-
|
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')
|
37
26
|
|
38
27
|
return egn + Util.egn_checksum(egn).to_s
|
39
28
|
end
|
40
29
|
|
41
30
|
# Check if the options contain a date that is valid and be turned into an EGN
|
42
|
-
def
|
31
|
+
def validate!(options)
|
43
32
|
raise ArgumentError, "Year out of bounds" unless (1800..2099).include?(options[:year])
|
44
33
|
raise ArgumentError, "Month out of bounds" unless (1..12).include?(options[:month])
|
45
34
|
raise ArgumentError, "Day out of bounds" unless (1..31).include?(options[:day])
|
46
35
|
end
|
47
36
|
|
37
|
+
def defaults
|
38
|
+
date = Util.time_rand
|
39
|
+
{
|
40
|
+
year: date.year,
|
41
|
+
month: date.month,
|
42
|
+
day: date.day
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
def randomize_options
|
47
|
+
# Get random century, region and sex
|
48
|
+
options[:century] = options[:year] - (options[:year] % 100)
|
49
|
+
options[:region] = Random.rand(0..999)
|
50
|
+
options[:sex] = Random.rand(1..2)
|
51
|
+
|
52
|
+
# Recalculate month based on the century
|
53
|
+
options[:month] += 20 if options[:century] == 1800
|
54
|
+
options[:month] += 40 if options[:century] == 2000
|
55
|
+
|
56
|
+
# Recalculate region based on sex
|
57
|
+
if options[:sex] == 1 && options[:region].odd?
|
58
|
+
options[:region] -= 1
|
59
|
+
elsif options[:sex] == 2 && options[:region].even?
|
60
|
+
options[:region] += 1
|
61
|
+
end
|
62
|
+
|
63
|
+
options[:year] = options[:year] - options[:century]
|
64
|
+
end
|
65
|
+
|
66
|
+
|
48
67
|
end
|
49
68
|
end
|
data/lib/egn/validator.rb
CHANGED
@@ -1,8 +1,18 @@
|
|
1
1
|
module Egn
|
2
2
|
class Validator
|
3
|
+
attr_reader :egn
|
3
4
|
|
4
|
-
#
|
5
|
+
# Convinience method
|
5
6
|
def self.validate(egn)
|
7
|
+
Validator.new(egn).validate
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(egn)
|
11
|
+
@egn = egn
|
12
|
+
end
|
13
|
+
|
14
|
+
# Checks if a given EGN is valid
|
15
|
+
def validate
|
6
16
|
return false unless egn.length == 10
|
7
17
|
|
8
18
|
# Extract the correct year and month
|
data/lib/egn/version.rb
CHANGED
data/spec/egn/generator_spec.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.
|
4
|
+
version: 1.0.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: 2014-
|
12
|
+
date: 2014-04-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -117,7 +117,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
117
117
|
version: '0'
|
118
118
|
segments:
|
119
119
|
- 0
|
120
|
-
hash:
|
120
|
+
hash: 3961938268472800387
|
121
121
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
122
|
none: false
|
123
123
|
requirements:
|
@@ -126,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
126
126
|
version: '0'
|
127
127
|
segments:
|
128
128
|
- 0
|
129
|
-
hash:
|
129
|
+
hash: 3961938268472800387
|
130
130
|
requirements: []
|
131
131
|
rubyforge_project:
|
132
132
|
rubygems_version: 1.8.23
|