moeffju-luhn 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +45 -0
- data/Rakefile +2 -0
- data/lib/luhn.rb +31 -0
- data/lib/luhn/version.rb +3 -0
- data/luhn.gemspec +17 -0
- data/spec/luhn_spec.rb +58 -0
- data/spec/spec_helper.rb +5 -0
- metadata +60 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color -f d
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Matthias Bauer <moeffju@moeffju.net>
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Luhn
|
2
|
+
|
3
|
+
Adds methods to Fixnum and String to generate and validate Luhn checksums.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'luhn', :git => 'git://github.com/moeffju/luhn.git'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
If you're not using bundler, you will have to build the gem from source.
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
`Fixnum`:
|
20
|
+
|
21
|
+
```
|
22
|
+
4100410382.luhn? #=> true
|
23
|
+
410041038.luhn #=> 2
|
24
|
+
410041038.luhn! #=> 4100410382
|
25
|
+
|
26
|
+
4100410648.luhn? #=> true
|
27
|
+
410041064.luhn #=> 8
|
28
|
+
410041064.luhn! #=> 4100410648
|
29
|
+
```
|
30
|
+
|
31
|
+
And for `String`s:
|
32
|
+
|
33
|
+
```
|
34
|
+
'4 10041064 8'.luhn? #=> true
|
35
|
+
'4 10041064'.luhn #=> "8"
|
36
|
+
'4 10041064'.luhn! #=> "4 100410648"
|
37
|
+
```
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
1. Fork it
|
42
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
43
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
44
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
45
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/luhn.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require "luhn/version"
|
2
|
+
|
3
|
+
class Fixnum
|
4
|
+
def luhn
|
5
|
+
digits = self.to_s.chars.map(&:to_i)
|
6
|
+
sum = digits.reverse.each_with_index.map{ |x, i| i.even? ? (x * 2).divmod(10).inject(:+) : x }.reverse.inject(:+)
|
7
|
+
(10 - sum % 10)
|
8
|
+
end
|
9
|
+
|
10
|
+
def luhn?
|
11
|
+
self.div(10).luhn == (self % 10)
|
12
|
+
end
|
13
|
+
|
14
|
+
def luhn!
|
15
|
+
self * 10 + luhn
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class String
|
20
|
+
def luhn
|
21
|
+
self.gsub(/\D+/, '').to_i.luhn.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def luhn?
|
25
|
+
self.gsub(/\D+/, '').to_i.luhn?
|
26
|
+
end
|
27
|
+
|
28
|
+
def luhn!
|
29
|
+
self + luhn
|
30
|
+
end
|
31
|
+
end
|
data/lib/luhn/version.rb
ADDED
data/luhn.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/luhn/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Matthias Bauer"]
|
6
|
+
gem.email = ["moeffju@moeffju.net"]
|
7
|
+
gem.description = %q{Adds methods to Fixnum and String to generate and validate Luhn check digits, e.g. for credit card numbers, civic numbers, some account numbers etc. More info: https://en.wikipedia.org/wiki/Luhn_algorithm}
|
8
|
+
gem.summary = %q{Adds methods to Fixnum and String to generate and validate Luhn check digits}
|
9
|
+
gem.homepage = "https://github.com/moeffju/luhn"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "moeffju-luhn"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Luhn::VERSION
|
17
|
+
end
|
data/spec/luhn_spec.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require_relative './spec_helper'
|
2
|
+
|
3
|
+
describe "Luhn" do
|
4
|
+
describe "on Fixnums" do
|
5
|
+
let(:valid) { 4100410382 }
|
6
|
+
let(:valid_check) { 2 }
|
7
|
+
let(:incomplete) { 410041038 }
|
8
|
+
let(:invalid) { 4100410383 }
|
9
|
+
|
10
|
+
it "should validate valid luhn codes" do
|
11
|
+
valid.luhn?.should be_true
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should invalidate invalid luhn codes" do
|
15
|
+
invalid.luhn?.should be_false
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should calculate the correct luhn check digit" do
|
19
|
+
incomplete.luhn.should == valid_check
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should append the correct check digit" do
|
23
|
+
incomplete.luhn!.should == valid
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return Fixnums" do
|
27
|
+
incomplete.luhn!.should be_a Fixnum
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "on Strings" do
|
32
|
+
let(:valid) { '4 10041038 2' }
|
33
|
+
let(:valid_check) { '2' }
|
34
|
+
let(:incomplete) { '410041038' }
|
35
|
+
let(:invalid) { '4100410383' }
|
36
|
+
|
37
|
+
it "should validate valid luhn codes" do
|
38
|
+
valid.luhn?.should be_true
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should invalidate invalid luhn codes" do
|
42
|
+
invalid.luhn?.should be_false
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should calculate the correct luhn check digit" do
|
46
|
+
incomplete.luhn.should == valid_check
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should append the correct check digit" do
|
50
|
+
incomplete.luhn!.should == valid.gsub(/\D+/, '')
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should return Strings" do
|
54
|
+
incomplete.luhn!.should be_a String
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: moeffju-luhn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matthias Bauer
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-16 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! 'Adds methods to Fixnum and String to generate and validate Luhn check
|
15
|
+
digits, e.g. for credit card numbers, civic numbers, some account numbers etc. More
|
16
|
+
info: https://en.wikipedia.org/wiki/Luhn_algorithm'
|
17
|
+
email:
|
18
|
+
- moeffju@moeffju.net
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- .rspec
|
25
|
+
- Gemfile
|
26
|
+
- LICENSE
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- lib/luhn.rb
|
30
|
+
- lib/luhn/version.rb
|
31
|
+
- luhn.gemspec
|
32
|
+
- spec/luhn_spec.rb
|
33
|
+
- spec/spec_helper.rb
|
34
|
+
homepage: https://github.com/moeffju/luhn
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.23
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Adds methods to Fixnum and String to generate and validate Luhn check digits
|
58
|
+
test_files:
|
59
|
+
- spec/luhn_spec.rb
|
60
|
+
- spec/spec_helper.rb
|