Class: Graphiti::Deserializer
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.
# => { 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 Attribute Summary collapse
-
#attributes ⇒ Hash
The raw :attributes hash +
id
.
Instance Method Summary collapse
-
#data ⇒ Hash
The raw :data value of the payload.
-
#id ⇒ String
The raw :id value of the payload.
- #include_hash(memo = {}, relationship_node = nil) ⇒ Object
-
#initialize(payload = {}) ⇒ Deserializer
constructor
A new instance of Deserializer.
-
#meta(action: :update) ⇒ Hash
‘meta’ information about this resource.
- #params ⇒ Object
-
#relationships ⇒ Hash
The relationships hash.
Constructor Details
#initialize(payload = {}) ⇒ Deserializer
Returns a new instance of Deserializer.
48 49 50 51 |
# File 'lib/graphiti/deserializer.rb', line 48 def initialize(payload = {}) @payload = payload @payload = @payload[:_jsonapi] if @payload.key?(:_jsonapi) end |
Instance Attribute Details
#attributes ⇒ Hash
Returns the raw :attributes hash + id
.
68 69 70 71 72 |
# File 'lib/graphiti/deserializer.rb', line 68 def attributes @attributes ||= raw_attributes.tap do |hash| hash[:id] = id if id end end |
Instance Method Details
#data ⇒ Hash
Returns the raw :data value of the payload.
58 59 60 |
# File 'lib/graphiti/deserializer.rb', line 58 def data @payload.fetch(:data, {}) end |
#id ⇒ String
Returns the raw :id value of the payload.
63 64 65 |
# File 'lib/graphiti/deserializer.rb', line 63 def id data[:id] end |
#include_hash(memo = {}, relationship_node = nil) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/graphiti/deserializer.rb', line 99 def include_hash(memo = {}, relationship_node = nil) relationship_node ||= relationships relationship_node.each_pair do |name, relationship_payload| merge_include_hash(memo, name, relationship_payload) end memo end |
#meta(action: :update) ⇒ Hash
‘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
85 86 87 88 89 90 91 92 |
# File 'lib/graphiti/deserializer.rb', line 85 def (action: :update) { type: data[:type], temp_id: data[:'temp-id'], method: action, payload_path: ["data"] } end |
#params ⇒ Object
53 54 55 |
# File 'lib/graphiti/deserializer.rb', line 53 def params @payload end |
#relationships ⇒ Hash
Returns the relationships hash.
95 96 97 |
# File 'lib/graphiti/deserializer.rb', line 95 def relationships @relationships ||= process_relationships(raw_relationships) end |