isikukood 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in isikukood.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Tanel Kosk
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Isikukood
2
+
3
+ Simple Ruby library for validating and extracting information from Estonian Personal Identification Code.
4
+ Know-how is taken from appropriate wiki page: http://et.wikipedia.org/wiki/Isikukood
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'isikukood'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install isikukood
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ require 'isikukood'
24
+
25
+ code = Isikukood.new('37605030299')
26
+ code.valid? # => true
27
+ code.sex # => 'M'
28
+ code.age # => 35
29
+ code.birthday # => '1976-05-03'
30
+ ```
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/*_test.rb'
8
+ test.warning = true
9
+ #test.verbose = true
10
+ test.ruby_opts = ['-rubygems']
11
+ end
12
+
13
+ desc "Run tests for iskukood gem"
14
+ task :default => :test
data/isikukood.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Tanel Kosk"]
5
+ gem.email = ["tanel@noonius.net"]
6
+ gem.description = "Estonian personal identification number validator"
7
+ gem.summary = "Ruby gem to validate and extract information out of Estonian Personal Identification number"
8
+ gem.homepage = "https://github.com/noonius/isikukood"
9
+
10
+ gem.files = `git ls-files`.split("\n")
11
+ gem.test_files = `git ls-files -- test/*`.split("\n")
12
+ gem.name = "isikukood"
13
+ gem.require_paths = ["lib"]
14
+ gem.version = '0.1.0'
15
+
16
+ gem.add_development_dependency 'riot', '~> 0.12.3'
17
+ gem.add_development_dependency 'rake'
18
+ end
data/lib/isikukood.rb ADDED
@@ -0,0 +1,49 @@
1
+ require 'date'
2
+
3
+ class Isikukood
4
+
5
+ attr_reader :code
6
+
7
+ def initialize(code)
8
+ @code = code.to_s
9
+ end
10
+
11
+ def valid?
12
+ code.size == 11 && code[10].chr.to_i == control_code && birth_date.is_a?(Date)
13
+ end
14
+
15
+ def birthday
16
+ return nil unless valid?
17
+ birth_date.to_s
18
+ end
19
+
20
+ def age
21
+ return nil unless valid?
22
+ now = Time.now.utc
23
+ correction = now.month > birth_date.month || (now.month == birth_date.month && now.day >= birth_date.day) ? 0 : 1
24
+ now.year - birth_date.year - correction
25
+ end
26
+
27
+ def sex
28
+ return nil unless valid?
29
+ code[0].to_i.odd? ? 'M' : 'F'
30
+ end
31
+
32
+ def control_code
33
+ scales1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1]
34
+ checknum = scales1.each_with_index.map {|scale, i| code[i].chr.to_i * scale}.inject(0, :+) % 11
35
+ return checknum unless checknum == 10
36
+
37
+ scales2 = [3, 4, 5, 6, 7, 8, 9, 1, 2, 3]
38
+ checknum = scales2.each_with_index.map {|scale, i| code[i].chr.to_i * scale}.inject(0, :+) % 11
39
+ checknum == 10 ? 0 : checknum
40
+ end
41
+
42
+ private
43
+
44
+ def birth_date
45
+ year, month, day = code[1..2].to_i, code[3..4].to_i, code[5..6].to_i
46
+ Date.valid_date?(year, month, day) ? Date.strptime("#{year}-#{month}-#{day}", '%y-%m-%d') : nil
47
+ end
48
+
49
+ end
@@ -0,0 +1,39 @@
1
+ require File.expand_path('../teststrap', __FILE__)
2
+
3
+ context "Isikukood" do
4
+ context "Valid code for male" do
5
+
6
+ setup {
7
+ Isikukood.new('37605030299')
8
+ }
9
+
10
+ asserts(:valid?)
11
+ asserts(:age).equals(35)
12
+ asserts(:sex).equals('M')
13
+ asserts(:birthday).equals('1976-05-03')
14
+ end
15
+
16
+ context "Valid code for female" do
17
+
18
+ setup {
19
+ Isikukood.new('60012240290')
20
+ }
21
+
22
+ asserts(:valid?)
23
+ asserts(:age).equals(11)
24
+ asserts(:sex).equals('F')
25
+ asserts(:birthday).equals('2000-12-24')
26
+ end
27
+
28
+ context "Invalid code" do
29
+
30
+ setup {
31
+ Isikukood.new('60013240294')
32
+ }
33
+
34
+ denies(:valid?)
35
+ asserts(:age).equals(nil)
36
+ asserts(:sex).equals(nil)
37
+ asserts(:birthday).equals(nil)
38
+ end
39
+ end
data/test/teststrap.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'riot'
2
+ require File.expand_path('../../lib/isikukood',__FILE__)
3
+
4
+ #Riot.pretty_dots
5
+
6
+ class Riot::Situation
7
+ # Custom situation code here
8
+ end
9
+
10
+ class Riot::Context
11
+ # Custom context code here
12
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: isikukood
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Tanel Kosk
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-28 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: riot
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 41
29
+ segments:
30
+ - 0
31
+ - 12
32
+ - 3
33
+ version: 0.12.3
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :development
49
+ version_requirements: *id002
50
+ description: Estonian personal identification number validator
51
+ email:
52
+ - tanel@noonius.net
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files: []
58
+
59
+ files:
60
+ - .gitignore
61
+ - Gemfile
62
+ - LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - isikukood.gemspec
66
+ - lib/isikukood.rb
67
+ - test/isikukood_test.rb
68
+ - test/teststrap.rb
69
+ homepage: https://github.com/noonius/isikukood
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project:
98
+ rubygems_version: 1.8.10
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Ruby gem to validate and extract information out of Estonian Personal Identification number
102
+ test_files:
103
+ - test/isikukood_test.rb
104
+ - test/teststrap.rb
105
+ has_rdoc: