Class: Yaks::Format::JsonAPI
Instance Attribute Summary
Attributes inherited from Yaks::Format
#env
Instance Method Summary
collapse
all, by_accept_header, by_media_type, by_name, #initialize, media_types, names, register
#deprecated_alias
Methods included from Util
#Resolve, #camelize, #extract_options, #reject_keys, #slice_hash, #symbolize_keys, #underscore
#to_proc
Constructor Details
This class inherits a constructor from Yaks::Format
Instance Method Details
#call(resource, _env = nil) ⇒ Hash
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/yaks/format/json_api.rb', line 10
def call(resource, _env = nil)
output = {}
if resource.collection?
output[:data] = resource.map(&method(:serialize_resource))
output[:links] = serialize_links(resource.links) if resource.links.any?
else
output[:data] = serialize_resource(resource)
end
included = resource.seq.each_with_object([]) do |res, array|
serialize_included_subresources(res.subresources, array)
end
output[:included] = included if included.any?
output[:meta] = resource[:meta] if resource[:meta]
output
end
|
#inverse ⇒ Object
102
103
104
|
# File 'lib/yaks/format/json_api.rb', line 102
def inverse
Yaks::Reader::JsonAPI.new
end
|
#serialize_included_resources(subresource, included) ⇒ Array
84
85
86
87
88
|
# File 'lib/yaks/format/json_api.rb', line 84
def serialize_included_resources(subresource, included)
subresource.seq.each_with_object(included) do |resource, memo|
serialize_subresource(resource, memo)
end
end
|
#serialize_included_subresources(subresources, array) ⇒ Array
75
76
77
78
79
|
# File 'lib/yaks/format/json_api.rb', line 75
def serialize_included_subresources(subresources, array)
subresources.each do |resources|
serialize_included_resources(resources, array)
end
end
|
#serialize_links(links) ⇒ Hash
29
30
31
32
33
|
# File 'lib/yaks/format/json_api.rb', line 29
def serialize_links(links)
links.each_with_object({}) do |link, hash|
hash[link.rel] = link.uri
end
end
|
#serialize_relationship(resource) ⇒ Array, Hash
63
64
65
66
67
68
69
70
|
# File 'lib/yaks/format/json_api.rb', line 63
def serialize_relationship(resource)
if resource.collection?
data = resource.map { |r| {type: pluralize(r.type), id: r[:id].to_s} }
elsif !resource.null_resource?
data = {type: pluralize(resource.type), id: resource[:id].to_s}
end
{data: data}
end
|
#serialize_relationships(subresources) ⇒ Hash
55
56
57
58
59
|
# File 'lib/yaks/format/json_api.rb', line 55
def serialize_relationships(subresources)
subresources.each_with_object({}) do |resource, hsh|
hsh[resource.rels.first.sub(/^rel:/, '').to_sym] = serialize_relationship(resource)
end
end
|
#serialize_resource(resource) ⇒ Hash
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/yaks/format/json_api.rb', line 37
def serialize_resource(resource)
result = {}
result[:type] = pluralize(resource.type)
result[:id] = resource[:id].to_s if resource[:id]
attributes = resource.attributes.reject { |k| k.equal?(:id) }
result[:attributes] = attributes if attributes.any?
relationships = serialize_relationships(resource.subresources)
result[:relationships] = relationships unless relationships.empty?
links = serialize_links(resource.links)
result[:links] = links unless links.empty?
result
end
|
#serialize_subresource(resource, included) ⇒ Hash
=> [{id: ‘3’, name: ‘foo’]}
95
96
97
98
99
100
|
# File 'lib/yaks/format/json_api.rb', line 95
def serialize_subresource(resource, included)
included << serialize_resource(resource) unless included.any? do |item|
item[:id].eql?(resource[:id].to_s) && item[:type].eql?(pluralize(resource.type))
end
serialize_included_subresources(resource.subresources, included)
end
|