luhn-check 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: afca8dc8ac4b88b51f3516c0b922b9d33cf3a238
4
+ data.tar.gz: 39891f3eacbae1049b5c83712e29096d9b090b5f
5
+ SHA512:
6
+ metadata.gz: 97fa3e6faa815923805713619260a9f29a2b8afc4c07bc58a72d3d3ce41510c0bba4a9587e7d076035602c0f94054a89d897c6c721ce6163cffcc07ef137f9a2
7
+ data.tar.gz: 97c843b929d1200a7889410e88eef40e82102e0e07b5a64795a9c2acb83340c23f9cee34603aebf188679a28fdf10ace46863f6062ce0ff30f10c63c350f8918
@@ -0,0 +1,27 @@
1
+ # Basic
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+
20
+ # OS X
21
+ .DS_Store
22
+ .AppleDouble
23
+ .LSOverride
24
+ Icon
25
+
26
+ # SublimeText project files
27
+ *.sublime-workspace
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in luhn-check.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jan van der Pas
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,29 @@
1
+ # Luhn::Check
2
+
3
+ With the luhn-check you can check a number with the Luhn algoritme.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'luhn-check'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install luhn-check
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 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = 'test/**/*_test.rb'
7
+ end
@@ -0,0 +1,67 @@
1
+ require 'pry'
2
+
3
+ module Luhn
4
+
5
+ class << self
6
+
7
+ def ping
8
+ 'pong'
9
+ end
10
+
11
+ def valid?(number)
12
+ checksum(number)
13
+ end
14
+
15
+ def calculate_check_digit(number)
16
+ doubles = double_digits(number)
17
+ sum_of_all(doubles) unless null?(doubles)
18
+ end
19
+
20
+ def double_digits(number)
21
+ doubles = []
22
+ digits = all_digits(number).reverse
23
+ digits.each_with_index do |d, i|
24
+ if i.odd? == true
25
+ sum = d * 2
26
+ if sum > 9 || !sum.nil?
27
+ split_sum = sum.to_s.split(//)
28
+ to_int = split_sum.map { |m| m.to_i }
29
+ make_sum = to_int.inject { |sum, n| sum + n }
30
+ doubles << make_sum
31
+ else
32
+ doubles << sum
33
+ end
34
+ else
35
+ doubles << d
36
+ end
37
+ end
38
+ return doubles
39
+ end
40
+
41
+ protected
42
+
43
+ def checksum(number)
44
+ check_digit = calculate_check_digit(number)
45
+ unless check_digit.nil?
46
+ check_digit % 10 == 0 ? true : false
47
+ else
48
+ return false
49
+ end
50
+ end
51
+
52
+ def sum_of_all(doubles)
53
+ doubles.inject { |sum, n| sum + n }
54
+ end
55
+
56
+ def all_digits(number)
57
+ ary = number.to_s.split(//)
58
+ return ary.map { |string| string.to_i }
59
+ end
60
+
61
+ def null?(doubles)
62
+ checked_doubles = doubles.reject { |d| d.eql?(0) }
63
+ return true if checked_doubles.empty?
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module Luhn
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'luhn/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "luhn-check"
8
+ 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"
15
+ spec.platform = Gem::Platform::RUBY
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency 'pry'
23
+ spec.add_development_dependency 'minitest'
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ end
@@ -0,0 +1,83 @@
1
+ require 'test_helper'
2
+
3
+ describe 'Luhn' do
4
+
5
+ it 'returns pong' do
6
+ Luhn.ping.must_equal('pong')
7
+ end
8
+
9
+ describe 'check_digit' do
10
+
11
+ describe '.calculate_check_digit' do
12
+
13
+ it 'computes the check digit of the .sum_of_all' do
14
+ Luhn.calculate_check_digit(123).must_equal(8)
15
+ Luhn.calculate_check_digit(492965255140195).must_equal(60)
16
+ Luhn.calculate_check_digit(402400711116634).must_equal(46)
17
+ Luhn.calculate_check_digit(199600).must_equal(26)
18
+ Luhn.calculate_check_digit(700).must_equal(7)
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ describe '.double_digits' do
25
+
26
+ describe 'sum is greater than 9' do
27
+
28
+ it 'computes the sum the digits of the sum' do
29
+ Luhn.double_digits(50).must_equal([0,1])
30
+ Luhn.double_digits(60).must_equal([0,3])
31
+ Luhn.double_digits(90).must_equal([0,9])
32
+ Luhn.double_digits(123).must_equal([3,4,1])
33
+ Luhn.double_digits(4992739871).must_equal([1,5,8,9,3,5,2,9,9,8])
34
+ Luhn.double_digits(7992739871).must_equal([1,5,8,9,3,5,2,9,9,5])
35
+ end
36
+ end
37
+
38
+ describe 'sum is less or equal than 9' do
39
+
40
+ it 'computes the double of the digit' do
41
+ Luhn.double_digits(10).must_equal([0,2])
42
+ Luhn.double_digits(40).must_equal([0,8])
43
+ Luhn.double_digits(50).wont_equal([10])
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ describe 'all 0' do
50
+
51
+ it 'rejects number with all 0' do
52
+ Luhn.valid?(0000000000000000).must_equal(false)
53
+ end
54
+ end
55
+
56
+ describe '.valid?' do
57
+
58
+ describe 'valid number' do
59
+ valid_numbers = [4556974027974373, 4539085529167499, 5247381631434707, 4111111111111111, 5555555555554444,
60
+ 5511828555531982, 371449635398431, 4716165622199, 869940826641794, 180002230256255,
61
+ 3096704907107219, 214937935327366, 6011312159763625, 38442242218311]
62
+ it 'is valid' do
63
+ valid_numbers.each do |n|
64
+ Luhn.valid?(n).must_equal(true)
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+ describe 'invalid number' do
71
+ invalid_numbers = [3844224221831, 601111111111117, 1234123412341234, 1234567890123456, 111111, 22222222222000,
72
+ 1111111111111111, 00010000, 23.67, 0000000000000000]
73
+
74
+ it 'is invalid' do
75
+ invalid_numbers.each do |n|
76
+ Luhn.valid?(n).must_equal(false)
77
+ end
78
+ end
79
+
80
+ end
81
+ end
82
+
83
+ end
@@ -0,0 +1,6 @@
1
+ require 'luhn'
2
+ require 'minitest/unit'
3
+ require 'minitest/spec'
4
+ require 'minitest/autorun'
5
+ require 'minitest/pride'
6
+ require 'pry'
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: luhn-check
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jan van der Pas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Luhn algorithm checker
70
+ email:
71
+ - janvanderpas@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/luhn.rb
82
+ - lib/luhn/version.rb
83
+ - luhn-check.gemspec
84
+ - test/luhn_test.rb
85
+ - test/test_helper.rb
86
+ homepage: https://github.com/JanDintel/luhn-check
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.0.6
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Check a number with the Luhn algorithm
110
+ test_files:
111
+ - test/luhn_test.rb
112
+ - test/test_helper.rb