credit_card_support 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- number = record.send(opts[:number]||:number) rescue (raise ":number not defined")
13
- expiry_year = record.send(opts[:expiry_year]||:expiry_year) rescue (raise ":expiry_year not defined")
14
- expiry_month = record.send(opts[:expiry_month]||:expiry_month) rescue (raise ":expiry_month not defined")
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
- @opts = opts
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
- validate_luhn
37
- validate_issuer
38
- validate_testcard
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 false
47
+ validate_expiration_date rescue nil
42
48
  end
43
49
 
44
- def validate_luhn
45
- errors.add(@opts[:number], t(:luhn_not_valid)) unless @instrument.has_valid_luhn?
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 validate_issuer
49
- errors.add(@opts[:number], t(:issuer_not_known)) unless @instrument.issuer
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(@opts[:number], t(:issuer_not_supported, issuer: @instrument.issuer.to_s.upcase))
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 validate_testcard
56
- errors.add(@opts[:number], t(:testcard_not_supported)) if !@allow_testcards && @instrument.is_testcard?
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(@opts[:expiry_year], t(:cant_be_blank)) unless @instrument.expiry_year
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(@opts[:expiry_month], t(:cant_be_blank)) unless @instrument.expiry_month
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.expired?
69
- errors.add(@opts[:expiry_year], t(:cant_be_expired))
70
- errors.add(@opts[:expiry_month], t(:cant_be_expired))
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 expired?
149
+ def is_expired?
150
150
  expiration_date < Date.today
151
151
  end
152
152
 
@@ -7,3 +7,4 @@ en:
7
7
  testcard_not_supported: "testcards not supported"
8
8
  cant_be_blank: "can't be blank"
9
9
  is_expired: "is expired"
10
+ cant_be_so_far_in_the_future: "can't be so far in the future"
@@ -2,7 +2,7 @@ module CreditCardSupport
2
2
  module Version
3
3
  MAJOR = 1
4
4
  MINOR = 0
5
- PATCH = 0
5
+ PATCH = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
8
  end
@@ -1,2 +1,3 @@
1
1
  require 'credit_card_support/luhn_number'
2
2
  require 'credit_card_support/instrument'
3
+ require 'credit_card_support/credit_card_validator' if Object.const_defined?(:ActiveModel)
@@ -1,6 +1,5 @@
1
- require 'spec_helper'
2
1
  require 'active_model'
3
- require 'credit_card_support/credit_card_validator'
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 "#expired?" do
79
+ describe "#is_expired?" do
80
80
  context "not expired" do
81
81
  it "returns false" do
82
- subject.expired?.should be_false
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.expired?.should be_true
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.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: []