luhn-check 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: afca8dc8ac4b88b51f3516c0b922b9d33cf3a238
4
- data.tar.gz: 39891f3eacbae1049b5c83712e29096d9b090b5f
3
+ metadata.gz: 33f175922b61819fcd65ab2081def3813d93827e
4
+ data.tar.gz: 054980845db991000adcdeb11b2800326f9e6592
5
5
  SHA512:
6
- metadata.gz: 97fa3e6faa815923805713619260a9f29a2b8afc4c07bc58a72d3d3ce41510c0bba4a9587e7d076035602c0f94054a89d897c6c721ce6163cffcc07ef137f9a2
7
- data.tar.gz: 97c843b929d1200a7889410e88eef40e82102e0e07b5a64795a9c2acb83340c23f9cee34603aebf188679a28fdf10ace46863f6062ce0ff30f10c63c350f8918
6
+ metadata.gz: 836db051316d9ee9dd4ceb1bbb56342ae7e3881d3094a5a22306103fc550d048cf637bd971ebd1fe0eaf4fe765d084b494a78ef0565d79b8658e15572ecf388f
7
+ data.tar.gz: a3baa6b42f9766ba1a1d5f7bc0a67c24fa31521a216954add2620a1761f895e6b7f381860ae7c4862c0f038900bb0fe22bd77241a3353750d1ca3e05717fe1be
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
- # Luhn::Check
1
+ # Luhn check
2
2
 
3
- With the luhn-check you can check a number with the Luhn algoritme.
3
+ With the luhn-check you can check a number with the Luhn algorithm.
4
4
 
5
5
  ## Installation
6
+ ### Version
7
+ `0.0.1`
6
8
 
7
9
  Add this line to your application's Gemfile:
8
10
 
@@ -18,7 +20,15 @@ Or install it yourself as:
18
20
 
19
21
  ## Usage
20
22
 
21
- TODO: Write usage instructions here
23
+ After the gem is installed. There are some helpers available.
24
+
25
+ To check if the number is valid with the Luhn algorithm:
26
+
27
+ ```ruby
28
+ Luhn.valid?(number)
29
+ Luhn.calculate_check_digit(number)
30
+ Luhn.double_digits(number) # Array with digits for checksum
31
+ ```
22
32
 
23
33
  ## Contributing
24
34
 
@@ -13,20 +13,26 @@ module Luhn
13
13
  end
14
14
 
15
15
  def calculate_check_digit(number)
16
- doubles = double_digits(number)
17
- sum_of_all(doubles) unless null?(doubles)
16
+ number_without_check_digit = number.to_s[0...-1] + '0'
17
+ doubles = double_digits(number_without_check_digit.to_i)
18
+ sum = sum_of_all(doubles) unless null?(doubles)
19
+ product = sum * 9
20
+ get_check_digit(product)
21
+ end
22
+
23
+ def get_check_digit(number)
24
+ number.to_s[-1].to_i
18
25
  end
19
26
 
20
27
  def double_digits(number)
21
28
  doubles = []
22
29
  digits = all_digits(number).reverse
23
30
  digits.each_with_index do |d, i|
24
- if i.odd? == true
31
+ if i.odd?
25
32
  sum = d * 2
