turkish_id 0.2.0 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d96231198d925643a7631ec1d6c349b2a0e9ecb0
4
- data.tar.gz: 486a165f4ec17935636c3533e5271a3282d75dc3
3
+ metadata.gz: 4dafdcdcde5e33445080ae67b280d26f0929eb9f
4
+ data.tar.gz: 4811796cd5e291b56f1fd59e2ad53342ea948ae3
5
5
  SHA512:
6
- metadata.gz: bef5b54e4efac51b1812196c4d60fc5a6d963223453022c598622921cfcddff79310725ed8b8fe18647a39a407aa693a34e8f13b8e49dccadde5853a3783eedb
7
- data.tar.gz: 491c4e22eac5b642f3a7cc52088814cae870d8c3dec666993000d57e6734518ab54a56422252624bf7f8b3af94891a53eb6fb20ddf0fe09ca43e04902aa594fc
6
+ metadata.gz: ace44efda6e9e1593e5b2fe5afbd570fb516bd541fc84f7e4788daf433db83b7a7685891523a2d313ac7f19f308bbba67952b5db634aa5d8b50c5a79bd70d164
7
+ data.tar.gz: 141d58cad8451777e9c29c26ec5e12ba4a1bde085ede9b6a6ad9437c0ccf21f49cc8c720943bd530d9eec9d0538538a0fc215f80f16e106bf955522b9d88f236
data/bin/console CHANGED
File without changes
@@ -1,3 +1,3 @@
1
1
  class TurkishId
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/turkish_id.rb CHANGED
@@ -1,33 +1,78 @@
1
1
  require "turkish_id/version"
2
2
 
3
+ class String
4
+ def digitize
5
+ self.split('').map { |s| Integer(s) }
6
+ end
7
+ end
8
+
3
9
  class TurkishId
4
10
 
5
11
  def initialize(id_number)
6
12
  begin
7
- # Convert string into array of integers
8
- @id_number = id_number.split('').map { |s| Integer(s) }
13
+ @id_number = id_number.digitize # Convert string into array of integers
9
14
  rescue
10
15
  @id_number = '' # Suppress error & return false (eventually)
11
16
  end
12
17
  end
13
18
 
19
+
20
+ def core
21
+ @id_number.take(9)
22
+ end
23
+
24
+
25
+ def self.get_checksum(id_core)
26
+
27
+ # Calculate the sums of odd and even digits
28
+ odds = id_core.values_at(0, 2, 4, 6, 8).reduce(:+)
29
+ evens = id_core.values_at(1, 3, 5, 7).reduce(:+)
30
+
31
+ # Generate checksum digits
32
+ d10 = ((odds * 7) - evens) % 10
33
+ d11 = (id_core.reduce(:+) + d10) % 10
34
+
35
+ [d10, d11]
36
+ end
37
+
38
+ def self.add_checksum(id_core)
39
+ id_core + TurkishId.get_checksum(id_core)
40
+ end
41
+
14
42
  def is_valid?
15
43
  id = @id_number # +1 brevity point
16
44
 
17
45
  # Early elimination, bad length or first digit
18
46
  return false if id.length != 11 || id.first == 0
19
47
 
20
- # Calculate the sums of odd and even digits
21
- odds = id.values_at(0, 2, 4, 6, 8).reduce(:+)
22
- evens = id.values_at(1, 3, 5, 7).reduce(:+)
48
+ # Get checksum digits
49
+ d10, d11 = TurkishId.get_checksum(id)
23
50
 
24
51
  # Check if the tenth digit matches the algorithm
25
- return false if id[9] != ((odds * 7) - evens) % 10
52
+ return false if id[9] != d10
26
53
 
27
54
  # Check if the eleventh digit matches the algorithm
28
- return false if id[10] != id[0..9].reduce(:+) % 10
55
+ return false if id[10] != d11
56
+
57
+ true # All conditions met
58
+ end
59
+
60
+ def get_relatives(num, direction='up')
61
+
62
+ id = self.core.join.to_i # Our core id as integer
63
+
64
+ tree = {'up' => 1, 'down' => -1} # + or - based on direction
65
+ magic = 131 * 229 * tree[direction] # Eliminates if statement
66
+
67
+ relatives = []
68
+
69
+ num.times do
70
+ id += magic # Gives us the next relative
71
+ relatives << id # Collecting ids in an array
72
+ end
29
73
 
30
- return true # All conditions met
74
+ # Traverse relatives and append checksum digits
75
+ relatives.map { |r| TurkishId.add_checksum(r.to_s.digitize).join }
31
76
  end
32
77
 
33
78
  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.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kerem Bozdas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-28 00:00:00.000000000 Z
11
+ date: 2016-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler