-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathrenderer.rb
76 lines (69 loc) · 2.49 KB
/
renderer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require 'jsonapi/renderer'
module JSONAPI
module Serializable
class Renderer
def initialize(renderer = JSONAPI::Renderer.new)
@renderer = renderer
end
# Serialize resources into a JSON API document.
#
# @param resources [nil,Object,Array]
# @param options [Hash] @see JSONAPI.render
# @option class [Hash{Symbol=>Class}] A map specifying for each type
# the corresponding serializable resource class.
# @option expose [Hash] The exposures made available in serializable
# resource class instances as instance variables.
# @return [Hash]
#
# @example
# renderer.render(nil)
# # => { data: nil }
#
# @example
# renderer.render(user, class: { User: SerializableUser })
# # => {
# data: {
# type: 'users',
# id: 'foo',
# attributes: { ... },
# relationships: { ... }
# }
# }
#
# @example
# renderer.render([user1, user2], class: { User: SerializableUser })
# # => { data: [{ type: 'users', id: 'foo', ... }, ...] }
def render(resources, options = {})
options = options.dup
klass = options.delete(:class) || {}
exposures = options.delete(:expose) || {}
exposures = exposures.merge(_class: klass)
resources =
JSONAPI::Serializable.resources_for(resources, exposures, klass)
@renderer.render(options.merge(data: resources))
end
# Serialize errors into a JSON API document.
#
# @param errors [Array]
# @param options [Hash] @see JSONAPI.render
# @option class [Hash{Symbol=>Class}] A map specifying for each type
# the corresponding serializable error class.
# @option expose [Hash] The exposures made available in serializable
# error class instances as instance variables.
# @return [Hash]
#
# @example
# error = JSONAPI::Serializable::Error.create(id: 'foo', title: 'bar')
# renderer.render([error])
# # => { errors: [{ id: 'foo', title: 'bar' }] }
def render_errors(errors, options = {})
options = options.dup
klass = options.delete(:class) || {}
exposures = options.delete(:expose) || {}
errors =
JSONAPI::Serializable.resources_for(errors, exposures, klass)
@renderer.render(options.merge(errors: errors))
end
end
end
end