luhn-ruby 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +3 -0
- data/lib/luhn.rb +30 -0
- data/luhn-ruby.gemspec +17 -0
- data/spec/unit/luhn_spec.rb +32 -0
- metadata +52 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
Y2UzYWI1NmYwMmY5YmNlMjY5ZjYwYjU2MzhkN2Q2Njg3ODRmM2QwMg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NTcwYmFmZDlkNmMyMjE4OTAwYmViODc0MzAyM2QzNzA5NDEyMGJkYw==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
Y2ZkYzBjNGRkOTIwMzFmNThmOTg0NGNkYjJmOThkZWRjNGZkN2Q3MGRkNzU1
|
10
|
+
YTQ2ZTIxYTk0NmZiNWYzODcwNjk5OTc2YjFhOGU0M2NhODQ4OWMzMjBlZjhm
|
11
|
+
YWNjMDY3MGVmNzMwNGVhZTU5MjZjYzU3YTgyMDQ2NzczYjFkZjk=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NjY3YjZlNDIyOWZjYjU3MDM2OWFjM2ViYzU1MjBlNjc0NjMzYTUxMjAwMWQz
|
14
|
+
NDA0MTNjNmUzOGQyYjhhNWJhZWVkYTg3M2U1ZDU4Nzg1NGRlNTZlOTI4ZDM5
|
15
|
+
OWQwMzgwNDNhMzZjZDM2ODc2MTdhYzY1ZjUxYmJkYWRhZGYwOTk=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2011 Rolf Bjaanes
|
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,29 @@
|
|
1
|
+
# luhn-ruby
|
2
|
+
|
3
|
+
Very simple library to calculate and validate Luhn numbers .
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'luhn-ruby'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install luhn-ruby
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/luhn.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
class Luhn
|
2
|
+
|
3
|
+
def self.checksum(number)
|
4
|
+
products = luhn_doubled(number)
|
5
|
+
sum = products.inject(0) { |t,p| t + sum_of(p) }
|
6
|
+
checksum = 10 - (sum % 10)
|
7
|
+
checksum == 10 ? 0 : checksum
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.luhn_doubled(number)
|
11
|
+
numbers = split_digits(number).reverse
|
12
|
+
numbers.map.with_index do |n,i|
|
13
|
+
i.even? ? n*2 : n*1
|
14
|
+
end.reverse
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.sum_of(number)
|
18
|
+
split_digits(number).inject(:+)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.valid?(number)
|
22
|
+
numbers = split_digits(number)
|
23
|
+
numbers.pop == checksum(numbers.join)
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.split_digits(number)
|
27
|
+
number.to_s.split(//).map(&:to_i)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/luhn-ruby.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.authors = ["Rolf Bjaanes"]
|
5
|
+
gem.email = []
|
6
|
+
gem.license = 'MIT'
|
7
|
+
gem.description = %q{Very simple library to calculate and validate Luhn numbers}
|
8
|
+
gem.summary = %q{Very simple library to calculate and validate Luhn numbers}
|
9
|
+
gem.homepage = "https://github.com/rolfb/luhn-ruby"
|
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 = "luhn-ruby"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = '1.0.0'
|
17
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '../../../lib/luhn')
|
2
|
+
|
3
|
+
describe Luhn do
|
4
|
+
|
5
|
+
it "sums numbers in a digit" do
|
6
|
+
Luhn.sum_of(10).should == 1
|
7
|
+
Luhn.sum_of(22).should == 4
|
8
|
+
Luhn.sum_of(99).should == 18
|
9
|
+
end
|
10
|
+
|
11
|
+
it "doubles numbers according to luhn" do
|
12
|
+
Luhn.luhn_doubled(4992739871).should == [4,18,9,4,7,6,9,16,7,2]
|
13
|
+
Luhn.luhn_doubled(123).should == [2,2,6]
|
14
|
+
Luhn.luhn_doubled(700).should == [14,0,0]
|
15
|
+
end
|
16
|
+
|
17
|
+
it "provides a valid control number based on a number" do
|
18
|
+
Luhn.checksum(123).should == 0
|
19
|
+
Luhn.checksum(12345123451234).should == 8
|
20
|
+
Luhn.checksum(4992739871).should == 6
|
21
|
+
Luhn.checksum(23994700000053866078).should == 3
|
22
|
+
Luhn.checksum(199600).should == 8
|
23
|
+
Luhn.checksum(700).should == 5
|
24
|
+
end
|
25
|
+
|
26
|
+
it "validates valid numbers" do
|
27
|
+
Luhn.valid?(123451234512348).should be_true
|
28
|
+
Luhn.valid?(1996008).should be_true
|
29
|
+
Luhn.valid?(1230).should be_true
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: luhn-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rolf Bjaanes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-19 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Very simple library to calculate and validate Luhn numbers
|
14
|
+
email: []
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- .gitignore
|
20
|
+
- Gemfile
|
21
|
+
- LICENSE
|
22
|
+
- README.md
|
23
|
+
- Rakefile
|
24
|
+
- lib/luhn.rb
|
25
|
+
- luhn-ruby.gemspec
|
26
|
+
- spec/unit/luhn_spec.rb
|
27
|
+
homepage: https://github.com/rolfb/luhn-ruby
|
28
|
+
licenses:
|
29
|
+
- MIT
|
30
|
+
metadata: {}
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project:
|
47
|
+
rubygems_version: 2.0.5
|
48
|
+
signing_key:
|
49
|
+
specification_version: 4
|
50
|
+
summary: Very simple library to calculate and validate Luhn numbers
|
51
|
+
test_files:
|
52
|
+
- spec/unit/luhn_spec.rb
|