Class: PayCertify::Gateway::Base::Validation

Inherits:
Object
  • Object
show all
Defined in:
lib/paycertify/gateway/base/validation.rb

Constant Summary collapse

EMAIL_REGEX =
/\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
CREDIT_CARD_REGEX =
/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Validation

Returns a new instance of Validation.



11
12
13
14
# File 'lib/paycertify/gateway/base/validation.rb', line 11

def initialize(attributes)
  self.attributes = attributes
  self.errors = {}
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



9
10
11
# File 'lib/paycertify/gateway/base/validation.rb', line 9

def attributes
  @attributes
end

#errorsObject

Returns the value of attribute errors.



9
10
11
# File 'lib/paycertify/gateway/base/validation.rb', line 9

def errors
  @errors
end

Instance Method Details

#add_error(attribute, message) ⇒ Object



98
99
100
101
# File 'lib/paycertify/gateway/base/validation.rb', line 98

def add_error(attribute, message)
  self.errors[attribute[:name]] ||= []
  self.errors[attribute[:name]] << message
end

#amount_validation(attribute) ⇒ Object



84
85
86
87
88
# File 'lib/paycertify/gateway/base/validation.rb', line 84

def amount_validation(attribute)
  set_attribute(attribute, Float(value_for(attribute)))
rescue ArgumentError
  add_error(attribute, "Must be a float, integer or decimal")
end

#card_number_validation(attribute) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/paycertify/gateway/base/validation.rb', line 41

def card_number_validation(attribute)
  # Non decimal numbers should be stripped to match.
  set_attribute(attribute, value_for(attribute).gsub(/\D/, ''))

  unless value_for(attribute) =~ CREDIT_CARD_REGEX
    add_error(attribute, "Doesn't validate as a credit card.")
  end
end

#email_validation(attribute) ⇒ Object



24
25
26
27
28
# File 'lib/paycertify/gateway/base/validation.rb', line 24

def email_validation(attribute)
  unless value_for(attribute) =~ EMAIL_REGEX
    add_error(attribute, "Doesn't validate as an email.")
  end
end

#expiration_month_validation(attribute) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/paycertify/gateway/base/validation.rb', line 50

def expiration_month_validation(attribute)
  # if a string, check if length = 2 and smaller than int 12
  # if int, transform into string with zero pad and check if smaller than int 12
  integer = Integer(value_for(attribute))

  if integer > 12
    add_error(attribute, "Must be smaller than 12.")
  end

  set_attribute(attribute, integer.to_s.rjust(2, '0'))
  
rescue ArgumentError
  add_error(attribute, "Must be an integer.")
end

#expiration_year_validation(attribute) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/paycertify/gateway/base/validation.rb', line 65

def expiration_year_validation(attribute)
  # if length = 4, strip to 2;
  # if a string, check if length = 2 and smaller than int 12
  # if int, transform into string with zero pad and check if smaller than int 12


  is_four_digit = if value_for(attribute).is_a?(String)
    value_for(attribute).length == 4
  else
    value_for(attribute) > 999
  end

  integer_value = Integer(value_for(attribute))

  set_attribute(attribute, integer_value.to_s.last(2))
rescue
  add_error(attribute, "Must be a 2 to 4-digit string.")
end

#no_validation(_) ⇒ Object



16
# File 'lib/paycertify/gateway/base/validation.rb', line 16

def no_validation(_); end

#presence_validation(attribute) ⇒ Object



18
19
20
21
22
# File 'lib/paycertify/gateway/base/validation.rb', line 18

def presence_validation(attribute)
  if value_for(attribute).blank?
    add_error(attribute, "Required attribute not present")
  end
end

#set_attribute(attribute, value) ⇒ Object



94
95
96
# File 'lib/paycertify/gateway/base/validation.rb', line 94

def set_attribute(attribute, value)
  self.attributes[attribute[:name]] = value
end

#value_for(attribute) ⇒ Object



90
91
92
# File 'lib/paycertify/gateway/base/validation.rb', line 90

def value_for(attribute)
  attributes[attribute[:name]]
end

#zip_validation(attribute) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/paycertify/gateway/base/validation.rb', line 30

def zip_validation(attribute)
  set_attribute(attribute, Integer(value_for(attribute)).to_s)

  unless value_for(attribute).length == 5
    add_error(attribute, "Must be a 5-digit string that can evaluate to a number.")
  end

rescue
  add_error(attribute, "Must be a 5-digit string that can evaluate to a number.")
end