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.



10
11
12
# File 'lib/fat_zebra/fat_zebra_object.rb', line 10

def accepted
  @accepted
end

Class Method Details

.initialize_from(response) ⇒ FatZebra::FatZebraObject

Initialize from the API response



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

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)


61
62
63
64
65
# File 'lib/fat_zebra/fat_zebra_object.rb', line 61

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:



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

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