credit_card_validator 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.rdoc +8 -8
- data/lib/credit_card_validator/validator.rb +16 -12
- metadata +63 -90
- data/History.txt +0 -4
- data/Rakefile +0 -27
- data/test/test_credit_card_validator.rb +0 -89
- data/test/test_helper.rb +0 -3
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5bf7397b99348fb735a15ed84e38bb175959eccb
|
4
|
+
data.tar.gz: 09f090e653b97f069bf9cc0f0a6e6e294b8d46f3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2fb322b10b5367dae3f65e13c3d81fb7d7b4a08699558c6a5206166d22a0afd6600067b09779cb758db53564683ac757bea388db91d10d6746a8b2754ea52db1
|
7
|
+
data.tar.gz: bd883614276a3899ade63cbb0fd9e5443cc440ebd2018b1d10add3d284ca1bef33c0bb591eb7b65fd666edd3e37c54a30a190c34277da9825bcbd3fa0c63535f
|
data/README.rdoc
CHANGED
@@ -11,16 +11,16 @@ A gem that provides credit card validation. It is basically a ruby port of the j
|
|
11
11
|
Usage:
|
12
12
|
CreditCardValidator::Validator.valid?('1111 2222 3333 4444')
|
13
13
|
|
14
|
-
# allow test numbers to be valid (for development)
|
14
|
+
# allow test numbers to be valid (for development)
|
15
15
|
CreditCardValidator::Validator.options[:test_numbers_are_valid] = true
|
16
16
|
CreditCardValidator::Validator.valid?('1111 2222 3333 4444')
|
17
17
|
|
18
18
|
# limit the card types you allow
|
19
|
-
CreditCardValidator::Validator.options[:allowed_card_types] = [:visa, :
|
19
|
+
CreditCardValidator::Validator.options[:allowed_card_types] = [:visa, :master_card]
|
20
20
|
CreditCardValidator::Validator.valid?('1111 2222 3333 4444')
|
21
21
|
|
22
22
|
Supported card types:
|
23
|
-
:amex, :discover, :diners_club, :master_card, :visa, :maestro
|
23
|
+
:amex, :discover, :diners_club, :master_card, :visa, :maestro, :jcb
|
24
24
|
|
25
25
|
Whitespace is stripped from the number automatically.
|
26
26
|
|
@@ -35,7 +35,7 @@ You can also use the validator to learn about the type of the card:
|
|
35
35
|
|
36
36
|
# gives the type back as a string (visa, master_card, etc)
|
37
37
|
CreditCardValidator::Validator.card_type(number)
|
38
|
-
|
38
|
+
|
39
39
|
CreditCardValidator::Validator.is_visa?(number)
|
40
40
|
CreditCardValidator::Validator.is_master_card?(number)
|
41
41
|
# etc. - works for all of the supported card types
|
@@ -68,10 +68,10 @@ Verbose mode will emit more detailed information:
|
|
68
68
|
$ credit_card_validator -v 5105105105105100
|
69
69
|
credit_card_validator Parameters:
|
70
70
|
verbose : true
|
71
|
-
help :
|
71
|
+
help :
|
72
72
|
allowed-card-types : amex,diners_club,discover,maestro,master_card,visa
|
73
|
-
test-numbers-are-valid :
|
74
|
-
|
73
|
+
test-numbers-are-valid :
|
74
|
+
|
75
75
|
card_number is_allowed_card_type? luhn_valid? card_type is_test_number? valid?
|
76
76
|
5105105105105100 true true master_card true false
|
77
77
|
|
@@ -101,7 +101,7 @@ Even from standard input:
|
|
101
101
|
|
102
102
|
== INSTALL:
|
103
103
|
|
104
|
-
* gem install credit_card_validator
|
104
|
+
* gem install credit_card_validator
|
105
105
|
|
106
106
|
== LICENSE:
|
107
107
|
|
@@ -5,7 +5,8 @@ module CreditCardValidator
|
|
5
5
|
:maestro => /(^6759[0-9]{2}([0-9]{10})$)|(^6759[0-9]{2}([0-9]{12})$)|(^6759[0-9]{2}([0-9]{13})$)/,
|
6
6
|
:diners_club => /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/,
|
7
7
|
:amex => /^3[47][0-9]{13}$/,
|
8
|
-
:discover => /^6(?:011|5[0-9]{2})[0-9]{12}
|
8
|
+
:discover => /^6(?:011|5[0-9]{2})[0-9]{12}$/,
|
9
|
+
:jcb => /^(?:2131|1800|35\d{3})\d{11}$/
|
9
10
|
}
|
10
11
|
|
11
12
|
TEST_NUMBERS = {
|
@@ -21,9 +22,12 @@ module CreditCardValidator
|
|
21
22
|
4111111111111111 4012888888881881 4222222222222
|
22
23
|
4005519200000004 4009348888881881 4012000033330026
|
23
24
|
4012000077777777 4217651111111119 4500600000000061
|
24
|
-
4000111111111115 }
|
25
|
+
4000111111111115 },
|
26
|
+
:jcb => %w{
|
27
|
+
3530111333300000 3566002020360505
|
28
|
+
}
|
25
29
|
}.values.flatten
|
26
|
-
|
30
|
+
|
27
31
|
class Validator
|
28
32
|
class << self
|
29
33
|
def valid?(number)
|
@@ -32,18 +36,18 @@ module CreditCardValidator
|
|
32
36
|
card_type(number) and
|
33
37
|
(options[:test_numbers_are_valid] ? true : !is_test_number(number))
|
34
38
|
end
|
35
|
-
|
39
|
+
|
36
40
|
def options
|
37
41
|
@@options ||= {}
|
38
42
|
end
|
39
|
-
|
43
|
+
|
40
44
|
def card_type(number)
|
41
45
|
CARD_TYPES.keys.each do |t|
|
42
46
|
return t.to_s if card_is(t, number)
|
43
47
|
end
|
44
48
|
nil
|
45
49
|
end
|
46
|
-
|
50
|
+
|
47
51
|
def is_allowed_card_type?(number)
|
48
52
|
card_type = card_type(number)
|
49
53
|
return nil if card_type.nil?
|
@@ -53,7 +57,7 @@ module CreditCardValidator
|
|
53
57
|
!card_type.nil?
|
54
58
|
end
|
55
59
|
end
|
56
|
-
|
60
|
+
|
57
61
|
def is_test_number(number)
|
58
62
|
TEST_NUMBERS.include?(strip(number))
|
59
63
|
end
|
@@ -65,10 +69,10 @@ module CreditCardValidator
|
|
65
69
|
accum[1] += 1
|
66
70
|
accum
|
67
71
|
end
|
68
|
-
|
72
|
+
|
69
73
|
(total[0] % 10 == 0)
|
70
74
|
end
|
71
|
-
|
75
|
+
|
72
76
|
CARD_TYPES.keys.each do |card_type|
|
73
77
|
self.class_eval do
|
74
78
|
define_method "is_#{card_type.to_s}?" do |number|
|
@@ -76,19 +80,19 @@ module CreditCardValidator
|
|
76
80
|
end
|
77
81
|
end
|
78
82
|
end
|
79
|
-
|
83
|
+
|
80
84
|
protected
|
81
85
|
def strip(number)
|
82
86
|
number.gsub(/\s/,'')
|
83
87
|
end
|
84
|
-
|
88
|
+
|
85
89
|
def rotate(number)
|
86
90
|
if number > 9
|
87
91
|
number = number % 10 + 1
|
88
92
|
end
|
89
93
|
number
|
90
94
|
end
|
91
|
-
|
95
|
+
|
92
96
|
def card_is(type, number)
|
93
97
|
type = type.to_sym
|
94
98
|
(CARD_TYPES[type] and strip(number) =~ CARD_TYPES[type])
|
metadata
CHANGED
@@ -1,131 +1,104 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: credit_card_validator
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 1
|
8
|
-
- 1
|
9
|
-
- 0
|
10
|
-
version: 1.1.0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.0
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
6
|
+
authors:
|
13
7
|
- Tobias Crawley
|
14
8
|
- Bruce Hauman
|
15
9
|
- Phil McClure
|
16
10
|
- Gabriel Reis
|
17
11
|
- Eric Young
|
18
12
|
- Kyle Burton <kyle.burton@gmail.com>
|
13
|
+
- Keitaroh Kobayashi
|
14
|
+
- Alan Ho
|
19
15
|
autorequire:
|
20
16
|
bindir: bin
|
21
17
|
cert_chain: []
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
- !ruby/object:Gem::Dependency
|
18
|
+
date: 2013-10-26 00:00:00.000000000 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
26
21
|
name: newgem
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
hash: 25
|
34
|
-
segments:
|
35
|
-
- 1
|
36
|
-
- 2
|
37
|
-
- 3
|
22
|
+
requirement: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
38
26
|
version: 1.2.3
|
39
27
|
type: :development
|
40
|
-
version_requirements: *id001
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: hoe
|
43
28
|
prerelease: false
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.2.3
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: hoe
|
36
|
+
requirement: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
54
40
|
version: 1.8.0
|
55
41
|
type: :development
|
56
|
-
|
57
|
-
|
42
|
+
prerelease: false
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.8.0
|
48
|
+
- !ruby/object:Gem::Dependency
|
58
49
|
name: base_app
|
50
|
+
requirement: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.0.5
|
55
|
+
type: :runtime
|
59
56
|
prerelease: false
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
- !ruby/object:Gem::Version
|
65
|
-
hash: 29
|
66
|
-
segments:
|
67
|
-
- 1
|
68
|
-
- 0
|
69
|
-
- 5
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
70
61
|
version: 1.0.5
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
executables:
|
62
|
+
description: A gem that provides credit card validation. It is basically a ruby port
|
63
|
+
of the javascript credit card validator by Thomas Fuchs (madrobby) (http://github.com/madrobby/creditcard_js).
|
64
|
+
email:
|
65
|
+
- toby@tcrawley.org
|
66
|
+
executables:
|
77
67
|
- credit_card_validator
|
78
68
|
extensions: []
|
79
|
-
|
80
|
-
extra_rdoc_files:
|
81
|
-
- History.txt
|
69
|
+
extra_rdoc_files:
|
82
70
|
- Manifest.txt
|
83
71
|
- README.rdoc
|
84
|
-
files:
|
85
|
-
- History.txt
|
72
|
+
files:
|
86
73
|
- Manifest.txt
|
87
74
|
- README.rdoc
|
88
|
-
- Rakefile
|
89
75
|
- lib/credit_card_validator.rb
|
90
76
|
- lib/credit_card_validator/validator.rb
|
91
|
-
- test/test_credit_card_validator.rb
|
92
|
-
- test/test_helper.rb
|
93
77
|
- bin/credit_card_validator
|
94
78
|
homepage: http://github.com/tobias/credit_card_validator
|
95
79
|
licenses: []
|
96
|
-
|
80
|
+
metadata: {}
|
97
81
|
post_install_message:
|
98
|
-
rdoc_options:
|
82
|
+
rdoc_options:
|
99
83
|
- --main
|
100
84
|
- README.rdoc
|
101
|
-
require_paths:
|
85
|
+
require_paths:
|
102
86
|
- bin
|
103
87
|
- lib
|
104
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
none: false
|
115
|
-
requirements:
|
116
|
-
- - ">="
|
117
|
-
- !ruby/object:Gem::Version
|
118
|
-
hash: 3
|
119
|
-
segments:
|
120
|
-
- 0
|
121
|
-
version: "0"
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
122
98
|
requirements: []
|
123
|
-
|
124
99
|
rubyforge_project: credit_card_validator
|
125
|
-
rubygems_version:
|
100
|
+
rubygems_version: 2.0.8
|
126
101
|
signing_key:
|
127
102
|
specification_version: 2
|
128
103
|
summary: A gem that provides credit card validation
|
129
|
-
test_files:
|
130
|
-
- test/test_credit_card_validator.rb
|
131
|
-
- test/test_helper.rb
|
104
|
+
test_files: []
|
data/History.txt
DELETED
data/Rakefile
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
%w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
|
2
|
-
require File.dirname(__FILE__) + '/lib/credit_card_validator'
|
3
|
-
|
4
|
-
# Generate all the Rake tasks
|
5
|
-
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
6
|
-
$hoe = Hoe.new('credit_card_validator', CreditCardValidator::VERSION) do |p|
|
7
|
-
p.developer('Tobias Crawley', 'tcrawley@gmail.com')
|
8
|
-
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
9
|
-
p.rubyforge_name = p.name # TODO this is default value
|
10
|
-
# p.extra_deps = [
|
11
|
-
# ['activesupport','>= 2.0.2'],
|
12
|
-
# ]
|
13
|
-
p.extra_dev_deps = [
|
14
|
-
['newgem', ">= #{::Newgem::VERSION}"]
|
15
|
-
]
|
16
|
-
|
17
|
-
p.clean_globs |= %w[**/.DS_Store tmp *.log]
|
18
|
-
path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
|
19
|
-
p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
|
20
|
-
p.rsync_args = '-av --delete --ignore-errors'
|
21
|
-
end
|
22
|
-
|
23
|
-
require 'newgem/tasks' # load /tasks/*.rake
|
24
|
-
Dir['tasks/**/*.rake'].each { |t| load t }
|
25
|
-
|
26
|
-
# TODO - want other tests/tasks run by default? Add them to the list
|
27
|
-
# task :default => [:spec, :features]
|
@@ -1,89 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
-
|
3
|
-
class TestCreditCardValidator < Test::Unit::TestCase
|
4
|
-
|
5
|
-
def setup
|
6
|
-
@v = CreditCardValidator::Validator
|
7
|
-
@v.options.clear
|
8
|
-
end
|
9
|
-
|
10
|
-
|
11
|
-
def test_recognize_card_type
|
12
|
-
assert_equal 'visa', @v.card_type('4111111111111111')
|
13
|
-
assert_equal 'master_card', @v.card_type('5555555555554444')
|
14
|
-
assert_equal 'diners_club', @v.card_type('30569309025904')
|
15
|
-
assert_equal 'amex', @v.card_type('371449635398431')
|
16
|
-
assert_equal 'discover', @v.card_type('6011000990139424')
|
17
|
-
assert_equal 'maestro', @v.card_type('6759671431256542')
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_detect_specific_types
|
21
|
-
assert @v.is_visa?('4111111111111111')
|
22
|
-
assert !@v.is_visa?('5555555555554444')
|
23
|
-
assert !@v.is_visa?('30569309025904')
|
24
|
-
assert !@v.is_visa?('371449635398431')
|
25
|
-
assert !@v.is_visa?('6011000990139424')
|
26
|
-
assert @v.is_master_card?('5555555555554444')
|
27
|
-
assert @v.is_diners_club?('30569309025904')
|
28
|
-
assert @v.is_amex?('371449635398431')
|
29
|
-
assert @v.is_discover?('6011000990139424')
|
30
|
-
assert @v.is_maestro?('6759671431256542')
|
31
|
-
assert !@v.is_maestro?('5555555555554444')
|
32
|
-
assert !@v.is_maestro?('30569309025904')
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_luhn_verification
|
36
|
-
assert @v.verify_luhn('49927398716')
|
37
|
-
assert @v.verify_luhn('049927398716')
|
38
|
-
assert @v.verify_luhn('0049927398716')
|
39
|
-
assert !@v.verify_luhn('49927398715')
|
40
|
-
assert !@v.verify_luhn('49927398717')
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_ignore_whitespace
|
44
|
-
assert_equal '4111111111111111', @v.send('strip', '4111 1111 1111 1111 ')
|
45
|
-
|
46
|
-
assert_equal 'visa', @v.card_type('4111 1111 1111 1111 ')
|
47
|
-
assert_equal 'visa', @v.card_type(' 4111 1111 1111 1111 ')
|
48
|
-
assert_equal 'visa', @v.card_type("\n4111 1111\t 1111 1111 ")
|
49
|
-
assert @v.verify_luhn(' 004 992739 87 16')
|
50
|
-
assert @v.is_test_number('601 11111111111 17')
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_should_recognize_test_numbers
|
54
|
-
%w(
|
55
|
-
378282246310005 371449635398431 378734493671000
|
56
|
-
30569309025904 38520000023237 6011111111111117
|
57
|
-
6011000990139424 5555555555554444 5105105105105100
|
58
|
-
4111111111111111 4012888888881881 4222222222222
|
59
|
-
).each do |n|
|
60
|
-
assert @v.is_test_number(n)
|
61
|
-
end
|
62
|
-
|
63
|
-
assert !@v.is_test_number('1234')
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_test_number_validity_cases
|
67
|
-
assert !@v.valid?('378282246310005')
|
68
|
-
@v.options[:test_numbers_are_valid] = true
|
69
|
-
assert @v.valid?('378282246310005')
|
70
|
-
end
|
71
|
-
|
72
|
-
def test_is_allowed_card_type
|
73
|
-
assert @v.is_allowed_card_type?('378282246310005')
|
74
|
-
@v.options[:allowed_card_types] = [:visa]
|
75
|
-
assert @v.is_allowed_card_type?('4012888888881881')
|
76
|
-
assert !@v.is_allowed_card_type?('378282246310005')
|
77
|
-
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_card_type_allowance
|
81
|
-
@v.options[:test_numbers_are_valid] = true
|
82
|
-
assert @v.valid?('378282246310005')
|
83
|
-
@v.options[:allowed_card_types] = [:visa]
|
84
|
-
assert @v.valid?('4012888888881881')
|
85
|
-
assert !@v.valid?('378282246310005')
|
86
|
-
|
87
|
-
end
|
88
|
-
|
89
|
-
end
|
data/test/test_helper.rb
DELETED