Class: FatZebra::FatZebraObject

Inherits:
Object
  • Object
show all
Extended by:
Validation
Includes:
ObjectHelper
Defined in:
lib/fat_zebra/fat_zebra_object.rb

Overview

FatZebra Object

Define the API objects

Direct Known Subclasses

APIResource

Instance Attribute Summary collapse

Attributes included from ObjectHelper

#data, #errors, #raw

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validation

errors, valid!, valid?, validates

Methods included from ObjectHelper

#[], #[]=, #add_accessor, #add_accessors, #add_data, #initialize, #inspect, #keys, #method_missing, #remove_accessor, #to_hash, #to_json, #update_attributes

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class FatZebra::ObjectHelper

Instance Attribute Details

#acceptedObject (readonly) Also known as: accepted?

Returns the value of attribute accepted.



12
13
14
# File 'lib/fat_zebra/fat_zebra_object.rb', line 12

def accepted
  @accepted
end

Class Method Details

.initialize_from(response) ⇒ FatZebra::FatZebraObject

Initialize from the API response



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fat_zebra/fat_zebra_object.rb', line 21

def initialize_from(response)
  object = new

  object.load_response_api(response.is_a?(Hash) ? response : JSON.parse(response))

  if object.raw['response']
    if object.raw['response'].is_a?(Array)
      object.raw['response'].each { |response_object| object.add_data(initialize_from(response_object)) }
    else
      object.update_attributes(object.raw['response'])
    end
  else
    object.update_attributes(object.raw)
  end

  object
end

Instance Method Details

#load_response_api(response) ⇒ Object

Load the root components for the API response

Parameters:

  • values (Hash)


63
64
65
66
67
# File 'lib/fat_zebra/fat_zebra_object.rb', line 63

def load_response_api(response)
  @raw        = response
  @accepted   = response['successful']
  @errors     = response['errors']
end

#update_from(response) ⇒ FatZebra::FatZebraObject

Update the object based on the response from the API Remove and new acessor

Parameters:

  • response (Hash)

Returns:



48
49
50
51
52
53
54
55
56
57
# File 'lib/fat_zebra/fat_zebra_object.rb', line 48

def update_from(response)
  load_response_api(response.is_a?(Hash) ? response : JSON.parse(response))

  # attributes to remove
  (@values.keys - raw['response'].keys).each { |key| remove_accessor(key) }

  update_attributes(raw['response'])

  self
end