luhn-check 0.0.4 → 0.0.5

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: 38fde38a04d580ba1a409e99ccf4d3040ed3ab6c
4
- data.tar.gz: ce07177a330063ee447a983a877372ebc36e1570
3
+ metadata.gz: 0db0669d522c8159314dfa37ca68366ae08aa1d7
4
+ data.tar.gz: 6f202f812d9322c4ebd2736b42fa5792071f7103
5
5
  SHA512:
6
- metadata.gz: c755af25e01c4fc9f1605f0a57503ec3918edf05fe80d3622eb1aa2664b0ec12caf1ebd6f24b5b35edef9a9b44cf2e69855285790425374cf68929ca90b5c7bf
7
- data.tar.gz: 9315757d6e9df937d731ce557a742a6b939ff4873bb0e28f6d3129ec9d05f21948b990625d3d56c2866af3378543e5ea66851b093ca3000c51c567fdc6df27d7
6
+ metadata.gz: f77af2183203a0f98f7bb2c988b2a1972831fc81672df84bd189ce70a5f8d04d3a1d7f41730080cc7b00e729f6329821aa842bdc72b3079dc29f03cd2a3d4626
7
+ data.tar.gz: 91181d2deaa16bccfc3ebfa4b349b755c483e0cb0bd62650b18d2c54bd45bc229f6f7fb6822968d5c4365e2dbe3251f1fbbe11290231db0232a7582700adc873
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ script: 'bundle exec rspec'
3
+ env:
4
+ - TRAVIS_CI=true
5
+ rvm:
6
+ - 1.9.3
7
+ - 2.0.0
data/Gemfile CHANGED
@@ -4,6 +4,7 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  group :development, :test do
7
- gem 'pry'
7
+ gem 'pry', require: false
8
8
  gem 'rspec'
9
+ gem 'coveralls', require: false
9
10
  end
data/README.md CHANGED
@@ -1,41 +1,48 @@
1
1
  # Luhn check
2
2
 
