Class: JsonapiCompliable::Deserializer

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi_compliable/deserializer.rb

Overview

Responsible for parsing incoming write payloads

Given a PUT payload like:

{
  data: {
    id: '1',
    type: 'posts',
    attributes: { title: 'My Title' },
    relationships: {
      author: {
        data: {
          id: '1',
          type: 'authors'
        }
      }
    }
  },
  included: [
    {
      id: '1'
      type: 'authors',
      attributes: { name: 'Joe Author' }
    }
  ]
}

You can now easily deal with this payload:

deserializer.attributes
# => { id: '1', title: 'My Title' }
deserializer.meta
# => { type: 'posts', method: :update }
deserializer.relationships
# {
#   author: {
#     meta: { ... },
#     attributes: { ... },
#     relationships: { ... }
#   }
# }

When creating objects, we accept a temp-id so that the client can track the object it just created. Expect this in meta:

{ type: 'authors', method: :create, temp_id: 'abc123' }

Instance Method Summary collapse

Constructor Details

#initialize(payload, env) ⇒ Deserializer

Returns a new instance of Deserializer

Parameters:

  • payload (Hash)

    The incoming payload with symbolized keys

  • env (Hash)

    the Rack env (e.g. request.env).



50
51
52
53
# File 'lib/jsonapi_compliable/deserializer.rb', line 50

def initialize(payload, env)
  @payload = payload
  @env = env
end

Instance Method Details

#attributesHash

Returns the raw :attributes hash + id

Returns:

  • (Hash)

    the raw :attributes hash + id



66
67
68
69
70
# File 'lib/jsonapi_compliable/deserializer.rb', line 66

def attributes
  @attributes ||= raw_attributes.tap do |hash|
    hash.merge!(id: id) if id
  end
end

#dataHash

Returns the raw :data value of the payload

Returns:

  • (Hash)

    the raw :data value of the payload



56
57
58
# File 'lib/jsonapi_compliable/deserializer.rb', line 56

def data
  @payload[:data]
end

#idString

Returns the raw :id value of the payload

Returns:

  • (String)

    the raw :id value of the payload



61
62
63
# File 'lib/jsonapi_compliable/deserializer.rb', line 61

def id
  data[:id]
end

#include_directive(memo = {}, relationship_node = nil) ⇒ Hash

Parses the relationships recursively and builds an all-hash include directive like

{ posts: { comments: {} } }

Relationships that have been marked for destruction will NOT be part of the include directive.

Returns:

  • (Hash)

    the include directive



101
102
103
104
105
106
107
108
109
# File 'lib/jsonapi_compliable/deserializer.rb', line 101

def include_directive(memo = {}, relationship_node = nil)
  relationship_node ||= relationships

  relationship_node.each_pair do |name, relationship_payload|
    merge_include_directive(memo, name, relationship_payload)
  end

  memo
end

#metaHash

'meta' information about this resource. Includes:

type: the jsonapi type method: create/update/destroy/disassociate. Based on the request env or the method within the relationships hash temp_id: the temp-id, if specified

Returns:

  • (Hash)


79
80
81
82
83
84
85
# File 'lib/jsonapi_compliable/deserializer.rb', line 79

def meta
  {
    type: data[:type],
    temp_id: data[:temp-id'],
    method: method
  }
end

#relationshipsHash

Returns the relationships hash

Returns:

  • (Hash)

    the relationships hash



88
89
90
# File 'lib/jsonapi_compliable/deserializer.rb', line 88

def relationships
  @relationships ||= process_relationships(raw_relationships)
end