evelpidon_validators 0.4.4 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NWNiMWUwMWZiMzYzZmE3YjhjNTQ2NWM2MDZlYzg3YjNmNDlkZGViZg==
5
+ data.tar.gz: !binary |-
6
+ ZDBkMTliNDgwMzkzYmFlYzMyMTY1YWEzMDM1YzVlMjNkZWRjOTUzYw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ OTAyNTVkOTVjZWRhMzc1YWY4YzExYTU1MTFhYjQzZThhM2UzMjMwYmE5NmQ4
10
+ NDU5YmU0YmJhOGY3NWY5ZjRjZTBiMWRlNTU2Y2VjYzIxZDk2MTNiMTA3NGY5
11
+ YjY1OGQyNTM0MGM3ZGM5ZjQ1MWRjZjFjYmJmOTM3NzdhZGY2MDM=
12
+ data.tar.gz: !binary |-
13
+ NDRhZWZmOTkxYTgwZDU3YjM1NTk3NDZmMTllZTMyOWQ5Njc1NWJlZjBjZmJk
14
+ YjkxYmQwNGZiYjYzZDEwM2U3ZDZkNGUwNzAwMjIzNWY0NTUzNDQ2ZTljMzdj
15
+ OTZmM2JjMjUwMGY4ZTllZjM2MmFhZWI2ODZiZGEwMzcxNWQ0ZjU=
data/.gitignore CHANGED
@@ -2,6 +2,8 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  .rvmrc
5
+ .ruby-version
6
+ .ruby-gemset
5
7
  pkg/*
6
8
  .idea
7
9
  *.iml
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Evelpidon Validators changelog
2
2
 
3
+ ## 0.5.0 / 2013-08-29
4
+
5
+ * CreditCardValidator learned the option of `:luhn_only` bypassing the actual cc type check.
6
+ Includes pure JS implementation.
7
+
3
8
  ## 0.4.4 / 2013-06-18
4
9
 
5
10
  * Test numbers are considered valid in all Rails environments except production.
@@ -1,7 +1,38 @@
1
+ clientSideValidations.helpers = clientSideValidations.helpers || {};
2
+
3
+ /**
4
+ * Variant of Avraham Plotnitzky's String.prototype method mixed with the "fast" version
5
+ * see: https://sites.google.com/site/abapexamples/javascript/luhn-validation.
6
+ *
7
+ * Taken from https://gist.github.com/ShirtlessKirk/2134376
8
+ *
9
+ * @param luhn {Integer}
10
+ * @returns {boolean}
11
+ */
12
+ clientSideValidations.helpers.luhnChk = function(luhn) {
13
+ var len = luhn.length,
14
+ mul = 0,
15
+ prodArr = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]],
16
+ sum = 0;
17
+
18
+ while (len--) {
19
+ sum += prodArr[mul][parseInt(luhn.charAt(len), 10)];
20
+ mul ^= 1;
21
+ }
22
+
23
+ return sum % 10 === 0 && sum > 0;
24
+ };
25
+
1
26
  clientSideValidations.validators.remote['credit_card'] = function(element, options) {
2
- if ($.ajax({
3
- url: '/validators/credit_card.json',
4
- data: { value: element.val(), type: clientSideValidations.helpers.getSiblingField(element, options.type_attribute).val() },
5
- async: false
6
- }).status == 404) { return options.message; }
27
+ if (options.luhn_only) {
28
+ if (clientSideValidations.helpers.luhnChk(element.val()) == false) {
29
+ return options.message;
30
+ }
31
+ } else {
32
+ if ($.ajax({
33
+ url: '/validators/credit_card.json',
34
+ data: { value: element.val(), type: clientSideValidations.helpers.getSiblingField(element, options.type_attribute).val()},
35
+ async: false
36
+ }).status == 404) { return options.message; }
37
+ }
7
38
  };
@@ -27,10 +27,20 @@ module ActiveModel
27
27
  ::CreditCardValidator::Validator.valid?(number)
28
28
  end
29
29
 
30
+ def self.valid_luhn?(number)
31
+ ::CreditCardValidator::Validator.verify_luhn(number)
32
+ end
33
+
30
34
  def validate_each(record, attribute, value)