3
- With the luhn-check gem you can validate a number with the Luhn algoritme.
3
+ [![Coverage Status](https://coveralls.io/repos/JanDintel/luhn-check/badge.png?branch=master)](https://coveralls.io/r/JanDintel/luhn-check?branch=master)
4
+ [![Build Status](https://travis-ci.org/JanDintel/luhn-check.png?branch=master)](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
- ###Gemfile
11
+ In your Gemfile:
10
12
 
11
- Since that the namespace in the `lib` directory is different than the name of the gem. You need to require the namespace of the class defined in the `lib` directory.
13
+ ```ruby
14
+ gem 'luhn-check', '~> 0.0.3', require: 'luhn'
15
+ ```
12
16
 
13
- Add this line to your application's Gemfile:
17
+ Or:
14
18
 
15
- gem 'luhn-check', '~> 0.0.3', require: 'luhn'
19
+ $ gem install luhn-check
16
20
 
17
- And then execute:
21
+ ## Usage
18
22
 
19
- $ bundle
23
+ Validate a number:
20
24
 
21
- Or install it yourself as:
25
+ ```ruby
26
+ Luhn.valid?(number) #=> true or false
27
+ Luhn.valid?(4539085529167499) #=> true
28
+ ```
22
29
 
23
- $ gem install luhn-check
30
+ ## Troubleshooting
24
31
 
25
- ## Usage
26
-
27
- After the gem is installed. You can use this method to validate the number with the Luhn algoritme:
32
+ ### Requirements of number
33
+ ```ruby
34
+ LuhnError::RequirementError
35
+ ```
28
36
 
29
- ```ruby
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
- Pretty standard:
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 Pull Request
48
+ 5. Create new pull request
@@ -1,4 +1,11 @@
1
+ require 'luhn/version'
1
2
  require 'luhn/base'
2
3
 
3
4
  module Luhn
5
+ class LuhnError < StandardError; end
6
+ class RequirementError < LuhnError; end
7
+
8
+ def self.valid?(number)
9
+ Luhn::Base.valid?(number)
10
+ end
4
11
  end
@@ -1,64 +1,80 @@
1
- class Base
2
- attr_reader :number_to_validate
1
+ module Luhn
2
+ class Base
3
+ attr_reader :number_to_validate
3
4
 
4
- def initialize(number_to_validate)
5
- @number_to_validate = number_to_validate
6
- end
5
+ def initialize(number_to_validate)
6
+ @number_to_validate = number_to_validate
7
+ number_meets_requirements?
8
+ end
7
9
 
8
- def self.valid?(number)
9
- new(number).validate
10
- end
10
+ def self.valid?(number)
11
+ new(number).validate
12
+ end
11
13
 
12
- def validate
13
- checksum % 10 == 0 if number_meets_requirements?
14
- end
14
+ def validate
15
+ checksum % 10 == 0
16
+ end
15
17
 
16
- def check_digit
17
- checksum.to_s[-1].to_i
18
- end
18
+ def check_digit
19
+ checksum.to_s[-1].to_i
20
+ end
19
21
 
20
- def checksum
21
- @checksum ||= double_digit_on_even_position.inject(:+)
22
- end
22
+ def checksum
23
+ @checksum ||= double_digit_on_even_position.inject(:+)
24
+ end
23
25
 
24
- def double_digit_on_even_position
25
- total = []
26
- # Offset of 1, so you can call even? instead of odd? on a number_position
27
- digits_of_number_to_validate.to_enum.with_index(1).each do |digit, number_position|
28
- if number_position.even? && product_of_digit_exceeds_nine(digit)
29
- digits_of_product = split_to_number_array multiply(digit)
30
- total << digits_of_product.inject(:+)
31
- elsif number_position.even? && !product_of_digit_exceeds_nine(digit)
32
- total << multiply(digit)
33
- else
34
- total << digit
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
- private
42
+ private
41
43
 
42
- def number_meets_requirements?
43
- true unless number_to_validate.class == String ||
44
- number_to_validate == 0
45
- end
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
- def digits_of_number_to_validate
48
- # Reverse number_to_validate to start
49
- # the itteration from right to left
50
- split_to_number_array(number_to_validate).reverse
51
- end
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
- def split_to_number_array(numbers)
54
- numbers.to_s.split(//).map(&:to_i)
55
- end
56
+ def split_to_number_array(numbers)
57
+ numbers.to_s.split(//).map(&:to_i)
58
+ end
56
59
 
57
- def product_of_digit_exceeds_nine(digit)
58
- multiply(digit) > 9
59
- end
60
+ def product_of_digit_exceeds_nine(digit)
61
+ multiply(digit) > 9
62
+ end
60
63
 
61
- def multiply(digit)
62
- digit * 2
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
@@ -1,3 +1,3 @@
1
1
  module Luhn
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -1,24 +1,25 @@
1
- # coding: utf-8
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 = "luhn-check"
8
+ spec.name = 'luhn-check'
8
9
  spec.version = Luhn::VERSION
9
- spec.authors = ["Jan van der Pas"]
10
- spec.email = ["janvanderpas@gmail.com"]
11
- spec.description = %q{Luhn algorithm checker}
12
- spec.summary = %q{Check a number with the Luhn algorithm}
13
- spec.homepage = "https://github.com/JanDintel/luhn-check"
14
- spec.license = "MIT"
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 = ["lib"]
21
+ spec.require_paths = ['lib']
21
22
 
22
- spec.add_development_dependency "bundler", "~> 1.3"
23
- spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'rake'
24
25
  end
@@ -1,36 +1,32 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Base do
4
- let(:base) { described_class.new(12345) }
3
+ describe Luhn::Base do
4
+ subject { described_class.new(12345) }
5
5
 
6
- describe 'attributes' do
7
- it 'responds to attribute' do
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 'validate numbers' do
12
+ context 'valid numbers' do
17
13
  # Different test creditcardnumbers from: Visa,
18
14
  # American Express, Diners Club and Mastercard
19
- let(:validate_numbers) { [4556974027974373, 4539085529167499, 5247381631434707, 4111111111111111,
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
- validate_numbers.each do |number|
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, 111111,
33
- 22222222222000, 1111111111111111, 00010000, 23.67, 0000000000000000, '123']}
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 '.validate' do
45
- describe 'validates the checksum' do
46
- context 'modulo 10 is equal to 0' do
47
- before { described_class.any_instance.
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
- it 'returns true' do
51
- expect(base.validate).to be_true
52
- end
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 'modulo 10 is NOT equal to 0' do
56
- before { described_class.any_instance.
57
- stub(:checksum).and_return 12 }
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
- it 'returns false' do
60
- expect(base.validate).to be_false
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 { described_class.any_instance.
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(base.check_digit).to eql 6
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(base.checksum).to eql 15
87
+ expect(subject.checksum).to eql 15
82
88
  end
83
89
  end
84
90
 
@@ -2,7 +2,10 @@ require 'spec_helper'
2
2
 
3
3
  describe Luhn do
4
4
 
5
- describe Base do
6
- it { should be_true }
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
@@ -1,3 +1,6 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
1
4
  require 'luhn'
2
5
 
3
6
  RSpec.configure do |config|
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
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-25 00:00:00.000000000 Z
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 algorithm checker
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: Check a number with the Luhn algorithm
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