credit_card_validations 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -34,11 +34,12 @@ The following issuing institutes are accepted:
34
34
  Dinner Club US | :diners_us
35
35
  Discover | :discover
36
36
  JCB | :jcb
37
+ Laser | :laser
37
38
  Maestro | :maestro
38
39
  MasterCard | :mastercard
39
40
  Solo | :solo
40
41
  Visa | :visa
41
- Laser | :laser
42
+
42
43
 
43
44
  Examples using string monkey patch
44
45
 
@@ -21,6 +21,7 @@ Gem::Specification.new do |gem|
21
21
  gem.add_dependency "activesupport", ">= 3.0", "< 4.1"
22
22
 
23
23
  gem.add_development_dependency "test-unit"
24
+ gem.add_development_dependency "mocha"
24
25
  gem.add_development_dependency 'rake'
25
26
 
26
27
  end
@@ -50,7 +50,7 @@ module CreditCardValidations
50
50
  ]
51
51
 
52
52
  UNIONPAY = [
53
- {length: [16, 17, 18, 19], prefixes: ['620', '621', '623', '625', '626']}
53
+ {length: [16, 17, 18, 19], prefixes: ['620', '621', '623', '625', '626'], :skip_validation => true}
54
54
  ]
55
55
 
56
56
 
@@ -15,18 +15,17 @@ module CreditCardValidations
15
15
  end
16
16
 
17
17
  #brand name
18
- def brand
19
- valid_number?
18
+ def brand(*brands)
19
+ valid_number?(*brands)
20
20
  end
21
21
 
22
22
  def valid_number?(*brands)
23
- return nil unless valid_luhn?
24
23
  number_length = number.length
25
24
  brand_rules = brands.blank? ? self.rules : self.rules.slice(*brands.map{ |el| el.downcase })
26
25
  unless brand_rules.blank?
27
26
  brand_rules.each do |brand_name, rules|
28
27
  rules.each do |rule|
29
- return brand_name if (rule[:length].include?(number_length) and number.match(rule[:regexp]))
28
+ return brand_name if ( (rule[:skip_validation] || valid_luhn?) and rule[:length].include?(number_length) and number.match(rule[:regexp]))
30
29
  end
31
30
  end
32
31
  end
@@ -35,7 +34,7 @@ module CreditCardValidations
35
34
 
36
35
  #check if luhn valid
37
36
  def valid_luhn?
38
- Luhn.valid?(number)
37
+ @valid_luhn ||= Luhn.valid?(number)
39
38
  end
40
39
 
41
40
  class << self
@@ -46,11 +45,11 @@ module CreditCardValidations
46
45
  end
47
46
 
48
47
  #create rule for detecting brand
49
- def add_rule(brand, length, prefixes)
48
+ def add_rule(brand, length, prefixes, skip_validation = false)
50
49
  prefixes = Array.wrap(prefixes)
51
50
  length = Array.wrap(length)
52
51
  rules[brand] = [] if rules[brand].blank?
53
- rules[brand] << {length: length, regexp: compile_regexp(prefixes), prefixes: prefixes}
52
+ rules[brand] << {length: length, regexp: compile_regexp(prefixes), prefixes: prefixes, :skip_validation => skip_validation}
54
53
  #create methods like visa? mastercard? etc
55
54
  class_eval <<-BOOLEAN_RULE, __FILE__, __LINE__
56
55
  def #{brand}?
@@ -62,11 +61,11 @@ module CreditCardValidations
62
61
 
63
62
  CardRules.constants.each do |const_name|
64
63
  CardRules.const_get(const_name).each do |const_value|
65
- self.add_rule(const_name.to_s.downcase.to_sym , const_value[:length], const_value[:prefixes])
64
+ self.add_rule(const_name.to_s.downcase.to_sym , const_value[:length], const_value[:prefixes], const_value[:skip_validation])
66
65
  end
67
66
 
68
67
  end
69
68
  end
70
69
 
71
70
 
72
- end
71
+ end
@@ -1,3 +1,3 @@
1
1
  module CreditCardValidations
2
- VERSION = "1.1.2"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require 'test/unit'
2
+ require 'mocha/setup'
2
3
  lib = File.expand_path("#{File.dirname(__FILE__)}/../lib")
3
4
  unit_tests = File.expand_path("#{File.dirname(__FILE__)}/../test")
4
5
  $:.unshift(lib)
@@ -41,6 +42,16 @@ class CreditCardValidationsTest < Test::Unit::TestCase
41
42
  assert_nil detector('1111111111111111').brand
42
43
  end
43
44
 
45
+ def test_card_brand_detection_with_restriction
46
+ @test_numbers.slice(:visa, :mastercard).each do |key, value|
47
+ assert_equal key, detector(value).brand(:visa, :mastercard)
48
+ end
49
+
50
+ @test_numbers.except(:visa, :mastercard).each do |key, value|
51
+ assert_nil detector(value).brand(:visa, :mastercard)
52
+ end
53
+ end
54
+
44
55
  def test_card_valid_method