31
- type = record.send(options[:type_attribute]) if options[:type_attribute]
32
- unless self.class.valid_credit_card? value, type
33
- record.errors.add(attribute, :credit_card, options)
35
+ if options[:luhn_only]
36
+ unless self.class.valid_luhn? value
37
+ record.errors.add(attribute, :credit_card, options)
38
+ end
39
+ else
40
+ type = record.send(options[:type_attribute]) if options[:type_attribute]
41
+ unless self.class.valid_credit_card? value, type
42
+ record.errors.add(attribute, :credit_card, options)
43
+ end
34
44
  end
35
45
  end
36
46
  end
@@ -40,7 +50,12 @@ end
40
50
  module ClientSideValidations::Middleware
41
51
  class CreditCard < Base
42
52
  def response
43
- self.status = ActiveModel::Validations::CreditCardValidator.valid_credit_card?(request.params[:value], request.params[:type]) ? 200 : 404
53
+ if request.params[:luhn_only]
54
+ self.status = ActiveModel::Validations::CreditCardValidator.valid_luhn?(request.params[:value]) ? 200 : 404
55
+ else
56
+ self.status = ActiveModel::Validations::CreditCardValidator.valid_credit_card?(request.params[:value], request.params[:type]) ? 200 : 404
57
+ end
58
+
44
59
  super
45
60
  end
46
61
  end
@@ -1,3 +1,3 @@
1
1
  module EvelpidonValidators
2
- VERSION = "0.4.4"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -113,6 +113,24 @@ class CreditCardValidatorTest < ActiveSupport::TestCase
113
113
  assert_valid_card_type_and_number 'Maestro', '4111111111111111'
114
114
  end
115
115
 
116
+ test "luhn check only" do
117
+ class PaymentWithNoTypeValidation
118
+ include ActiveModel::Validations
119
+ attr_accessor :card_type, :card_number
120
+ validates :card_number, :credit_card => {:luhn_only => true}
121
+ end
122
+
123
+ @payment = PaymentWithNoTypeValidation.new
124
+
125
+ %w(
126
+ 50339619890917 586824160825533338 6759411100000008 5641821111166669 6759560045005727054
127
+ 3530111333300000 4111111111111111 5555555555554444 30569309025904 371449635398431
128
+ 6011000990139424
129
+ ).each do |cc_number|
130
+ assert_valid_card_number cc_number
131
+ end
132
+ end
133
+
116
134
  #########
117
135
  protected
118
136
  #########
@@ -123,6 +141,11 @@ class CreditCardValidatorTest < ActiveSupport::TestCase
123
141
  assert_valid_attribute @payment, :card_number
124
142
  end
125
143
 
144
+ def assert_valid_card_number(number)
145
+ @payment.card_number = number
146
+ assert_valid_attribute @payment, :card_number
147
+ end
148
+
126
149
  def assert_invalid_card_type_and_number(type, number)
127
150
  @payment.card_type = type
128
151
  @payment.card_number = number
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evelpidon_validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
5
- prerelease:
4
+ version: 0.5.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Nikos Dimitrakopoulos
@@ -10,12 +9,11 @@ authors:
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2013-06-18 00:00:00.000000000 Z
12
+ date: 2013-08-29 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: rake
17
16
  requirement: !ruby/object:Gem::Requirement
18
- none: false
19
17
  requirements:
20
18
  - - ! '>='
21
19
  - !ruby/object:Gem::Version
@@ -23,7 +21,6 @@ dependencies:
23
21
  type: :development
24
22
  prerelease: false
25
23
  version_requirements: !ruby/object:Gem::Requirement
26
- none: false
27
24
  requirements:
28
25
  - - ! '>='
29
26
  - !ruby/object:Gem::Version
@@ -31,7 +28,6 @@ dependencies:
31
28
  - !ruby/object:Gem::Dependency
32
29
  name: activesupport
33
30
  requirement: !ruby/object:Gem::Requirement
34
- none: false
35
31
  requirements:
36
32
  - - ! '>='
37
33
  - !ruby/object:Gem::Version
