Class: RippleRest::RestObject

Inherits:
Object
  • Object
show all
Defined in:
lib/ripple-rest/rest-object.rb

Instance Method Summary collapse

Constructor Details

#initialize(data = nil) ⇒ RestObject

Returns a new instance of RestObject.

Parameters:

  • data (Hash) (defaults to: nil)

131
132
133
134
135
136
137
138
139
140
# File 'lib/ripple-rest/rest-object.rb', line 131

def initialize data = nil
  data.each do |key, value|
    if @@properties[self.class][key.to_sym]
      self.instance_variable_set :"@#{key}", RestTypeHelper.convert_from(@@properties[self.class][key.to_sym], value)
    else
      warn "Cannot found field `#{key}' in RippleRestObject #{self.class}. The value will leave as-is. However, this will not be serialized to JSON."
      self.instance_variable_set :"@#{key}",  value
    end
  end if data
end

Instance Method Details

#to_hashHash

Convert to a hash

Returns:

  • (Hash)

144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/ripple-rest/rest-object.rb', line 144

def to_hash
  result = {}
  @@properties[self.class].each do |key, type|
    value = self.instance_variable_get(:"@#{key}")
    if @@required[self.class][key] && value == nil
      raise ArgumentError.new("Field `#{key}' is required in RippleRestObject #{self.class}.")
    end
    result[key] = RestTypeHelper.convert_to(type, value) if value != nil
  end
  
  result
end