luhn_credit_card 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +35 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/lib/luhn_credit_card/luhn_validator.rb +17 -0
- data/lib/luhn_credit_card/number_cleaner.rb +5 -0
- data/lib/luhn_credit_card/pretty_printer.rb +5 -0
- data/lib/luhn_credit_card/text_file_printer.rb +9 -0
- data/lib/luhn_credit_card/type_finder.rb +18 -0
- data/lib/luhn_credit_card.rb +33 -0
- data/luhn_credit_card.gemspec +20 -0
- data/spec/luhn_credit_card/luhn_credit_card_spec.rb +162 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/support/crazy_file.txt +4 -0
- data/spec/support/credit_cards.txt +8 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 63383f816dc0e515e55db1d773d1aa5635bf0a86
|
4
|
+
data.tar.gz: 0b7a256a8786dba7acd198b77b604ad50cb5ba13
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5f37e0d22febd1995f26e12dabbbc1f63a9c54844319e8e1ef18d672006038fb1bb51ad4ed004fe8b9576fe3bcca18fa48b6d3bf47d1c21814c73cc061766c03
|
7
|
+
data.tar.gz: fabb95b0905f8dafb8657c4c0799669832fd29fd532e2dfa721ff1a5a7ed414049d47940e1c8d44e4332bd3d2339b3ae864f5d025da92182a1823c04ac2652a1
|
data/.gitignore
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/lib/bundler/man/
|
26
|
+
|
27
|
+
# for a library or gem, you might want to ignore these files since the code is
|
28
|
+
# intended to run in multiple environments; otherwise, check them in:
|
29
|
+
# Gemfile.lock
|
30
|
+
# .ruby-version
|
31
|
+
# .ruby-gemset
|
32
|
+
|
33
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
34
|
+
.rvmrc
|
35
|
+
.ruby-version
|
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.3
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class LuhnValidator
|
2
|
+
def initialize(number)
|
3
|
+
@number = number
|
4
|
+
end
|
5
|
+
|
6
|
+
def valid?
|
7
|
+
number_sum % 10 == 0
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def number_sum
|
13
|
+
@number.split('').reverse.map.each_with_index do |number, i|
|
14
|
+
((i + 1).even? ? (number.to_i * 2) : number).to_s.split('')
|
15
|
+
end.flatten.map(&:to_i).reduce(:+)
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class TypeFinder
|
2
|
+
CHARACTERISTICS = {
|
3
|
+
'AMEX' => { begins_with: ['34', '37'], length: [15] },
|
4
|
+
'Discover' => { begins_with: ['6011'], length: [16] },
|
5
|
+
'MasterCard' => { begins_with: ('51'..'55').to_a, length: [16] },
|
6
|
+
'VISA' => { begins_with: ['4'], length: [13, 16] }
|
7
|
+
}
|
8
|
+
|
9
|
+
def self.for(number)
|
10
|
+
CHARACTERISTICS.detect do |card, settings|
|
11
|
+
return card if settings[:begins_with].detect {
|
12
|
+
|n_start| number.start_with? n_start
|
13
|
+
} && settings[:length].include?(number.size)
|
14
|
+
end
|
15
|
+
|
16
|
+
'Unknown'
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'luhn_credit_card/number_cleaner'
|
2
|
+
require 'luhn_credit_card/text_file_printer'
|
3
|
+
require 'luhn_credit_card/type_finder'
|
4
|
+
require 'luhn_credit_card/luhn_validator'
|
5
|
+
|
6
|
+
class LuhnCreditCard
|
7
|
+
attr_reader :number
|
8
|
+
|
9
|
+
def self.pretty_print_from_file(file_path)
|
10
|
+
begin
|
11
|
+
TextFilePrinter.parse(file_path)
|
12
|
+
rescue => e
|
13
|
+
# Log the error
|
14
|
+
nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(number)
|
19
|
+
@number = NumberCleaner.clean(number)
|
20
|
+
end
|
21
|
+
|
22
|
+
def type
|
23
|
+
TypeFinder.for(number)
|
24
|
+
end
|
25
|
+
|
26
|
+
def valid?
|
27
|
+
LuhnValidator.new(number).valid?
|
28
|
+
end
|
29
|
+
|
30
|
+
def status
|
31
|
+
valid? ? :valid : :invalid
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "luhn_credit_card"
|
6
|
+
spec.version = '0.0.2'
|
7
|
+
spec.authors = ["Oswaldo Ferreira"]
|
8
|
+
spec.email = ["oswluizf@gmail.com"]
|
9
|
+
spec.summary = %q{Credit card validations using Luhn Algorithm.}
|
10
|
+
spec.description = %q{It's a simple tool that provides helper methods for credit card number validations using Luhn Algorithm.}
|
11
|
+
spec.license = "MIT"
|
12
|
+
|
13
|
+
spec.files = `git ls-files -z`.split("\x0")
|
14
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
15
|
+
spec.require_paths = ["lib"]
|
16
|
+
|
17
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
18
|
+
spec.add_development_dependency 'rspec', '~> 3.1', '>= 3.1.0'
|
19
|
+
spec.add_development_dependency 'simplecov', '~> 0.9'
|
20
|
+
end
|
@@ -0,0 +1,162 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe LuhnCreditCard do
|
4
|
+
context 'initial attributes' do
|
5
|
+
describe '#number' do
|
6
|
+
context 'when spaced number' do
|
7
|
+
let(:credit_card) { LuhnCreditCard.new('3782 8224 6310 005') }
|
8
|
+
|
9
|
+
it 'returns plain number' do
|
10
|
+
expect(credit_card.number).to eq '378282246310005'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'when number ends with \n' do
|
15
|
+
let(:credit_card) { LuhnCreditCard.new('378282246310005\n') }
|
16
|
+
|
17
|
+
it 'returns plain number' do
|
18
|
+
expect(credit_card.number).to eq '378282246310005'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when spaced number and ends with \n' do
|
23
|
+
let(:credit_card) { LuhnCreditCard.new('3782 8224 6310 005\n') }
|
24
|
+
|
25
|
+
it 'returns plain number' do
|
26
|
+
expect(credit_card.number).to eq '378282246310005'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#valid?' do
|
33
|
+
context 'AMEX' do
|
34
|
+
it 'when valid' do
|
35
|
+
credit_card = LuhnCreditCard.new('3782 8224 6310 005')
|
36
|
+
expect(credit_card.valid?).to eq true
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'when invalid' do
|
40
|
+
credit_card = LuhnCreditCard.new('3782 8224 6310 0051')
|
41
|
+
expect(credit_card.valid?).to eq false
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'VISA' do
|
46
|
+
it 'when valid' do
|
47
|
+
credit_card = LuhnCreditCard.new('4111111111111111')
|
48
|
+
expect(credit_card.valid?).to eq true
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'when invalid' do
|
52
|
+
credit_card = LuhnCreditCard.new('4111111111111')
|
53
|
+
expect(credit_card.valid?).to eq false
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'MasterCard' do
|
58
|
+
it 'when valid' do
|
59
|
+
credit_card = LuhnCreditCard.new('5105105105105100')
|
60
|
+
expect(credit_card.valid?).to eq true
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'when invalid' do
|
64
|
+
credit_card = LuhnCreditCard.new('5105105105105106')
|
65
|
+
expect(credit_card.valid?).to eq false
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'Discover' do
|
70
|
+
it 'when valid' do
|
71
|
+
credit_card = LuhnCreditCard.new('6011111111111117')
|
72
|
+
expect(credit_card.valid?).to eq true
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'when invalid' do
|
76
|
+
credit_card = LuhnCreditCard.new('6011 1111 1111 1127')
|
77
|
+
expect(credit_card.valid?).to eq false
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe '#status' do
|
83
|
+
let(:number) { 'whatever 123' }
|
84
|
+
let(:credit_card) { LuhnCreditCard.new(number) }
|
85
|
+
|
86
|
+
it 'when true' do
|
87
|
+
allow(credit_card).to receive(:valid?).and_return(true)
|
88
|
+
|
89
|
+
expect(credit_card.status).to eq :valid
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'when false' do
|
93
|
+
allow(credit_card).to receive(:valid?).and_return(false)
|
94
|
+
|
95
|
+
expect(credit_card.status).to eq :invalid
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe '#type' do
|
100
|
+
it 'when AMEX' do
|
101
|
+
credit_card = LuhnCreditCard.new('378282246310005')
|
102
|
+
expect(credit_card.type).to eq 'AMEX'
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'when Discover' do
|
106
|
+
credit_card = LuhnCreditCard.new('6011111111111117')
|
107
|
+
expect(credit_card.type).to eq 'Discover'
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'when MasterCard' do
|
111
|
+
credit_card = LuhnCreditCard.new('5105 1051 0510 5106')
|
112
|
+
expect(credit_card.type).to eq 'MasterCard'
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'when VISA' do
|
116
|
+
credit_card = LuhnCreditCard.new('4012888888881881')
|
117
|
+
expect(credit_card.type).to eq 'VISA'
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'when Unknown' do
|
121
|
+
credit_card = LuhnCreditCard.new('9111111111111111')
|
122
|
+
expect(credit_card.type).to eq 'Unknown'
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe '.pretty_print_from_file(path)' do
|
127
|
+
context 'when path is provided' do
|
128
|
+
let(:file_path) do
|
129
|
+
File.join(File.dirname(__FILE__), '..', 'support', 'credit_cards.txt')
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'returns formated string with validation info' do
|
133
|
+
expect(LuhnCreditCard.pretty_print_from_file(file_path)).to eq(
|
134
|
+
"VISA: 4111111111111111 (valid)
|
135
|
+
VISA: 4111111111111 (invalid)
|
136
|
+
VISA: 4012888888881881 (valid)
|
137
|
+
AMEX: 378282246310005 (valid)
|
138
|
+
Discover: 6011111111111117 (valid)
|
139
|
+
MasterCard: 5105105105105100 (valid)
|
140
|
+
MasterCard: 5105105105105106 (invalid)
|
141
|
+
Unknown: 9111111111111111 (invalid)"
|
142
|
+
)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
context 'when path is not provided' do
|
147
|
+
it 'returns nil' do
|
148
|
+
expect(LuhnCreditCard.pretty_print_from_file(nil)).to be_nil
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
context 'when path from wrong format txt file is provided' do
|
153
|
+
let(:file_path) do
|
154
|
+
File.join(File.dirname(__FILE__), '..', 'support', 'crazy_file.txt')
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'returns nil' do
|
158
|
+
expect(LuhnCreditCard.pretty_print_from_file(file_path)).to be_nil
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,4 @@
|
|
1
|
+
Mussum ipsum cacilds, vidis litro abertis. Consetis adipiscings elitis. Pra lá , depois divoltis porris, paradis. Paisis, filhis, espiritis santis. Mé faiz elementum girarzis, nisi eros vermeio, in elementis mé pra quem é amistosis quis leo. Manduma pindureta quium dia nois paga. Sapien in monti palavris qui num significa nadis i pareci latim. Interessantiss quisso pudia ce receita de bolis, mais bolis eu num gostis.
|
2
|
+
|
3
|
+
Suco de cevadiss, é um leite divinis, qui tem lupuliz, matis, aguis e fermentis. Interagi no mé, cursus quis, vehicula ac nisi. Aenean vel dui dui. Nullam leo erat, aliquet quis tempus a, posuere ut mi. Ut scelerisque neque et turpis posuere pulvinar pellentesque nibh ullamcorper. Pharetra in mattis molestie, volutpat elementum justo. Aenean ut ante turpis. Pellentesque laoreet mé vel lectus scelerisque interdum cursus velit auctor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ac mauris lectus, non scelerisque augue. Aenean justo massa.
|
4
|
+
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: luhn_credit_card
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Oswaldo Ferreira
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.1'
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 3.1.0
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '3.1'
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 3.1.0
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: simplecov
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0.9'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0.9'
|
61
|
+
description: It's a simple tool that provides helper methods for credit card number
|
62
|
+
validations using Luhn Algorithm.
|
63
|
+
email:
|
64
|
+
- oswluizf@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- ".gitignore"
|
70
|
+
- ".rspec"
|
71
|
+
- ".ruby-version"
|
72
|
+
- lib/luhn_credit_card.rb
|
73
|
+
- lib/luhn_credit_card/luhn_validator.rb
|
74
|
+
- lib/luhn_credit_card/number_cleaner.rb
|
75
|
+
- lib/luhn_credit_card/pretty_printer.rb
|
76
|
+
- lib/luhn_credit_card/text_file_printer.rb
|
77
|
+
- lib/luhn_credit_card/type_finder.rb
|
78
|
+
- luhn_credit_card.gemspec
|
79
|
+
- spec/luhn_credit_card/luhn_credit_card_spec.rb
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
- spec/support/crazy_file.txt
|
82
|
+
- spec/support/credit_cards.txt
|
83
|
+
homepage:
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.2.2
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: Credit card validations using Luhn Algorithm.
|
107
|
+
test_files: []
|