turkish_id 0.2.2 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b8dca9f48bfca637f6d987a9469d4a30860fe66
4
- data.tar.gz: 66a40925b91acba68bef5a8cfd28e9f6ab0e20d2
3
+ metadata.gz: 15a42720981e14cba171d93f4aae070f10142976
4
+ data.tar.gz: 578b38fcffb08ece46644abd56099be085764203
5
5
  SHA512:
6
- metadata.gz: 7aa6fa00f6f039ad3393c23d8c86d7d6b23f8fd2ca7bf58d3a239cb52f4671647a181092ea561f7bcf1a24d47bb96a4c6a6e3671c5633bb58e93e9cbe207bba3
7
- data.tar.gz: 13fba52300273a7876caa6f266e42330453f338323b4b522552a87440401a36dbec97ff2f2cb3b2d599f0666460da90e39908012eb11d099e170067e2cd1509e
6
+ metadata.gz: f7dba1b5eb2f5c166262a971f5af438f9b78046457ac39a7cc92f42ad7339878625a1a46e5689f5e2ba2afe63abcdf542575aad55abd26b1747e3c66710e59a4
7
+ data.tar.gz: 1b5f8d025248da5f83fd45b6a3a470f0fab9743c59ffe5e4538a4326045a77e2921706d7f795b33c126f6c420e82125b1583258af938eb2a2dbcac74c44b7ff8
data/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  .idea/
19
+ coverage
data/.travis.yml CHANGED
@@ -1,11 +1,25 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.0
3
+ - ruby-head
4
+ - 2.5.1
5
+ - 2.4.4
6
+ - 2.3.7
7
+ - 2.2.10
8
+ - 2.1.8
4
9
  - 1.9.3
5
- install:
6
- - gem uninstall bundler
10
+ before_install:
11
+ - gem update --system
12
+ - gem --version
7
13
  - gem install bundler
8
- - bundle install
9
- addons:
10
- code_climate:
11
- repo_token: ae61bac6e33448d6990e3f36d9f16dbabfcae03d279e2b2226108f39a0212a59
14
+
15
+ env:
16
+ global:
17
+ - CC_TEST_REPORTER_ID=92655ba2c8b47200f80e996afb80c410b9491514845d414d7517108ad35a5178
18
+ before_script:
19
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
20
+ - chmod +x ./cc-test-reporter
21
+ - ./cc-test-reporter before-build
22
+ script:
23
+ - bundle exec rspec
24
+ after_script:
25
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/Gemfile CHANGED
@@ -3,5 +3,8 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in turkish_id.gemspec
4
4
  gemspec
5
5
 
6
- gem 'codeclimate-test-reporter', group: :test, require: nil
7
- gem 'pry', '~> 0.10.3', group: :development, require: nil
6
+ gem 'rake', group: :test
7
+ gem 'simplecov', group: :test, require: false
8
+ gem 'simplecov-console', group: :test, require: false
9
+ gem 'pry', '~> 0.10.3', group: :development, require: nil
10
+
data/README.md CHANGED
@@ -41,13 +41,6 @@ $ turkish_id 10000000078
41
41
  Your identification number is valid.
42
42
  ```
43
43
 
44
- Use ```get_relatives``` method to check relatives:
45
-
46
- ```rb
47
- identity_number.get_relatives(3)
48
- => ["10003000082", "10005999902", "10008999848"]
49
- ```
50
-
51
44
  ## Anatomy of the Turkish ID Number
52
45
 
53
46
  The Turkish Identification Number consists of ```11``` digits.
data/Rakefile CHANGED
@@ -3,3 +3,4 @@ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
  task :default => :spec
6
+
@@ -1,3 +1,3 @@
1
1
  class TurkishId
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/turkish_id.rb CHANGED
@@ -1,78 +1,54 @@
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
-
9
3
  class TurkishId
4
+ attr_reader :id_number, :checksum
10
5
 
11
6
  def initialize(id_number)
12
- begin
13
- @id_number = id_number.digitize # Convert string into array of integers
14
- rescue
15
- @id_number = '' # Suppress error & return false (eventually)
16
- end
17
- end
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)
7
+ @id_number = digitize(id_number)
8
+ @checksum = calculate_checksum(@id_number)
40
9
  end
41
10
 
42
11
  def is_valid?
43
- id = @id_number # +1 brevity point
12
+ id = @id_number # For brevity
44
13
 
45
- # Early elimination, bad length or first digit
14
+ # Early termination, bad length or first digit
46
15
  return false if id.length != 11 || id.first == 0
47
16
 
48
- # Get checksum digits
49
- d10, d11 = TurkishId.get_checksum(id)
17
+ # Calculate checksum if not already calculated
18
+ @checksum ||= calculate_checksum(id_number)
50
19
 
51
20
  # Check if the tenth digit matches the algorithm
52
- return false if id[9] != d10
21
+ return false if id[9] != @checksum[0]
53
22
 
54
23
  # Check if the eleventh digit matches the algorithm
55
- return false if id[10] != d11
24
+ return false if id[10] != @checksum[1]
56
25
 
57
26
  true # All conditions met
58
27
  end
59
28
 
60
- def get_relatives(num, direction='up')
29
+ private
61
30
 
62
- id = self.core.join.to_i # Our core id as integer
31
+ def calculate_checksum(id_number)
32
+ # Calculate the sums of odd and even digits
33
+ odds = id_number.values_at(0, 2, 4, 6, 8).reduce(:+)
34
+ evens = id_number.values_at(1, 3, 5, 7).reduce(:+)
63
35
 
64
- tree = {'up' => 1, 'down' => -1} # + or - based on direction
65
- magic = 131 * 229 * tree[direction] # Eliminates if statement
36
+ # Generate checksum digits
37
+ d10 = ((odds * 7) - evens) % 10
38
+ d11 = (id_number.take(9).reduce(:+) + d10) % 10
66
39
 
67
- relatives = []
40
+ # Return checksum
41
+ [d10, d11]
42
+ rescue
43
+ [] # Unable to calculate checksum, initial value remains unchainged
44
+ end
68
45
 
69
- num.times do
70
- id += magic # Gives us the next relative
71
- relatives << id # Collecting ids in an array
72
- end
46
+ def digitize(id_number)
47
+ # Narrow down scope of accepted types
48
+ return [] unless [String, Integer, Fixnum].include?(id_number.class)
73
49
 
74
- # Traverse relatives and append checksum digits
75
- relatives.map { |r| TurkishId.add_checksum(r.to_s.digitize).join }
50
+ # Convert string of numbers into an array of integers
51
+ id_number.to_s.split('').map { |s| Integer(s) }
76
52
  end
77
53
 
78
54
  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.2
4
+ version: 0.3.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: 2016-04-10 00:00:00.000000000 Z
11
+ date: 2018-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  version: '0'
111
111
  requirements: []
112
112
  rubyforge_project:
113
- rubygems_version: 2.4.6
113
+ rubygems_version: 2.6.14
114
114
  signing_key:
115
115
  specification_version: 4
116
116
  summary: Validate Turkish Identification Numbers and More!