45
56
  @test_numbers.each do |key, value|
46
57
  assert detector(value).valid?(key)
@@ -77,20 +88,40 @@ class CreditCardValidationsTest < Test::Unit::TestCase
77
88
  cc = CreditCardModel.new
78
89
  cc.number = @test_numbers[:maestro]
79
90
  assert cc.valid?
80
-
91
+
81
92
  cc = CreditCardModel.new
82
93
  cc.number = @test_numbers[:mastercard]
83
94
  assert !cc.valid?
84
95
  assert cc.errors[:number].include?(cc.errors.generate_message(:number, :invalid))
85
96
  end
86
-
97
+
87
98
  def test_string_extension
88
99
  require 'credit_card_validations/string'
89
- assert_equal @test_numbers[:mastercard].credit_card_brand, :mastercard
90
- assert @test_numbers[:mastercard].valid_credit_card_brand?(:mastercard)
91
- assert !@test_numbers[:mastercard].valid_credit_card_brand?(:visa, :amex)
92
- end
93
-
100
+ assert_equal @test_numbers[:mastercard].credit_card_brand, :mastercard
101
+ assert @test_numbers[:mastercard].valid_credit_card_brand?(:mastercard)
102
+ assert !@test_numbers[:mastercard].valid_credit_card_brand?(:visa, :amex)
103
+ end
104
+
105
+
106
+ def test_skip_validation
107
+ d = detector(@test_numbers[:unionpay])
108
+ d.expects(:valid_luhn?).never
109
+ assert d.valid?(:unionpay)
110
+
111
+ d = detector(@test_numbers[:unionpay])
112
+ d.expects(:valid_luhn?).once
113
+ assert !d.valid?(:visa)
114
+
115
+ d = detector(@test_numbers[:unionpay])
116
+ d.expects(:valid_luhn?).never
117
+ assert d.valid?(:unionpay, :visa)
118
+
119
+ d = detector(@test_numbers[:unionpay])
120
+ d.expects(:valid_luhn?).once
121
+ assert d.valid?(:visa, :unionpay)
122
+ end
123
+
124
+
94
125
  protected
95
126
 
96
127
  def detector(number)
@@ -98,4 +129,4 @@ class CreditCardValidationsTest < Test::Unit::TestCase
98
129
  end
99
130
 
100
131
 
101
- end
132
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: credit_card_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-14 00:00:00.000000000 Z
12
+ date: 2013-05-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
16
- requirement: &70126777364960 !ruby/object:Gem::Requirement
16
+ requirement: &70292463116540 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -24,10 +24,10 @@ dependencies:
24
24
  version: '4.1'
25
25
  type: :runtime
26
26
  prerelease: false
27
- version_requirements: *70126777364960
27
+ version_requirements: *70292463116540
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: activesupport
30
- requirement: &70126777364180 !ruby/object:Gem::Requirement
30
+ requirement: &70292463129120 !ruby/object:Gem::Requirement
31
31
  none: false
32
32
  requirements:
33
33
  - - ! '>='
@@ -38,10 +38,10 @@ dependencies:
38
38
  version: '4.1'
39
39
  type: :runtime
40
40
  prerelease: false
41
- version_requirements: *70126777364180
41
+ version_requirements: *70292463129120
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: test-unit
44
- requirement: &70126777363520 !ruby/object:Gem::Requirement
44
+ requirement: &70292467198680 !ruby/object:Gem::Requirement
45
45
  none: false
46
46
  requirements:
47
47
  - - ! '>='
@@ -49,10 +49,21 @@ dependencies:
49
49
  version: '0'
50
50
  type: :development
51
51
  prerelease: false
52
- version_requirements: *70126777363520
52
+ version_requirements: *70292467198680
53
+ - !ruby/object:Gem::Dependency
54
+ name: mocha
55
+ requirement: &70292467197900 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: *70292467197900
53
64
  - !ruby/object:Gem::Dependency
54
65
  name: rake
55
- requirement: &70126777363040 !ruby/object:Gem::Requirement
66
+ requirement: &70292467197180 !ruby/object:Gem::Requirement
56
67
  none: false
57
68
  requirements:
58
69
  - - ! '>='
@@ -60,7 +71,7 @@ dependencies:
60
71
  version: '0'
61
72
  type: :development
62
73
  prerelease: false
63
- version_requirements: *70126777363040
74
+ version_requirements: *70292467197180
64
75
  description: A ruby gem for validating credit card numbers (a port of ZF2 Zend\Validator\CreditCard)
65
76
  email:
66
77
  - fedoronchuk@gmail.com