26
- if sum > 9 || !sum.nil?
27
- split_sum = sum.to_s.split(//)
28
- to_int = split_sum.map { |m| m.to_i }
29
- make_sum = to_int.inject { |sum, n| sum + n }
33
+ if sum && sum > 9
34
+ split_sum = sum.to_s.split(//).map(&:to_i)
35
+ make_sum = split_sum.inject { |sum, n| sum + n }
30
36
  doubles << make_sum
31
37
  else
32
38
  doubles << sum
@@ -41,11 +47,12 @@ module Luhn
41
47
  protected
42
48
 
43
49
  def checksum(number)
44
- check_digit = calculate_check_digit(number)
45
- unless check_digit.nil?
46
- check_digit % 10 == 0 ? true : false
47
- else
50
+ if null?(number)
48
51
  return false
52
+ else
53
+ doubles = double_digits(number)
54
+ sum = sum_of_all(doubles)
55
+ sum % 10 == 0 ? true : false
49
56
  end
50
57
  end
51
58
 
@@ -54,13 +61,16 @@ module Luhn
54
61
  end
55
62
 
56
63
  def all_digits(number)
57
- ary = number.to_s.split(//)
58
- return ary.map { |string| string.to_i }
64
+ number.to_s.split(//).map(&:to_i)
59
65
  end
60
66
 
61
- def null?(doubles)
62
- checked_doubles = doubles.reject { |d| d.eql?(0) }
63
- return true if checked_doubles.empty?
67
+ def null?(number)
68
+ checked_number = all_digits(number).reject! { |d| d.eql?(0) }
69
+ if checked_number.nil?
70
+ return false
71
+ else
72
+ checked_number.empty? ? true : false
73
+ end
64
74
  end
65
75
 
66
76
  end
@@ -1,3 +1,3 @@
1
1
  module Luhn
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -8,17 +8,35 @@ describe 'Luhn' do
8
8
 
9
9
  describe 'check_digit' do
10
10
 
11
- describe '.calculate_check_digit' do
12
-
13
- it 'computes the check digit of the .sum_of_all' do
14
- Luhn.calculate_check_digit(123).must_equal(8)
15
- Luhn.calculate_check_digit(492965255140195).must_equal(60)
16
- Luhn.calculate_check_digit(402400711116634).must_equal(46)
17
- Luhn.calculate_check_digit(199600).must_equal(26)
18
- Luhn.calculate_check_digit(700).must_equal(7)
11
+ describe 'only for valid numbers' do
12
+
13
+ describe '.calculate_check_digit' do
14
+
15
+ it 'computes the check digit' do
16
+ Luhn.calculate_check_digit(79927398713).must_equal(3)
17
+ Luhn.calculate_check_digit(4539085529167499).must_equal(9)
18
+ Luhn.calculate_check_digit(6011312159763625).must_equal(5)
19
+ Luhn.calculate_check_digit(4111111111111111).must_equal(1)
20
+ Luhn.calculate_check_digit(180002230256255).must_equal(5)
21
+ Luhn.calculate_check_digit(6011312159763625).must_equal(5)
22
+ end
19
23
  end
20
24
  end
21
25
 
26
+ describe 'for all numbers' do
27
+
28
+ describe '.get_check_digit' do
29
+
30
+ it 'get last digit of number' do
31
+ Luhn.get_check_digit(123).must_equal(3)
32
+ Luhn.get_check_digit(7992739871).must_equal(1)
33
+ Luhn.get_check_digit(492965255140195).must_equal(5)
34
+ Luhn.get_check_digit(402400711116634).must_equal(4)
35
+ Luhn.get_check_digit(199600).must_equal(0)
36
+ Luhn.get_check_digit(4556974027974373).must_equal(3)
37
+ end
38
+ end
39
+ end
22
40
  end
23
41
 
24
42
  describe '.double_digits' do
@@ -28,10 +46,11 @@ describe 'Luhn' do
28
46
  it 'computes the sum the digits of the sum' do
29
47
  Luhn.double_digits(50).must_equal([0,1])
30
48
  Luhn.double_digits(60).must_equal([0,3])
49
+ Luhn.double_digits(9).must_equal([9])
31
50
  Luhn.double_digits(90).must_equal([0,9])
32
51
  Luhn.double_digits(123).must_equal([3,4,1])
33
- Luhn.double_digits(4992739871).must_equal([1,5,8,9,3,5,2,9,9,8])
34
- Luhn.double_digits(7992739871).must_equal([1,5,8,9,3,5,2,9,9,5])
52
+ Luhn.double_digits(4992739871).must_equal([1, 5, 8, 9, 3, 5, 2, 9, 9, 8])
53
+ Luhn.double_digits(7992739871).must_equal([1, 5, 8, 9, 3, 5, 2, 9, 9, 5])
35
54
  end
36
55
  end
37
56
 
@@ -40,7 +59,7 @@ describe 'Luhn' do
40
59
  it 'computes the double of the digit' do
41
60
  Luhn.double_digits(10).must_equal([0,2])
42
61
  Luhn.double_digits(40).must_equal([0,8])
43
- Luhn.double_digits(50).wont_equal([10])
62
+ Luhn.double_digits(50531).must_equal([1,6,5,0,5])
44
63
  end
45
64
  end
46
65
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: luhn-check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan van der Pas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-24 00:00:00.000000000 Z
11
+ date: 2013-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry