luhn-check 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +13 -3
- data/lib/luhn.rb +26 -16
- data/lib/luhn/version.rb +1 -1
- data/test/luhn_test.rb +30 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33f175922b61819fcd65ab2081def3813d93827e
|
4
|
+
data.tar.gz: 054980845db991000adcdeb11b2800326f9e6592
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 836db051316d9ee9dd4ceb1bbb56342ae7e3881d3094a5a22306103fc550d048cf637bd971ebd1fe0eaf4fe765d084b494a78ef0565d79b8658e15572ecf388f
|
7
|
+
data.tar.gz: a3baa6b42f9766ba1a1d5f7bc0a67c24fa31521a216954add2620a1761f895e6b7f381860ae7c4862c0f038900bb0fe22bd77241a3353750d1ca3e05717fe1be
|
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
# Luhn
|
1
|
+
# Luhn check
|
2
2
|
|
3
|
-
With the luhn-check you can check a number with the Luhn
|
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
|
-
|
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
|
|
data/lib/luhn.rb
CHANGED
@@ -13,20 +13,26 @@ module Luhn
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def calculate_check_digit(number)
|
16
|
-
|
17
|
-
|
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?
|
31
|
+
if i.odd?
|
25
32
|
sum = d * 2
|
26
|
-
if sum > 9
|
27
|
-
split_sum = sum.to_s.split(//)
|
28
|
-
|
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
|
-
|
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
|
-
|
58
|
-
return ary.map { |string| string.to_i }
|
64
|
+
number.to_s.split(//).map(&:to_i)
|
59
65
|
end
|
60
66
|
|
61
|
-
def null?(
|
62
|
-
|
63
|
-
|
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
|
data/lib/luhn/version.rb
CHANGED
data/test/luhn_test.rb
CHANGED
@@ -8,17 +8,35 @@ describe 'Luhn' do
|
|
8
8
|
|
9
9
|
describe 'check_digit' do
|
10
10
|
|
11
|
-
describe '
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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(
|
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.
|
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-
|
11
|
+
date: 2013-09-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|