Module: AuthorizeNet::TypeConversions

Included in:
Response, Transaction
Defined in:
lib/authorize_net/authorize_net.rb

Overview

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

Constant Summary collapse

API_FIELD_PREFIX =
'x_'

Instance Method Summary collapse

Instance Method Details

#boolean_to_value(bool) ⇒ Object

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.



27
28
29
30
31
32
33
34
# 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) ⇒ Object

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.



61
62
63
64
65
66
67
68
# 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) ⇒ Object

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.



77
78
79
80
81
82
83
84
85
86
# 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) ⇒ Object

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.



43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object

Coverts an Integer into an Authorize.Net integer string.



94
95
96
# File 'lib/authorize_net/authorize_net.rb', line 94

def integer_to_value(int)
  int.to_s
end

#to_external_field(key) ⇒ Object

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



114
115
116
# 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) ⇒ Object

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.



121
122
123
124
# 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) ⇒ Object

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



100
101
102
103
104
105
106
107
108
109
# 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) ⇒ Object

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.



13
14
15
16
17
18
19
20
21
22
# 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) ⇒ Object

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



55
56
57
# 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) ⇒ Object

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



71
72
73
# 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) ⇒ Object

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



37
38
39
# File 'lib/authorize_net/authorize_net.rb', line 37

def value_to_decimal(value)
  BigDecimal.new(value)
end

#value_to_integer(value) ⇒ Object

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



89
90
91
# File 'lib/authorize_net/authorize_net.rb', line 89

def value_to_integer(value)
  value.to_s.to_i
end