credit_card_support 1.0.0 → 1.0.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.
- data/lib/credit_card_support/credit_card_validator.rb +31 -22
- data/lib/credit_card_support/instrument.rb +2 -2
- data/lib/credit_card_support/locale/en.yml +1 -0
- data/lib/credit_card_support/version.rb +1 -1
- data/lib/credit_card_support.rb +1 -0
- data/spec/lib/credit_card_support/credit_card_validator_spec.rb +9 -2
- data/spec/lib/credit_card_support/instrument_spec.rb +3 -3
- metadata +2 -2
@@ -1,4 +1,3 @@
|
|
1
|
-
raise "ActiveModel not included" unless Object.const_defined?(:ActiveModel)
|
2
1
|
I18n.load_path << File.dirname(__FILE__) + '/locale/en.yml'
|
3
2
|
|
4
3
|
class CreditCardValidator < ActiveModel::EachValidator
|
@@ -9,16 +8,20 @@ class CreditCardValidator < ActiveModel::EachValidator
|
|
9
8
|
opts = options.dup
|
10
9
|
opts[:number] ||= attribute
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
@number_name = opts[:number] || :number
|
12
|
+
@expiry_year_name = opts[:expiry_year] || :expiry_year
|
13
|
+
@expiry_month_name = opts[:expiry_month] || :expiry_month
|
14
|
+
|
15
|
+
@number = record.send(@number_name)
|
16
|
+
@expiry_year = record.send(@expiry_year_name)
|
17
|
+
@expiry_month = record.send(@expiry_month_name)
|
15
18
|
@allowed_issuers = opts[:allowed_issuers]
|
16
19
|
@allow_testcards = opts[:allow_testcards]
|
17
20
|
|
18
|
-
@
|
19
|
-
@instrument = CreditCardSupport::Instrument.new(number: number, expiry_year: expiry_year, expiry_month: expiry_month)
|
21
|
+
@instrument = CreditCardSupport::Instrument.new(number: @number, expiry_year: @expiry_year, expiry_month: @expiry_month)
|
20
22
|
|
21
23
|
validate_number
|
24
|
+
validate_expiration
|
22
25
|
end
|
23
26
|
|
24
27
|
|
@@ -33,41 +36,47 @@ class CreditCardValidator < ActiveModel::EachValidator
|
|
33
36
|
end
|
34
37
|
|
35
38
|
def validate_number
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
+
validate_number_luhn
|
40
|
+
validate_number_issuer
|
41
|
+
validate_number_testcard
|
42
|
+
end
|
43
|
+
|
44
|
+
def validate_expiration
|
39
45
|
validate_expiry_year
|
40
46
|
validate_expiry_month
|
41
|
-
validate_expiration_date rescue
|
47
|
+
validate_expiration_date rescue nil
|
42
48
|
end
|
43
49
|
|
44
|
-
def
|
45
|
-
errors.add(@
|
50
|
+
def validate_number_luhn
|
51
|
+
errors.add(@number_name, t(:luhn_not_valid)) unless @instrument.has_valid_luhn?
|
46
52
|
end
|
47
53
|
|
48
|
-
def
|
49
|
-
errors.add(@
|
54
|
+
def validate_number_issuer
|
55
|
+
errors.add(@number_name, t(:issuer_not_known)) unless @instrument.issuer
|
50
56
|
if @allowed_issuers and @instrument.issuer and !@allowed_issuers.map(&:to_sym).include?(@instrument.issuer)
|
51
|
-
errors.add(@
|
57
|
+
errors.add(@number_name, t(:issuer_not_supported, issuer: @instrument.issuer.to_s.upcase))
|
52
58
|
end
|
53
59
|
end
|
54
60
|
|
55
|
-
def
|
56
|
-
errors.add(@
|
61
|
+
def validate_number_testcard
|
62
|
+
errors.add(@number_name, t(:testcard_not_supported)) if !@allow_testcards && @instrument.is_testcard?
|
57
63
|
end
|
58
64
|
|
59
65
|
def validate_expiry_year
|
60
|
-
errors.add(@
|
66
|
+
errors.add(@expiry_year_name, t(:cant_be_blank)) unless @instrument.expiry_year
|
61
67
|
end
|
62
68
|
|
63
69
|
def validate_expiry_month
|
64
|
-
errors.add(@
|
70
|
+
errors.add(@expiry_month_name, t(:cant_be_blank)) unless @instrument.expiry_month
|
65
71
|
end
|
66
72
|
|
67
73
|
def validate_expiration_date
|
68
|
-
if @instrument.
|
69
|
-
errors.add(@
|
70
|
-
errors.add(@
|
74
|
+
if @instrument.is_expired?
|
75
|
+
errors.add(@expiry_year_name, t(:cant_be_expired))
|
76
|
+
errors.add(@expiry_month_name, t(:cant_be_expired))
|
77
|
+
end
|
78
|
+
if @instrument.expiration_date > Date.new(Date.today.year+10)
|
79
|
+
errors.add(@expiry_year_name, t(:cant_be_so_far_in_the_future))
|
71
80
|
end
|
72
81
|
end
|
73
82
|
|
@@ -143,10 +143,10 @@ module CreditCardSupport
|
|
143
143
|
end
|
144
144
|
|
145
145
|
def expiration_date
|
146
|
-
Date.new(expiry_year, expiry_month, -1)
|
146
|
+
Date.new(expiry_year, expiry_month, -1) rescue nil
|
147
147
|
end
|
148
148
|
|
149
|
-
def
|
149
|
+
def is_expired?
|
150
150
|
expiration_date < Date.today
|
151
151
|
end
|
152
152
|
|
data/lib/credit_card_support.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
require 'spec_helper'
|
2
1
|
require 'active_model'
|
3
|
-
require '
|
2
|
+
require 'spec_helper'
|
4
3
|
|
5
4
|
|
6
5
|
class CreditCard
|
@@ -87,6 +86,12 @@ describe CreditCardValidator do
|
|
87
86
|
subject.should be_valid
|
88
87
|
end
|
89
88
|
end
|
89
|
+
context "in the future too much" do
|
90
|
+
it "is valid" do
|
91
|
+
subject.expiry_year = today.year + 11
|
92
|
+
subject.should be_invalid
|
93
|
+
end
|
94
|
+
end
|
90
95
|
context "in the past" do
|
91
96
|
it "is invalid" do
|
92
97
|
subject.expiry_year = today.year - 1
|
@@ -103,6 +108,8 @@ describe CreditCardValidator do
|
|
103
108
|
context "in the future" do
|
104
109
|
it "is valid" do
|
105
110
|
subject.expiry_month = today.month + 1
|
111
|
+
subject.valid?
|
112
|
+
puts subject.errors.inspect
|
106
113
|
subject.should be_valid
|
107
114
|
end
|
108
115
|
end
|
@@ -76,17 +76,17 @@ describe CreditCardSupport::Instrument do
|
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
|
-
describe "#
|
79
|
+
describe "#is_expired?" do
|
80
80
|
context "not expired" do
|
81
81
|
it "returns false" do
|
82
|
-
subject.
|
82
|
+
subject.is_expired?.should be_false
|
83
83
|
end
|
84
84
|
end
|
85
85
|
context "expired" do
|
86
86
|
it "returns true" do
|
87
87
|
subject.expiry_year = today.year
|
88
88
|
subject.expiry_month = today.month-1
|
89
|
-
subject.
|
89
|
+
subject.is_expired?.should be_true
|
90
90
|
end
|
91
91
|
end
|
92
92
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: credit_card_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ bindir: bin
|
|
11
11
|
cert_chain: []
|
12
12
|
date: 2012-09-21 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
|
-
description: Detects issuer from number, has ActiveModel validations and translations
|
14
|
+
description: Detects issuer from a number, has ActiveModel validations and translations
|
15
15
|
support.
|
16
16
|
email: margus@tione.eu
|
17
17
|
executables: []
|