module AuthorizeNet::TypeConversions

Some type conversion routines that will be injected into our Transaction/Response classes.

Constants

API_FIELD_PREFIX

Public Instance Methods

boolean_to_value(bool) click to toggle source

Converts a boolean into an Authorize.Net boolean value string. This is designed to handle the wide range of boolean formats that Authorize.Net uses. If bool isn’t a Boolean, its converted to a string and passed along.

# File lib/authorize_net/authorize_net.rb, line 27
def boolean_to_value(bool)
  case bool
  when TrueClass, FalseClass
    bool ? 'TRUE' : 'FALSE'
  else
    bool.to_s
  end
end
date_to_value(date) click to toggle source

Converts a Date (or DateTime, or Time) into an Authorize.Net date value string. If date isn’t a Date (or DateTime, or Time), its converted to a string and passed along.

# File lib/authorize_net/authorize_net.rb, line 61
def date_to_value(date)
  case date
  when Date, DateTime, Time
    date.strftime('%Y-%m-%d')
  else
    date.to_s
  end
end
datetime_to_value(datetime) click to toggle source

Converts a Date (or DateTime, or Time) into an Authorize.Net datetime value string. If date isn’t a Date (or DateTime, or Time), its converted to a string and passed along.

# File lib/authorize_net/authorize_net.rb, line 77
def datetime_to_value(datetime)
  case datetime
  when Date, DateTime
    datetime.new_offset(0).strftime('%Y-%m-%dT%H:%M:%SZ')
  when Time
    datetime.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
  else
    datetime.to_s
  end
end
decimal_to_value(float) click to toggle source

Converts a BigDecimal (or Float) into an Authorize.Net float value string. If float isn’t a BigDecimal (or Float), its converted to a string and passed along.

# File lib/authorize_net/authorize_net.rb, line 43
def decimal_to_value(float)
  case float
  when Float
    "%0.2f" % float
  when BigDecimal
    float.truncate(2).to_s('F')
  else
    float.to_s
  end
end
integer_to_value(int) click to toggle source

Coverts an Integer into an Authorize.Net integer string.

# File lib/authorize_net/authorize_net.rb, line 94
def integer_to_value(int)
  int.to_s
end
to_external_field(key) click to toggle source

Converts an internal field name (Symbol) into an external field name (Symbol) that can be consumed by the Authorize.Net API.

# File lib/authorize_net/authorize_net.rb, line 114
def to_external_field(key)
  (API_FIELD_PREFIX + key.to_s).to_sym
end
to_internal_field(key) click to toggle source

Converts an external field name (Symbol) into an internal field name (Symbol). This is the exact inverse of to_external_field. Running to_internal_field(to_external_field(:foo)) would return :foo back.

# File lib/authorize_net/authorize_net.rb, line 121
def to_internal_field(key)
  k_str = key.to_s
  k_str[API_FIELD_PREFIX.length..k_str.length].to_sym
end
to_param(key, value, key_prefix = API_FIELD_PREFIX) click to toggle source

Converts a key value pair into a HTTP POST parameter. The key is prefixed with key_prefix when being converted to a parameter name.

# File lib/authorize_net/authorize_net.rb, line 100
def to_param(key, value, key_prefix = API_FIELD_PREFIX)
  key_str = "#{key_prefix}#{key}="
  if value.kind_of?(Array) 
    (value.collect do |v|
      key_str + CGI::escape(v.to_s)
    end).join('&')
  else
    key_str + CGI::escape(value.to_s)
  end
end
value_to_boolean(value) click to toggle source

Coverts a value received from Authorize.Net into a boolean if possible. This is designed to handle the wide range of boolean formats that Authorize.Net uses.

# File lib/authorize_net/authorize_net.rb, line 13
def value_to_boolean(value)
  case value
  when "TRUE", "T", "YES", "Y", "1", "true"
    true
  when "FALSE", "F", "NO", "N", "0", "false"
    false
  else
    value
  end
end
value_to_date(value) click to toggle source

Coverts a value received from Authorize.Net into a Date.

# File lib/authorize_net/authorize_net.rb, line 55
def value_to_date(value)
  Date.strptime(value, '%Y-%m-%d')
end
value_to_datetime(value) click to toggle source

Coverts a value received from Authorize.Net into a DateTime.

# File lib/authorize_net/authorize_net.rb, line 71
def value_to_datetime(value)
  DateTime.strptime(value, '%Y-%m-%dT%H:%M:%S')
end
value_to_decimal(value) click to toggle source

Coverts a value received from Authorize.Net into a BigDecimal.

# File lib/authorize_net/authorize_net.rb, line 37
def value_to_decimal(value)
  BigDecimal.new(value)
end
value_to_integer(value) click to toggle source

Coverts a value received from Authorize.Net into an Integer.

# File lib/authorize_net/authorize_net.rb, line 89
def value_to_integer(value)
  value.to_s.to_i
end