credit_card_support 2.2.0 → 2.5.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d28f5ada154ffad6494b10e654dd28ce5a70ef43
4
- data.tar.gz: a2b190748bda184e408bb857ac3c58738f485071
3
+ metadata.gz: 2c234bacc8120091ca1852b16ed40061c72fc238
4
+ data.tar.gz: dad00f6e83a6172da99df4a6b0dcf84d215d639a
5
5
  SHA512:
6
- metadata.gz: fea56635c7c333e07d03b5cc1b6a8f38f3d4d71cbbcb194ce06932a66bdf8315346b1765fcc9021d3ee0b1b922bfb8fe2e359f9ff17bcfe84ec666c264ec85a0
7
- data.tar.gz: 310fc7c7a36fcbd4d29a1452d901c7dc2b6fb5adb24cbb438bc7dcf0f1b2708ab909f9eabbeac9082cd63967dfc969c351efe5b85c7a240b4ac0304118a3dfff
6
+ metadata.gz: c738a7bee61bf2383af58a0d541d14f622d14a549ae87ee868b67e61a0c42d091b4dbe2a3ac5eb1e24b3e8d217ddcb74a00c08c5cdce0c03efb16c89654d5aaf
7
+ data.tar.gz: ebcc7cba76b432ec8cf11bd77de18048e959a4e768f08675095ea9e539112ca20ce5239c70fa1616af15848d9514eaabd7ac5e6c5cf6950ac3333105c91b0bf2
@@ -2,32 +2,36 @@ I18n.load_path << File.dirname(__FILE__) + '/../locale/en.yml'
2
2
 
3
3
  module ActiveModel
4
4
  module Validations
5
- class CreditCardNumberValidator < ActiveModel::EachValidator
6
5
 
6
+ class CreditCardValidator < ActiveModel::EachValidator
7
7
  def validate_each(record, attribute, value)
8
8
  @record = record
9
9
  @attribute = attribute
10
- @value = "#{value}"
10
+ @value = "#{value}".strip.gsub(/ /, '') # remove random whitespace
11
11
 
12
12
  @value.extend(CreditCardSupport::CreditCardNumber)
13
13
 
14
- validates_luhn &&
15
- validates_testcard &&
16
- validates_issuer_allowed
14
+ validate_internal
17
15
  end
18
16
 
19
- def validates_luhn
20
- if !@value.luhn?
21
- @record.errors.add(@attribute, t(:luhn_not_valid))
22
- false
23
- else
24
- true
25
- end
17
+ def t(code, *args)
18
+ I18n.t("credit_card_support.validators.number.messages.#{code}", *args)
19
+ end
20
+ end
21
+
22
+ class CreditCardSupportValidator < CreditCardValidator
23
+ def validate_internal
24
+ validates_testcard &&
25
+ validates_issuer_allowed
26
26
  end
27
27
 
28
28
  def validates_testcard
29
29
  if !options[:allow_testcards] && @value.testcard?
30
- @record.errors.add(@attribute, t(:testcard_not_supported))
30
+ @record.errors.add(
31
+ @attribute,
32
+ :testcard_not_supported,
33
+ options.merge(message: options[:message] || t(:testcard_not_supported))
34
+ )
31
35
  false
32
36
  else
33
37
  true
@@ -36,17 +40,31 @@ module ActiveModel
36
40
 
37
41
  def validates_issuer_allowed
38
42
  if options[:allow_issuers] && !options[:allow_issuers].include?(@value.issuer)
39
- @record.errors.add(@attribute, t(:issuer_not_supported, issuer: @value.issuer))
43
+ @record.errors.add(
44
+ @attribute,
45
+ :issuer_not_supported,
46
+ options.merge(message: options[:message] || t(:issuer_not_supported, issuer: @value.issuer)).merge(issuer: @value.issuer)
47
+ )
40
48
  false
41
49
  else
42
50
  true
43
51
  end
44
52
  end
53
+ end
45
54
 
46
- def t(code, *args)
47
- I18n.t("credit_card_support.validators.number.messages.#{code}", *args)
55
+ class CreditCardNumberValidator < CreditCardValidator
56
+ def validate_internal
57
+ if !@value.luhn?
58
+ @record.errors.add(
59
+ @attribute,
60
+ :luhn_not_valid,
61
+ options.merge(message: options[:message] || t(:luhn_not_valid))
62
+ )
63
+ false
64
+ else
65
+ true
66
+ end
48
67
  end
49
-
50
68
  end
51
69
  end
52
70
  end
@@ -1,8 +1,8 @@
1
1
  module CreditCardSupport
2
2
  module Version
3
3
  MAJOR = 2
4
- MINOR = 2
5
- PATCH = 0
4
+ MINOR = 5
5
+ PATCH = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
8
  end
@@ -23,27 +23,41 @@ class CreditCard
23
23
  end
24
24
 
25
25
  class CreditCardTest < CreditCard
26
- validates :number, presence: true,
27
- length: {
28
- minimum: 13,
29
- maximum: 19
30
- },
31
- credit_card_number: {
32
- allow_testcards: true,
33
- allow_issuers: [:visa, :master_card]
34
- }
26
+ validates :number,
27
+ credit_card_number: true,
28
+ credit_card_support: {
29
+ allow_testcards: true,
30
+ allow_issuers: [:visa, :master_card]
31
+ }
32
+ end
33
+
34
+ class CreditCardWithCustomMessage < CreditCard
35
+ validates :number,
36
+ credit_card_number: { message: 'Luhn fail!' },
37
+ credit_card_support: {
38
+ allow_testcards: true,
39
+ allow_issuers: [:visa, :master_card],
40
+ message: "Not supported!"
41
+ }
42
+ end
43
+
44
+ class CreditCardWithCustomLambdaMessage < CreditCard
45
+ validates :number,
46
+ credit_card_number: { message: ->(trans_string, context){ "Luhn fail! #{trans_string} #{context[:value]}" } },
47
+ credit_card_support: {
48
+ allow_testcards: false,
49
+ allow_issuers: [:visa, :master_card],
50
+ message: ->(trans_string, context){ "Not supported! #{trans_string} #{context[:value]}" }
51
+ }
35
52
  end
