luhn-check 0.0.4 → 0.0.5
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/.travis.yml +7 -0
- data/Gemfile +2 -1
- data/README.md +28 -21
- data/lib/luhn.rb +7 -0
- data/lib/luhn/base.rb +64 -48
- data/lib/luhn/version.rb +1 -1
- data/luhn-check.gemspec +12 -11
- data/spec/lib/luhn/base_spec.rb +37 -31
- data/spec/lib/luhn_spec.rb +5 -2
- data/spec/spec_helper.rb +3 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0db0669d522c8159314dfa37ca68366ae08aa1d7
|
4
|
+
data.tar.gz: 6f202f812d9322c4ebd2736b42fa5792071f7103
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f77af2183203a0f98f7bb2c988b2a1972831fc81672df84bd189ce70a5f8d04d3a1d7f41730080cc7b00e729f6329821aa842bdc72b3079dc29f03cd2a3d4626
|
7
|
+
data.tar.gz: 91181d2deaa16bccfc3ebfa4b349b755c483e0cb0bd62650b18d2c54bd45bc229f6f7fb6822968d5c4365e2dbe3251f1fbbe11290231db0232a7582700adc873
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,41 +1,48 @@
|
|
1
1
|
# Luhn check
|
2
2
|
|
3
|
-
|
3
|
+
[](https://coveralls.io/r/JanDintel/luhn-check?branch=master)
|
4
|
+
[](https://travis-ci.org/JanDintel/luhn-check)
|
5
|
+
|
6
|
+
With the luhn-check gem you can validate a number with the Luhn algorithm.
|
7
|
+
This is commonly used to check validity of creditcard numbers and others.
|
4
8
|
|
5
9
|
## Installation
|
6
|
-
### Version
|
7
|
-
`0.0.3`
|
8
10
|
|
9
|
-
|
11
|
+
In your Gemfile:
|
10
12
|
|
11
|
-
|
13
|
+
```ruby
|
14
|
+
gem 'luhn-check', '~> 0.0.3', require: 'luhn'
|
15
|
+
```
|
12
16
|
|
13
|
-
|
17
|
+
Or:
|
14
18
|
|
15
|
-
gem
|
19
|
+
$ gem install luhn-check
|
16
20
|
|
17
|
-
|
21
|
+
## Usage
|
18
22
|
|
19
|
-
|
23
|
+
Validate a number:
|
20
24
|
|
21
|
-
|
25
|
+
```ruby
|
26
|
+
Luhn.valid?(number) #=> true or false
|
27
|
+
Luhn.valid?(4539085529167499) #=> true
|
28
|
+
```
|
22
29
|
|
23
|
-
|
30
|
+
## Troubleshooting
|
24
31
|
|
25
|
-
|
26
|
-
|
27
|
-
|
32
|
+
### Requirements of number
|
33
|
+
```ruby
|
34
|
+
LuhnError::RequirementError
|
35
|
+
```
|
28
36
|
|
29
|
-
|
30
|
-
Luhn.valid?(number)
|
31
|
-
```
|
37
|
+
The number you want to validate must be a least:
|
32
38
|
|
39
|
+
* From the `Fixnum` class
|
40
|
+
* Positive
|
41
|
+
* Not 0 (zero)
|
33
42
|
|
34
43
|
## Contributing
|
35
|
-
|
36
|
-
|
37
|
-
1. Fork it
|
44
|
+
1. Fork
|
38
45
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
46
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
40
47
|
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
-
5. Create new
|
48
|
+
5. Create new pull request
|
data/lib/luhn.rb
CHANGED
data/lib/luhn/base.rb
CHANGED
@@ -1,64 +1,80 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Luhn
|
2
|
+
class Base
|
3
|
+
attr_reader :number_to_validate
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
def initialize(number_to_validate)
|
6
|
+
@number_to_validate = number_to_validate
|
7
|
+
number_meets_requirements?
|
8
|
+
end
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
10
|
+
def self.valid?(number)
|
11
|
+
new(number).validate
|
12
|
+
end
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
14
|
+
def validate
|
15
|
+
checksum % 10 == 0
|
16
|
+
end
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
|
18
|
+
def check_digit
|
19
|
+
checksum.to_s[-1].to_i
|
20
|
+
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
22
|
+
def checksum
|
23
|
+
@checksum ||= double_digit_on_even_position.inject(:+)
|
24
|
+
end
|
23
25
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
26
|
+
def double_digit_on_even_position
|
27
|
+
total = []
|
28
|
+
# Offset of 1, so you can call even? instead of odd? on a number_position
|
29
|
+
digits_of_number_to_validate.to_enum.with_index(1).each do |digit, number_position|
|
30
|
+
if number_position.even? && product_of_digit_exceeds_nine(digit)
|
31
|
+
digits_of_product = split_to_number_array multiply(digit)
|
32
|
+
total << digits_of_product.inject(:+)
|
33
|
+
elsif number_position.even? && !product_of_digit_exceeds_nine(digit)
|
34
|
+
total << multiply(digit)
|
35
|
+
else
|
36
|
+
total << digit
|
37
|
+
end
|
35
38
|
end
|
39
|
+
total
|
36
40
|
end
|
37
|
-
total
|
38
|
-
end
|
39
41
|
|
40
|
-
|
42
|
+
private
|
41
43
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
44
|
+
def number_meets_requirements?
|
45
|
+
raise_fixnum_error unless number_to_validate.class == Fixnum
|
46
|
+
raise_zero_error if number_to_validate.zero?
|
47
|
+
raise_negative_error if number_to_validate < 0
|
48
|
+
end
|
46
49
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
50
|
+
def digits_of_number_to_validate
|
51
|
+
# Reverse number_to_validate to start
|
52
|
+
# the itteration from right to left
|
53
|
+
split_to_number_array(number_to_validate).reverse
|
54
|
+
end
|
52
55
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
+
def split_to_number_array(numbers)
|
57
|
+
numbers.to_s.split(//).map(&:to_i)
|
58
|
+
end
|
56
59
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
+
def product_of_digit_exceeds_nine(digit)
|
61
|
+
multiply(digit) > 9
|
62
|
+
end
|
60
63
|
|
61
|
-
|
62
|
-
|
64
|
+
def multiply(digit)
|
65
|
+
digit * 2
|
66
|
+
end
|
67
|
+
|
68
|
+
def raise_fixnum_error
|
69
|
+
raise(Luhn::RequirementError, 'Number must be of Fixnum')
|
70
|
+
end
|
71
|
+
|
72
|
+
def raise_zero_error
|
73
|
+
raise(Luhn::RequirementError, 'Number is not allowed to be 0 (zero)')
|
74
|
+
end
|
75
|
+
|
76
|
+
def raise_negative_error
|
77
|
+
raise(Luhn::RequirementError, 'Number is not allowed to be negative')
|
78
|
+
end
|
63
79
|
end
|
64
80
|
end
|
data/lib/luhn/version.rb
CHANGED
data/luhn-check.gemspec
CHANGED
@@ -1,24 +1,25 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
4
5
|
require 'luhn/version'
|
5
6
|
|
6
7
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
8
|
+
spec.name = 'luhn-check'
|
8
9
|
spec.version = Luhn::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.description =
|
12
|
-
spec.summary =
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
10
|
+
spec.authors = ['Jan van der Pas']
|
11
|
+
spec.email = ['janvanderpas@gmail.com']
|
12
|
+
spec.description = 'A Ruby gem to validate numbers with the Luhn algoritme'
|
13
|
+
spec.summary = "With the luhn-check gem you can validate a number with the Luhn algorithm. This is commonly used to check validity of creditcard numbers and others."
|
14
|
+
spec.homepage = 'https://github.com/JanDintel/luhn-check'
|
15
|
+
spec.license = 'MIT'
|
15
16
|
spec.platform = Gem::Platform::RUBY
|
16
17
|
|
17
18
|
spec.files = `git ls-files`.split($/)
|
18
19
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
20
|
spec.test_files = spec.files.grep(%r{^(spec)/})
|
20
|
-
spec.require_paths = [
|
21
|
+
spec.require_paths = ['lib']
|
21
22
|
|
22
|
-
spec.add_development_dependency
|
23
|
-
spec.add_development_dependency
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
|
+
spec.add_development_dependency 'rake'
|
24
25
|
end
|
data/spec/lib/luhn/base_spec.rb
CHANGED
@@ -1,36 +1,32 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Base do
|
4
|
-
|
3
|
+
describe Luhn::Base do
|
4
|
+
subject { described_class.new(12345) }
|
5
5
|
|
6
|
-
describe 'attributes' do
|
7
|
-
it
|
8
|
-
[:number_to_validate].each do |attr|
|
9
|
-
expect(base).to respond_to attr
|
10
|
-
end
|
11
|
-
end
|
6
|
+
describe 'response to attributes' do
|
7
|
+
it { should respond_to :number_to_validate }
|
12
8
|
end
|
13
9
|
|
14
10
|
## Class methods
|
15
11
|
describe '.valid?' do
|
16
|
-
context '
|
12
|
+
context 'valid numbers' do
|
17
13
|
# Different test creditcardnumbers from: Visa,
|
18
14
|
# American Express, Diners Club and Mastercard
|
19
|
-
let(:
|
15
|
+
let(:valid_numbers) { [4556974027974373, 4539085529167499, 5247381631434707, 4111111111111111,
|
20
16
|
5555555555554444, 5511828555531982, 371449635398431, 4716165622199,
|
21
17
|
869940826641794, 180002230256255, 3096704907107219, 214937935327366,
|
22
18
|
6011312159763625, 38442242218311] }
|
23
19
|
|
24
20
|
it 'returns true' do
|
25
|
-
|
21
|
+
valid_numbers.each do |number|
|
26
22
|
expect(described_class.valid?(number)).to be_true
|
27
23
|
end
|
28
24
|
end
|
29
25
|
end
|
30
26
|
|
31
27
|
context 'invalid numbers' do
|
32
|
-
let(:invalid_numbers) { [3844224221831, 601111111111117, 1234123412341234, 1234567890123456,
|
33
|
-
22222222222000, 1111111111111111, 00010000
|
28
|
+
let(:invalid_numbers) { [3844224221831, 601111111111117, 1234123412341234, 1234567890123456,
|
29
|
+
111111, 22222222222000, 1111111111111111, 00010000]}
|
34
30
|
|
35
31
|
it 'returns false' do
|
36
32
|
invalid_numbers.each do |number|
|
@@ -41,34 +37,44 @@ describe Base do
|
|
41
37
|
end
|
42
38
|
|
43
39
|
## Instance methods
|
44
|
-
describe '
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
stub(:checksum).and_return 60 }
|
40
|
+
describe 'initializing' do
|
41
|
+
context 'number meets the requirements' do
|
42
|
+
specify { expect(subject).to be_true }
|
43
|
+
end
|
49
44
|
|
50
|
-
|
51
|
-
|
52
|
-
|
45
|
+
context 'number does NOT meet the requirements' do
|
46
|
+
context 'is not a Fixnum' do
|
47
|
+
specify { expect{ described_class.new(23.67) }.to raise_error(Luhn::RequirementError, 'Number must be of Fixnum') }
|
48
|
+
specify { expect{ described_class.new('123') }.to raise_error(Luhn::RequirementError, 'Number must be of Fixnum') }
|
53
49
|
end
|
54
50
|
|
55
|
-
context '
|
56
|
-
|
57
|
-
|
51
|
+
context 'number is 0 (zero)' do
|
52
|
+
specify { expect{ described_class.new(0) }.to raise_error(Luhn::RequirementError, 'Number is not allowed to be 0 (zero)') }
|
53
|
+
end
|
58
54
|
|
59
|
-
|
60
|
-
|
61
|
-
end
|
55
|
+
context 'number is negative' do
|
56
|
+
specify { expect{ described_class.new(-123) }.to raise_error(Luhn::RequirementError, 'Number is not allowed to be negative') }
|
62
57
|
end
|
63
58
|
end
|
64
59
|
end
|
65
60
|
|
61
|
+
describe '.validate' do
|
62
|
+
context 'modulo 10 is equal to 0' do
|
63
|
+
before { described_class.any_instance.stub(:checksum).and_return 60 }
|
64
|
+
specify { expect(subject.validate).to be_true }
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'modulo 10 is NOT equal to 0' do
|
68
|
+
before { described_class.any_instance.stub(:checksum).and_return 12 }
|
69
|
+
specify { expect(subject.validate).to be_false }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
66
73
|
describe '.check_digit' do
|
67
|
-
before
|
68
|
-
stub(:checksum).and_return 126 }
|
74
|
+
before { described_class.any_instance.stub(:checksum).and_return 126 }
|
69
75
|
|
70
76
|
it 'calculates the check_digit out of the checksum' do
|
71
|
-
expect(
|
77
|
+
expect(subject.check_digit).to eql 6
|
72
78
|
end
|
73
79
|
end
|
74
80
|
|
@@ -78,7 +84,7 @@ describe Base do
|
|
78
84
|
and_return [1,2,3,4,5] }
|
79
85
|
|
80
86
|
it 'sums up the double_digit_on_even_position' do
|
81
|
-
expect(
|
87
|
+
expect(subject.checksum).to eql 15
|
82
88
|
end
|
83
89
|
end
|
84
90
|
|
data/spec/lib/luhn_spec.rb
CHANGED
@@ -2,7 +2,10 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Luhn do
|
4
4
|
|
5
|
-
describe
|
6
|
-
it
|
5
|
+
describe '.valid?' do
|
6
|
+
it 'delegates the method to the Base class' do
|
7
|
+
expect(Luhn::Base).to receive(:valid?).with(12345)
|
8
|
+
described_class.valid?(12345)
|
9
|
+
end
|
7
10
|
end
|
8
11
|
end
|
data/spec/spec_helper.rb
CHANGED
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.5
|
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-
|
11
|
+
date: 2013-12-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,7 +38,7 @@ dependencies:
|
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
description: Luhn
|
41
|
+
description: A Ruby gem to validate numbers with the Luhn algoritme
|
42
42
|
email:
|
43
43
|
- janvanderpas@gmail.com
|
44
44
|
executables: []
|
@@ -47,6 +47,7 @@ extra_rdoc_files: []
|
|
47
47
|
files:
|
48
48
|
- .gitignore
|
49
49
|
- .rspec
|
50
|
+
- .travis.yml
|
50
51
|
- Gemfile
|
51
52
|
- LICENSE.txt
|
52
53
|
- README.md
|
@@ -81,7 +82,8 @@ rubyforge_project:
|
|
81
82
|
rubygems_version: 2.1.11
|
82
83
|
signing_key:
|
83
84
|
specification_version: 4
|
84
|
-
summary:
|
85
|
+
summary: With the luhn-check gem you can validate a number with the Luhn algorithm.
|
86
|
+
This is commonly used to check validity of creditcard numbers and others.
|
85
87
|
test_files:
|
86
88
|
- spec/lib/luhn/base_spec.rb
|
87
89
|
- spec/lib/luhn_spec.rb
|