moeffju-luhn 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color -f d
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in luhn.gemspec
4
+ gemspec
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.
@@ -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
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -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
@@ -0,0 +1,3 @@
1
+ module Luhn
2
+ VERSION = "0.1.0"
3
+ end
@@ -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
@@ -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
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'luhn'
5
+ require 'rspec'
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