class PayCertify::Gateway::Base::Validation

Constants

CREDIT_CARD_REGEX
EMAIL_REGEX

Attributes

attributes[RW]
errors[RW]

Public Class Methods

new(attributes) click to toggle source
# File lib/paycertify/gateway/base/validation.rb, line 11
def initialize(attributes)
  self.attributes = attributes
  self.errors = {}
end

Public Instance Methods

add_error(attribute, message) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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(_) click to toggle source
# File lib/paycertify/gateway/base/validation.rb, line 16
def no_validation(_); end
presence_validation(attribute) click to toggle source
# 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) click to toggle source
# File lib/paycertify/gateway/base/validation.rb, line 94
def set_attribute(attribute, value)
  self.attributes[attribute[:name]] = value
end
value_for(attribute) click to toggle source
# File lib/paycertify/gateway/base/validation.rb, line 90
def value_for(attribute)
  attributes[attribute[:name]]
end
zip_validation(attribute) click to toggle source
# 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