class Bandwidth::BaseModel

Base model.

Public Instance Methods

to_hash() click to toggle source

Returns a Hash representation of the current object.

# File lib/bandwidth/models/base_model.rb, line 10
def to_hash
  hash = {}
  instance_variables.each do |name|
    value = instance_variable_get(name)
    name = name[1..-1]
    key = self.class.names.key?(name) ? self.class.names[name] : name

    hash[key] = nil
    unless value.nil?
      if respond_to?("to_#{name}")
        if (value.instance_of? Array) || (value.instance_of? Hash)
          params = [hash, key]
          hash[key] = send("to_#{name}", *params)
        else
          hash[key] = send("to_#{name}")
        end
      elsif value.instance_of? Array
        hash[key] = value.map { |v| v.is_a?(BaseModel) ? v.to_hash : v }
      elsif value.instance_of? Hash
        hash[key] = {}
        value.each do |k, v|
          hash[key][k] = v.is_a?(BaseModel) ? v.to_hash : v
        end
      else
        hash[key] = value.is_a?(BaseModel) ? value.to_hash : value
      end
    end
  end
  hash
end
to_json(options = {}) click to toggle source

Returns a JSON representation of the curent object.

# File lib/bandwidth/models/base_model.rb, line 42
def to_json(options = {})
  hash = to_hash.reject { |k, v| v.nil? }
  hash.to_json(options)
end