Module: SKApi::Utils::Serializer::ClassMethods

Defined in:
lib/utils/serializer.rb

Instance Method Summary collapse

Instance Method Details

#to_hash_from_schema(obj) ⇒ Object

Create a Hash with the available (api)object attributes defined in api_fields.

Parameter

obj<object>:. An object which is returned as hash



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/utils/serializer.rb', line 21

def to_hash_from_schema(obj)
  # first set the root node to the objects class name as symbol
  obj_class_name =  obj.class.name.split('::').last.underscore
#          obj_class_name = obj.class.to_s.underscore.to_sym
  data = { obj_class_name => {} }
  # iterate over the defined api fields hash
  self.schema_props.each do |field, props| 
    if props['type'] == 'array'
      # always set the field, so the user can expect an empty array
      data[obj_class_name][field] = []    
      if rel_objects = obj.send( field )
        rel_objects.each do |rel_obj|
          # setup scope for related class
          klass = "SKApi::Resources::#{rel_obj.class}".constantize
          # call related objects to_hash_from_schema method ex: data[:client][:addresses] << SKApi::Models::Address.to_hash_from_schema(object)
          data[obj_class_name][field] << klass.to_hash_from_schema(rel_obj)
        end
      end
    elsif props['type'] == 'object'     # a singular resource TODO should we add an empty object?
      if rel_obj = obj.send( field )
        klass = "SKApi::Resources::#{rel_obj.class}".constantize
        # ex: data['invoice']['client'] = SKApi::Models::Client.to_hash_from_schema(client)
        data[obj_class_name][field] = klass.to_hash_from_schema(rel_obj)
      end
      else # a simple field which can be directly called, only added of objects know its
      data[obj_class_name][field] = obj.send(field) if obj.respond_to?(field.to_sym)
    end
  end
  data
end

#to_json(obj) ⇒ Object



52
53
54
55
56
# File 'lib/utils/serializer.rb', line 52

def to_json(obj)
  data = self.to_hash_from_schema(obj)
#          data[:links] = self.api_links
  ActiveSupport::JSON.encode(data)
end