turkish_id 0.3.1 → 0.4.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.
- checksums.yaml +5 -5
- data/.ruby-version +1 -1
- data/README.md +48 -3
- data/bin/console +4 -4
- data/lib/turkish_id/version.rb +1 -1
- data/lib/turkish_id.rb +46 -21
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bbb26b22018a74e1d6f96e7a99fb04b67e62763b3470b3fa3b4efebd347dee95
|
4
|
+
data.tar.gz: 426878d705b49faeb15398622e4746a30f0955cb31fe80c27f8193be72397297
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 662308d80cd939cdeaccb7e121d84b26051f2f29827d54ec617a17786d372d8f985ddd964c33547e7c693226aedc138d98e18e728655ddee67d74ab73f498cf5
|
7
|
+
data.tar.gz: 2a0e0d9fe6f032e5d7a7766281fbbeae8610b35eaa06758edeaaf959c766a4d19d96e317bdf5bd946fda02f3d3d31bb6cbb2359aca79a05b4b69088316daa4f2
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.
|
1
|
+
ruby-2.5.1
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
# Turkish ID
|
2
|
-
|
1
|
+
# Turkish ID
|
2
|
+
|
3
|
+
[](https://travis-ci.org/krmbzds/turkish_id) [](https://github.com/krmbzds/turkish_id) [](https://rubygems.org/gems/turkish_id) [](https://rubygems.org/gems/turkish_id) [](https://codeclimate.com/github/krmbzds/turkish_id) [](https://codeclimate.com/github/krmbzds/turkish_id/coverage)
|
3
4
|
|
5
|
+
This gem provides methods to validate Turkish Identification Numbers.
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
@@ -23,7 +25,7 @@ Or install it yourself as:
|
|
23
25
|
Create a new instance:
|
24
26
|
|
25
27
|
```rb
|
26
|
-
identity_number = TurkishId.new(
|
28
|
+
identity_number = TurkishId.new(10000000146)
|
27
29
|
```
|
28
30
|
|
29
31
|
Use ```is_valid?``` method to check validity:
|
@@ -54,6 +56,49 @@ Where ```dn``` refers to the ```n-th``` digit of the identification number.
|
|
54
56
|
|
55
57
|
Remember that a valid identification number does not imply the existence of an ID. It could only be used as a preliminary check e.g. before querying a government website. This is very similar to credit card validation.
|
56
58
|
|
59
|
+
|
60
|
+
## Generating Relatives
|
61
|
+
|
62
|
+
You can generate ID numbers for your younger or elder relatives.
|
63
|
+
|
64
|
+
```rb
|
65
|
+
me = TurkishId.new(10000000146)
|
66
|
+
me.elder_relatives.take(5)
|
67
|
+
```
|
68
|
+
|
69
|
+
Calling `younger_relative` or `elder_relative` will return an Enumerable class.
|
70
|
+
|
71
|
+
```rb
|
72
|
+
me.elder_relative
|
73
|
+
#=> #<Enumerator:0x00007f9e629032d0>
|
74
|
+
```
|
75
|
+
|
76
|
+
You can perform standard Enumerable operations on it.
|
77
|
+
|
78
|
+
```rb
|
79
|
+
me.elder_relative.first
|
80
|
+
|
81
|
+
#=> 10003000082
|
82
|
+
```
|
83
|
+
|
84
|
+
```rb
|
85
|
+
3.times do
|
86
|
+
puts me.elder_relative.next()
|
87
|
+
end
|
88
|
+
|
89
|
+
#=> 10035998982
|
90
|
+
#=> 10005999902
|
91
|
+
#=> 10008999848
|
92
|
+
```
|
93
|
+
|
94
|
+
```rb
|
95
|
+
me.elder_relative.take(5)
|
96
|
+
|
97
|
+
#=> [10003000082, 10005999902, 10008999848, 10011999774, 10014999610]
|
98
|
+
```
|
99
|
+
|
100
|
+
And so on.
|
101
|
+
|
57
102
|
## Development
|
58
103
|
|
59
104
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/bin/console
CHANGED
@@ -7,8 +7,8 @@ require "turkish_id"
|
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
8
8
|
|
9
9
|
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
require "pry"
|
11
|
-
Pry.start
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
12
|
|
13
|
-
|
14
|
-
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/lib/turkish_id/version.rb
CHANGED
data/lib/turkish_id.rb
CHANGED
@@ -1,54 +1,79 @@
|
|
1
1
|
require "turkish_id/version"
|
2
2
|
|
3
3
|
class TurkishId
|
4
|
-
attr_reader :id_number, :checksum
|
4
|
+
attr_reader :id_number, :checksum, :elder_relative, :younger_relative
|
5
5
|
|
6
6
|
def initialize(id_number)
|
7
|
-
@id_number =
|
7
|
+
@id_number = get_id_array(id_number)
|
8
8
|
@checksum = calculate_checksum(@id_number)
|
9
|
+
@elder_relative = generate_relatives(@id_number, :up)
|
10
|
+
@younger_relative = generate_relatives(@id_number, :down)
|
9
11
|
end
|
10
12
|
|
11
13
|
def is_valid?
|
12
|
-
id = @id_number # For brevity
|
13
|
-
|
14
14
|
# Early termination, bad length or first digit
|
15
|
-
return false if
|
15
|
+
return false if @id_number.length != 11 || @id_number.first == 0
|
16
16
|
|
17
17
|
# Calculate checksum if not already calculated
|
18
|
-
@checksum ||= calculate_checksum(id_number)
|
19
|
-
|
20
|
-
# Check if the tenth digit matches the algorithm
|
21
|
-
return false if id[9] != @checksum[0]
|
18
|
+
@checksum ||= calculate_checksum(@id_number)
|
22
19
|
|
23
|
-
# Check if
|
24
|
-
return false if
|
20
|
+
# Check if tenth or eleventh digits match the algorithm
|
21
|
+
return false if @id_number[9] != @checksum[0] || @id_number[10] != @checksum[1]
|
25
22
|
|
26
23
|
true # All conditions met
|
27
24
|
end
|
28
25
|
|
29
26
|
private
|
30
27
|
|
31
|
-
def calculate_checksum(
|
28
|
+
def calculate_checksum(id_array)
|
32
29
|
# Calculate the sums of odd and even digits
|
33
|
-
odds =
|
34
|
-
evens =
|
30
|
+
odds = id_array.values_at(0, 2, 4, 6, 8).reduce(:+)
|
31
|
+
evens = id_array.values_at(1, 3, 5, 7).reduce(:+)
|
35
32
|
|
36
33
|
# Generate checksum digits
|
37
34
|
d10 = ((odds * 7) - evens) % 10
|
38
|
-
d11 = (
|
35
|
+
d11 = (id_array.take(9).reduce(:+) + d10) % 10
|
39
36
|
|
40
37
|
# Return checksum
|
41
38
|
[d10, d11]
|
42
39
|
rescue
|
43
|
-
[]
|
40
|
+
[]
|
41
|
+
end
|
42
|
+
|
43
|
+
def append_checksum(id)
|
44
|
+
id_core = split_num(id)
|
45
|
+
join_num(id_core + calculate_checksum(id_core))
|
46
|
+
end
|
47
|
+
|
48
|
+
def next_relative(id_array, direction)
|
49
|
+
tree = { up: 1, down: -1 }
|
50
|
+
get_core(id_array) + 29999 * tree[direction]
|
44
51
|
end
|
45
52
|
|
46
|
-
def
|
47
|
-
|
48
|
-
|
53
|
+
def generate_relatives(id_array, direction)
|
54
|
+
Enumerator.new do |y|
|
55
|
+
id = join_num(id_array)
|
56
|
+
loop do
|
57
|
+
id = next_relative(id, direction)
|
58
|
+
y << append_checksum(id)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def get_id_array(id)
|
64
|
+
split_num(Integer(id)) rescue []
|
65
|
+
end
|
49
66
|
|
50
|
-
|
51
|
-
|
67
|
+
def split_num(num)
|
68
|
+
n = Math.log10(num).floor
|
69
|
+
(0..n).map{ |i| (num / 10 ** (n - i)) % 10 }
|
52
70
|
end
|
53
71
|
|
72
|
+
def join_num(id_array)
|
73
|
+
id_array.inject{ |a, i| a * 10 + i }
|
74
|
+
end
|
75
|
+
|
76
|
+
def get_core(id_array)
|
77
|
+
join_num(split_num(id_array).take(9))
|
78
|
+
end
|
54
79
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: turkish_id
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kerem Bozdas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-04-
|
11
|
+
date: 2018-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Validate Turkish Identification Numbers and More!
|
14
14
|
email:
|
@@ -54,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
54
|
version: '0'
|
55
55
|
requirements: []
|
56
56
|
rubyforge_project:
|
57
|
-
rubygems_version: 2.6
|
57
|
+
rubygems_version: 2.7.6
|
58
58
|
signing_key:
|
59
59
|
specification_version: 4
|
60
60
|
summary: Validate Turkish Identification Numbers and More!
|