Method: Commerce::APIHelper.form_encode

Defined in:
lib/commerce/api_helper.rb

.form_encode(obj, instance_name) ⇒ Hash

Form encodes an object.

Parameters:

  • An (Dynamic)

    object to form encode.

  • The (String)

    name of the object.

Returns:

  • (Hash)

    A form encoded representation of the object in the form of a hash.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/commerce/api_helper.rb', line 112

def self.form_encode(obj, instance_name)
  retval = Hash.new

  # If this is a structure, resolve it's field names.
  if obj.kind_of? BaseModel
    obj = obj.to_hash
  end
  
  # Create a form encoded hash for this object.
  if obj == nil
    nil         
  elsif obj.instance_of? Array
    obj.each_with_index do |value, index|
      retval.merge!(APIHelper.form_encode(value, instance_name + "[" + index.to_s + "]"))
    end
  elsif obj.instance_of? Hash
    obj.each do |key, value|
      retval.merge!(APIHelper.form_encode(value, instance_name + "[" + key + "]"))
    end
  else
    retval[instance_name] = obj
  end
  return retval
end