@@ -39,7 +35,6 @@ dependencies:
39
35
  type: :development
40
36
  prerelease: false
41
37
  version_requirements: !ruby/object:Gem::Requirement
42
- none: false
43
38
  requirements:
44
39
  - - ! '>='
45
40
  - !ruby/object:Gem::Version
@@ -47,7 +42,6 @@ dependencies:
47
42
  - !ruby/object:Gem::Dependency
48
43
  name: evelpidon_test_helpers
49
44
  requirement: !ruby/object:Gem::Requirement
50
- none: false
51
45
  requirements:
52
46
  - - ! '>='
53
47
  - !ruby/object:Gem::Version
@@ -55,7 +49,6 @@ dependencies:
55
49
  type: :development
56
50
  prerelease: false
57
51
  version_requirements: !ruby/object:Gem::Requirement
58
- none: false
59
52
  requirements:
60
53
  - - ! '>='
61
54
  - !ruby/object:Gem::Version
@@ -63,7 +56,6 @@ dependencies:
63
56
  - !ruby/object:Gem::Dependency
64
57
  name: test-unit
65
58
  requirement: !ruby/object:Gem::Requirement
66
- none: false
67
59
  requirements:
68
60
  - - ! '>='
69
61
  - !ruby/object:Gem::Version
@@ -71,7 +63,6 @@ dependencies:
71
63
  type: :development
72
64
  prerelease: false
73
65
  version_requirements: !ruby/object:Gem::Requirement
74
- none: false
75
66
  requirements:
76
67
  - - ! '>='
77
68
  - !ruby/object:Gem::Version
@@ -79,7 +70,6 @@ dependencies:
79
70
  - !ruby/object:Gem::Dependency
80
71
  name: activemodel
81
72
  requirement: !ruby/object:Gem::Requirement
82
- none: false
83
73
  requirements:
84
74
  - - ! '>='
85
75
  - !ruby/object:Gem::Version
@@ -87,7 +77,6 @@ dependencies:
87
77
  type: :runtime
88
78
  prerelease: false
89
79
  version_requirements: !ruby/object:Gem::Requirement
90
- none: false
91
80
  requirements:
92
81
  - - ! '>='
93
82
  - !ruby/object:Gem::Version
@@ -95,7 +84,6 @@ dependencies:
95
84
  - !ruby/object:Gem::Dependency
96
85
  name: credit_card_validator
97
86
  requirement: !ruby/object:Gem::Requirement
98
- none: false
99
87
  requirements:
100
88
  - - ! '>='
101
89
  - !ruby/object:Gem::Version
@@ -103,7 +91,6 @@ dependencies:
103
91
  type: :runtime
104
92
  prerelease: false
105
93
  version_requirements: !ruby/object:Gem::Requirement
106
- none: false
107
94
  requirements:
108
95
  - - ! '>='
109
96
  - !ruby/object:Gem::Version
@@ -145,33 +132,26 @@ files:
145
132
  - test/test_helper.rb
146
133
  homepage: ''
147
134
  licenses: []
135
+ metadata: {}
148
136
  post_install_message:
149
137
  rdoc_options: []
150
138
  require_paths:
151
139
  - lib
152
140
  required_ruby_version: !ruby/object:Gem::Requirement
153
- none: false
154
141
  requirements:
155
142
  - - ! '>='
156
143
  - !ruby/object:Gem::Version
157
144
  version: '0'
158
- segments:
159
- - 0
160
- hash: 692248226337988979
161
145
  required_rubygems_version: !ruby/object:Gem::Requirement
162
- none: false
163
146
  requirements:
164
147
  - - ! '>='
165
148
  - !ruby/object:Gem::Version
166
149
  version: '0'
167
- segments:
168
- - 0
169
- hash: 692248226337988979
170
150
  requirements: []
171
151
  rubyforge_project: evelpidon_validators
172
- rubygems_version: 1.8.25
152
+ rubygems_version: 2.0.3
173
153
  signing_key:
174
- specification_version: 3
154
+ specification_version: 4
175
155
  summary: Useful ActiveModel validators
176
156
  test_files:
177
157
  - test/associated_test.rb