36
53
 
37
54
  class CreditCardProduction < CreditCard
38
- validates :number, presence: true,
39
- length: {
40
- minimum: 13,
41
- maximum: 19
42
- },
43
- credit_card_number: {
44
- allow_testcards: false,
45
- allow_issuers: [:visa, :master_card]
46
- }
55
+ validates :number,
56
+ credit_card_number: true,
57
+ credit_card_support: {
58
+ allow_testcards: false,
59
+ allow_issuers: [:visa, :master_card]
60
+ }
47
61
  end
48
62
 
49
63
 
@@ -59,16 +73,71 @@ describe ActiveModel::Validations::CreditCardNumberValidator do
59
73
  subject.number = nil
60
74
  subject.should_not be_valid
61
75
  end
76
+
62
77
  it "must be luhn" do
63
78
  subject.number = '4012888888881882'
64
79
  subject.should_not be_valid
80
+ subject.errors[:number].first.should == 'is not valid'
81
+ end
82
+
83
+ it "must reject unsupported cards" do
84
+ subject.number = '3528000000000007'
85
+ subject.should_not be_valid
86
+ subject.errors[:number].first.should == 'jcb not supported'
87
+ end
88
+
89
+ it "must support random whitespace around card numbers" do
90
+ subject.number = ' 4012 8888 8 8 8 8 1881 '
91
+ subject.should be_valid
92
+ end
93
+
94
+ context "with custom card support messages" do
95
+ subject { CreditCardWithCustomMessage.new(number: '3528000000000007') }
96
+ it "has a custom message" do
97
+ subject.valid?
98
+ subject.errors[:number].first.should == 'Not supported!'
99
+ end
65
100
  end
101
+
102
+ context "with custom luhn calculation failure messages" do
103
+ subject { CreditCardWithCustomMessage.new(number: '4111111111111112') }
104
+ it "has a custom message" do
105
+ subject.valid?
106
+ subject.errors[:number].first.should == 'Luhn fail!'
107
+ end
108
+ end
109
+
110
+ context "with custom card support messages in lambda form" do
111
+ subject { CreditCardWithCustomLambdaMessage.new(number: '3528000000000007') }
112
+ it "has a custom message" do
113
+ subject.valid?
114
+ subject.errors[:number].first.should == 'Not supported! activemodel.errors.models.credit_card_with_custom_lambda_message.attributes.number.testcard_not_supported 3528000000000007'
115
+ end
116
+ end
117
+
118
+ context "with custom test card support messages in lambda form" do
119
+ subject { CreditCardWithCustomLambdaMessage.new(number: '4111111111111111') }
120
+ it "has a custom message" do
121
+ subject.valid?
122
+ subject.errors[:number].first.should == 'Not supported! activemodel.errors.models.credit_card_with_custom_lambda_message.attributes.number.testcard_not_supported 4111111111111111'
123
+ end
124
+ end
125
+
126
+ context "with custom luhn calculation failure messages in lambda form" do
127
+ subject { CreditCardWithCustomLambdaMessage.new(number: '4111111111111112') }
128
+ it "has a custom message" do
129
+ subject.valid?
130
+ subject.errors[:number].first.should == 'Luhn fail! activemodel.errors.models.credit_card_with_custom_lambda_message.attributes.number.luhn_not_valid 4111111111111112'
131
+ end
132
+ end
133
+
66
134
  context "production" do
67
135
  subject { CreditCardProduction.new(number: '4485071359608368') }
68
136
  context "testnumber" do
69
137
  it "is invalid" do
70
138
  subject.number = '4012888888881881'
71
139
  subject.should be_invalid
140
+ subject.errors[:number].first.should == 'testcards not supported'
72
141
  end
73
142
  end
74
143
  context "valid number" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: credit_card_support
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Margus Pärt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-30 00:00:00.000000000 Z
11
+ date: 2014-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -61,8 +61,6 @@ extra_rdoc_files: []
61
61
  files:
62
62
  - Gemfile
63
63
  - README.md
64
- - LICENSE.txt
65
- - HISTORY.txt
66
64
  - lib/credit_card_support/credit_card_number.rb
67
65
  - lib/credit_card_support/locale/en.yml
68
66
  - lib/credit_card_support/luhn_number.rb
@@ -74,7 +72,8 @@ files:
74
72
  - spec/lib/credit_card_support/validators/credit_card_number_validator_spec.rb
75
73
  - spec/spec_helper.rb
76
74
  homepage: https://github.com/tione/credit_card_support
77
- licenses: []
75
+ licenses:
76
+ - MIT
78
77
  metadata: {}
79
78
  post_install_message:
80
79
  rdoc_options: []
data/HISTORY.txt DELETED
@@ -1,18 +0,0 @@
1
- 2.0.0
2
-
3
- Rewrite.
4
-
5
-
6
- 1.0.2
7
-
8
- Readme improvement.
9
-
10
-
11
- 1.0.1
12
-
13
- Redid validator loading - its automatically loaded if ActiveModel is loaded.
14
-
15
-
16
- 1.0.0
17
-
18
- Initial.
data/LICENSE.txt DELETED
@@ -1,20 +0,0 @@
1
- Copyright (c) 2012 mxrguspxrt
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.