ccheck 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ TAGS
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ # Copyright (c) 2012 Jose Pablo Barrantes
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
@@ -0,0 +1,134 @@
1
+ # CCheck
2
+
3
+ ## Description:
4
+
5
+ Ruby credit cards checker using the Luhn algorithm.
6
+
7
+ ## Problem
8
+
9
+ Before submitting a credit card to a payment gateway it's important that
10
+ we run some sanity checks on the number.
11
+
12
+ A common check that is performed upfront is to validate the card type
13
+ based on the starting digits and length of card number. The main
14
+ patterns that we care about are as follows:
15
+
16
+ +============+=============+===============+
17
+ | Card Type | Begins With | Number Length |
18
+ +============+=============+===============+
19
+ | AMEX | 34 or 37 | 15 |
20
+ +------------+-------------+---------------+
21
+ | Discover | 6011 | 16 |
22
+ +------------+-------------+---------------+
23
+ | MasterCard | 51-55 | 16 |
24
+ +------------+-------------+---------------+
25
+ | Visa | 4 | 13 or 16 |
26
+ +------------+-------------+---------------+
27
+
28
+ All of these card types also generate numbers such that they can be
29
+ validated by the Luhn algorithm, so that's the second check systems
30
+ usually try. The steps are:
31
+
32
+ 1. Starting with the next to last digit and continuing with every other
33
+ digit going back to the beginning of the card, double the digit
34
+ 2. Sum all doubled and untouched digits in the number. For digits
35
+ greater than 9 you will need to split them and sum the independently
36
+ (i.e. `"10", 1 + 0`).
37
+ 3. If that total is a multiple of 10, the number is valid.
38
+
39
+ For example, given the card number `4408 0412 3456 7893`:
40
+
41
+ 1 8 4 0 8 0 4 2 2 6 4 10 6 14 8 18 3
42
+ 2 8+4+0+8+0+4+2+2+6+4+1+0+6+1+4+8+1+8+3 = 70
43
+ 3 70 % 10 == 0
44
+
45
+ Thus that card is valid.
46
+
47
+ Let's try one more, `4417 1234 5678 9112`:
48
+
49
+ 1 8 4 2 7 2 2 6 4 10 6 14 8 18 1 2 2
50
+ 2 8+4+2+7+2+2+6+4+1+0+6+1+4+8+1+8+1+2+2 = 69
51
+ 3 69 % 10 != 0
52
+
53
+ This card is not valid.
54
+
55
+ ## Features
56
+
57
+ Card numbers can be passed in line by line (one set of numbers per
58
+ line). The program will print the card in the following format:
59
+
60
+ TYPE: NUMBERS (VALIDITY)
61
+
62
+ ## Input and Output
63
+
64
+ Given the following credit cards:
65
+
66
+ 4111111111111111
67
+ 4111111111111
68
+ 4012888888881881
69
+ 378282246310005
70
+ 6011111111111117
71
+ 5105105105105100
72
+ 5105 1051 0510 5106
73
+ 9111111111111111
74
+
75
+ It will output the following:
76
+
77
+ VISA: 4111111111111111 (valid)
78
+ VISA: 4111111111111 (invalid)
79
+ VISA: 4012888888881881 (valid)
80
+ AMEX: 378282246310005 (valid)
81
+ Discover: 6011111111111117 (valid)
82
+ MasterCard: 5105105105105100 (valid)
83
+ MasterCard: 5105105105105106 (invalid)
84
+ Unknown: 9111111111111111 (invalid)
85
+
86
+ ## Installation:
87
+
88
+ With RubyGems, open a terminal and type:
89
+
90
+ $ {sudo} gem install ccheck
91
+
92
+ Local installation:
93
+
94
+ $ git clone git@bitbucket.org:jpablobr/ccheck.git
95
+
96
+ then:
97
+
98
+ $ cd ccheck
99
+ $ rake install
100
+
101
+ or with bundler
102
+
103
+ $ cd ccheck
104
+ $ bundle build
105
+ $ bundle install
106
+
107
+ ## Command line Usage:
108
+
109
+ -h --help", "Print this help."
110
+ -v --version", "Print version."
111
+
112
+ examples:
113
+
114
+ $ ccheck 4111111111111111
115
+ $ ccheck 4111111111111111 9111111111111111 '5105 1051 0510 5106'
116
+ $ cat cards-stdin.txt | while read card; do ccheck $card; done
117
+
118
+ Note: Remember to take account of native $SHELL expansion. Proper
119
+ character escaping and the like is required.
120
+
121
+ ## Running Tests
122
+
123
+ $ cd ccheck
124
+ $ rake
125
+
126
+ ## Caveats
127
+
128
+ ruby 1.9.3 is required (see `.rvmrc` file).
129
+
130
+ ## Copyright
131
+
132
+ Copyright (c) 2012 Jose Pablo Barrantes. See [LICENSE][] for details.
133
+
134
+ [license]: https://bitbucket.org/jpablobr/ccheck/src/master/LICENSE
@@ -0,0 +1,13 @@
1
+ require 'rake/testtask'
2
+ require 'bundler/gem_tasks'
3
+
4
+ namespace :test do
5
+ Rake::TestTask.new do |t|
6
+ t.name = "minitest"
7
+ t.libs << 'test'
8
+ t.test_files = ['test/**/test_*.rb']
9
+ t.verbose = true
10
+ end
11
+ end
12
+
13
+ task :default => "test:minitest"
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Stdout/stderr should not buffer output
4
+ $stdout.sync = true
5
+ $stderr.sync = true
6
+
7
+ require_relative '../lib/ccheck'
8
+
9
+ ARGV << '--help' if ARGV.empty?
10
+ CCheck::CLI.new.parse_options(ARGV)
@@ -0,0 +1,18 @@
1
+ # encoding: UTF-8
2
+ require File.expand_path('../lib/ccheck/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'ccheck'
6
+ s.homepage = 'http://github.com/jpablobr/ccheck'
7
+ s.summary = 'A small implementation of the Luhn algorithm.'
8
+ s.require_path = 'lib'
9
+ s.authors = ['Jose Pablo Barrantes']
10
+ s.email = ['xjpablobrx@gmail.com']
11
+ s.version = CCheck::Version
12
+ s.platform = Gem::Platform::RUBY
13
+ s.files = `git ls-files`.split("\n")
14
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
15
+ s.require_path = 'lib'
16
+
17
+ s.add_development_dependency 'minitest', '~> 2.11'
18
+ end
@@ -0,0 +1,43 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module CCheck
3
+ autoload :CLI, 'ccheck/cli'
4
+ autoload :Card, 'ccheck/card'
5
+ autoload :Luhn, 'ccheck/luhn'
6
+
7
+ class << self
8
+
9
+ # Public: Verifies if number is a valid CC number according to
10
+ # Luhn algorithm.
11
+ #
12
+ # number - String
13
+ #
14
+ # Return Boolean
15
+ def valid? number
16
+ Luhn.new(number).valid?
17
+ end
18
+
19
+ # Public: Determines CC type.
20
+ #
21
+ # number - String
22
+ #
23
+ # Return String of CC type (Visa, MC). if not found "Unknown"
24
+ def type number
25
+ Card.new(number).type
26
+ end
27
+
28
+ # Public: Prints to stdout CC info.
29
+ #
30
+ # Input - String or Array
31
+ #
32
+ # If parameter is a String, prints CC info.
33
+ # if parameter is an Array, print their info one by one.
34
+ #
35
+ def print input
36
+ if input.is_a? Array
37
+ input.each { |number| puts Card.new(number).to_s }
38
+ else
39
+ puts Card.new(input).to_s
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,49 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module CCheck
3
+ class Card
4
+ attr_reader :number
5
+
6
+ # Cleans the given raw CC number.
7
+ #
8
+ # raw_number - CC raw String
9
+ #
10
+ def initialize raw_number
11
+ @number = cleanup(raw_number)
12
+ end
13
+
14
+ # Public: Determines CC type/brand.
15
+ #
16
+ # Returns CC type as a String or "Unknown" if not found.
17
+ def type
18
+ case number
19
+ when /^3[47]\d{13}$/ ; return "AMEX"
20
+ when /^4(\d{15}|\d{12})$/ ; return "VISA"
21
+ when /^5\d{15}|36\d{14}$/ ; return "MasterCard"
22
+ when /^6011\d{12}|650\d{13}$/ ; return "Discover"
23
+ else return "Unknown"
24
+ end
25
+ end
26
+
27
+ # Public: Formats CC information as such:
28
+ # TYPE: NUMBERS (VALIDITY)
29
+ #
30
+ # Returns CC information as a String
31
+ def to_s
32
+ "%-28s (%s)\n" % [
33
+ "#{type}: #{number}",
34
+ Luhn.new(number).valid? ? 'valid' : 'invalid'
35
+ ]
36
+ end
37
+
38
+ private
39
+
40
+ # Private: Cleans up the given CC number
41
+ #
42
+ # raw_number - as String
43
+ #
44
+ # Returns CC number as String
45
+ def cleanup raw_number
46
+ raw_number.gsub(/\D/, '')
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module CCheck
3
+ class CLI
4
+ require 'optparse'
5
+
6
+ # Public: Parses user CLI input and sends to CCheck#print the CCs.
7
+ #
8
+ # argv - ARGV Array
9
+ #
10
+ def parse_options argv
11
+ opts = OptionParser.new do |o|
12
+ o.banner = "Usage: ccheck [-v] [-h] [CARD]"
13
+ o.separator ""
14
+ o.on("-h", "--help", "Print this help.") { $stderr.puts(opts) }
15
+ o.on("-v", "--version", "Print version.") { return $stderr.puts(VERSION) }
16
+ o.separator ""
17
+ o.separator "Examples:"
18
+ o.separator "\$ ccheck 4111111111111111"
19
+ o.separator "\$ ccheck 4111111111111111 9111111111111111 '5105 1051 0510 5106'"
20
+ o.separator "\$ cat cards-stdin.txt | while read card; do ccheck \$card; done"
21
+ end
22
+ opts.parse!(argv) rescue return $stderr.puts(opts)
23
+ CCheck.print(argv)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,50 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module CCheck
3
+ class Luhn
4
+ attr_reader :card, :digits
5
+
6
+ # Sets @card and also builds its luhn @digits.
7
+ #
8
+ # card - CC String
9
+ #
10
+ def initialize card
11
+ @card = card
12
+ @digits = luhn_digits
13
+ end
14
+
15
+ # Public: Verifies that the given number is a valid CC.
16
+ # Basically the third step of the Luhn algorighthm.
17
+ # If the result is a multiple of 10, the number is valid.
18
+ #
19
+ # Returns Boolean
20
+ def valid?
21
+ digits.split('').inject(0) do |sum, digit|
22
+ sum + digit.to_i
23
+ end % 10 == 0
24
+ end
25
+
26
+ private
27
+
28
+ # Private: Luhn's algorithm main logic.
29
+ # It perfoms the first two steps.
30
+ #
31
+ # 1. Starting with the next to last digit and continuing with
32
+ # every other digit going back to the beginning of the card,
33
+ # double the digit
34
+ #
35
+ # 2. Sum all doubled and untouched digits in the number. For
36
+ # digits greater than 9 you will need to split them and sum
37
+ # the independently (i.e. `"10", 1 + 0`).
38
+ #
39
+ # Returns String
40
+ def luhn_digits
41
+ digits = ''
42
+ card.reverse.chars.each_with_index do |digit,index|
43
+ digits += digit if index % 2 == 0
44
+ digits += (digit.to_i * 2).to_s if index % 2 == 1
45
+ end
46
+ digits
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module CCheck
3
+ Version = '0.1.0'
4
+ end
@@ -0,0 +1,8 @@
1
+ 4111111111111111
2
+ 4111111111111
3
+ 4012888888881881
4
+ 378282246310005
5
+ 6011111111111117
6
+ 5105105105105100
7
+ 5105 1051 0510 5106
8
+ 9111111111111111
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'test_helper'
3
+ require 'minitest/autorun'
4
+
5
+ module CCheck
6
+ class CardTest < MiniTest::Unit::TestCase
7
+
8
+ def test_returns_the_correct_card_type
9
+ [
10
+ %w[VISA 4111111111111111],
11
+ %w[VISA 4111111111111],
12
+ %w[VISA 4012888888881881],
13
+ %w[AMEX 378282246310005],
14
+ %w[Discover 6011111111111117],
15
+ %w[MasterCard 5105105105105100],
16
+ %w[MasterCard 5105105105105106],
17
+ %w[Unknown 9111111111111111]
18
+ ].each { |card| assert_equal(card[0], Card.new(card[1]).type) }
19
+ end
20
+
21
+ def test_to_s_method_returns_properly_formatted_card
22
+ assert_equal("VISA: 4111111111111111 (valid)\n",
23
+ Card.new('4111111111111111').to_s)
24
+ assert_equal("MasterCard: 5105105105105100 (valid)\n" ,
25
+ Card.new('5105105105105100').to_s)
26
+ assert_equal("MasterCard: 5105105105105106 (invalid)\n" ,
27
+ Card.new('5105 1051 0510 5106').to_s)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'test_helper'
3
+ require 'minitest/autorun'
4
+
5
+ module CCheck
6
+ class CCheckTest < MiniTest::Unit::TestCase
7
+
8
+ def test_returns_the_correct_card_type
9
+ assert_equal 'VISA' , CCheck.type('4111111111111111')
10
+ assert_equal 'VISA' , CCheck.type('4111111111111')
11
+ assert_equal 'VISA' , CCheck.type('4012888888881881')
12
+ assert_equal 'AMEX' , CCheck.type('378282246310005')
13
+ assert_equal 'Discover' , CCheck.type('6011111111111117')
14
+ assert_equal 'MasterCard' , CCheck.type('5105105105105100')
15
+ assert_equal 'MasterCard' , CCheck.type('5105 1051 0510 5106')
16
+ assert_equal 'Unknown' , CCheck.type('9111111111111111')
17
+ end
18
+
19
+ def test_validates_if_card_is_valid_or_invalid
20
+ assert CCheck.valid?('4111111111111111') , 'Invalid card'
21
+ refute CCheck.valid?('4111111111111') , 'valid card'
22
+ assert CCheck.valid?('4012888888881881') , 'Invalid card'
23
+ assert CCheck.valid?('378282246310005') , 'Invalid card'
24
+ assert CCheck.valid?('6011111111111117') , 'Invalid card'
25
+ assert CCheck.valid?('5105105105105100') , 'Invalid card'
26
+ refute CCheck.valid?('5105 1051 0510 5106') , 'valid card'
27
+ refute CCheck.valid?('9111111111111111') , 'valid card'
28
+ end
29
+
30
+ def test_print_method_can_accept_an_array_of_cards
31
+ assert_output(EXP_CARDS_STDOUT) { CCheck.print(CARDS) }
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,16 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'test_helper'
3
+ require 'minitest/autorun'
4
+
5
+ module CCheck
6
+ class CLITest < MiniTest::Unit::TestCase
7
+
8
+ def test_validates_multiple_card_input
9
+ assert_output(EXP_CARDS_STDOUT) { CLI.new.parse_options(CARDS) }
10
+ end
11
+
12
+ def test_validates_single_card_input
13
+ assert_output(EXP_CARD_STDOUT) { CLI.new.parse_options(STDIN_CARD) }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'test_helper'
3
+ require 'minitest/autorun'
4
+
5
+ module CCheck
6
+ class LuhnTest < MiniTest::Unit::TestCase
7
+
8
+ def test_validates_card
9
+ assert Luhn.new('4111111111111111').valid? , 'Invalid card'
10
+ refute Luhn.new('4111111111111').valid? , 'valid card'
11
+ assert Luhn.new('4012888888881881').valid? , 'Invalid card'
12
+ assert Luhn.new('378282246310005').valid? , 'Invalid card'
13
+ assert Luhn.new('6011111111111117').valid? , 'Invalid card'
14
+ assert Luhn.new('5105105105105100').valid? , 'Invalid card'
15
+ refute Luhn.new('5105 1051 0510 5106').valid? , 'valid card'
16
+ refute Luhn.new('9111111111111111').valid? , 'valid card'
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ lib_dir = File.join(File.dirname(__FILE__), '..', 'lib')
4
+ $LOAD_PATH.unshift lib_dir, File.dirname(__FILE__)
5
+
6
+ require 'ccheck'
7
+
8
+ CARDS = [
9
+ '4111111111111111', '4111111111111', '4012888888881881',
10
+ '378282246310005', '6011111111111117', '5105105105105100',
11
+ '5105 1051 0510 5106', '9111111111111111',
12
+ ]
13
+
14
+ EXP_CARDS_STDOUT = <<FORMAT
15
+ VISA: 4111111111111111 (valid)
16
+ VISA: 4111111111111 (invalid)
17
+ VISA: 4012888888881881 (valid)
18
+ AMEX: 378282246310005 (valid)
19
+ Discover: 6011111111111117 (valid)
20
+ MasterCard: 5105105105105100 (valid)
21
+ MasterCard: 5105105105105106 (invalid)
22
+ Unknown: 9111111111111111 (invalid)
23
+ FORMAT
24
+
25
+ STDIN_CARD = ['4111111111111111']
26
+
27
+ EXP_CARD_STDOUT = "VISA: 4111111111111111 (valid)\n"
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ccheck
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jose Pablo Barrantes
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: &71844730 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.11'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *71844730
25
+ description:
26
+ email:
27
+ - xjpablobrx@gmail.com
28
+ executables:
29
+ - ccheck
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - .rvmrc
35
+ - Gemfile
36
+ - LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - bin/ccheck
40
+ - ccheck.gemspec
41
+ - lib/ccheck.rb
42
+ - lib/ccheck/card.rb
43
+ - lib/ccheck/cli.rb
44
+ - lib/ccheck/luhn.rb
45
+ - lib/ccheck/version.rb
46
+ - test/assets/cards-stdin
47
+ - test/ccheck/test_card.rb
48
+ - test/ccheck/test_ccheck.rb
49
+ - test/ccheck/test_cli.rb
50
+ - test/ccheck/test_luhn.rb
51
+ - test/test_helper.rb
52
+ homepage: http://github.com/jpablobr/ccheck
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.17
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: A small implementation of the Luhn algorithm.
76
+ test_files: []
77
+ has_rdoc: