-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrendering.rb
67 lines (57 loc) · 1.76 KB
/
rendering.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
require 'jsonapi/serializable'
require 'jsonapi/hanami/rendering/dsl'
module JSONAPI
module Hanami
module Rendering
def self.included(base)
base.class_eval do
include JSONAPI::Hanami::Rendering::DSL
after do
_jsonapi_render if @_body.nil?
end
end
end
def _jsonapi_render
if @_jsonapi.key?(:errors)
_jsonapi_render_error
else
_jsonapi_render_success
end
end
def _jsonapi_render_success
self.format = :jsonapi if @format.nil?
return unless @_jsonapi.key?(:data)
self.body = JSONAPI::Serializable::Renderer.render(@_jsonapi[:data],
_jsonapi_params)
end
# NOTE(beauby): It might be worth factoring those methods out into a
# class.
def _jsonapi_params
# TODO(beauby): Inject global params (toplevel jsonapi, etc.).
@_jsonapi.dup.merge!(expose: _jsonapi_exposures)
end
def _jsonapi_exposures
{ routes: routes }.merge!(exposures)
end
def _jsonapi_render_error
document =
JSONAPI::Serializable::ErrorRenderer.render(@_jsonapi[:errors],
_jsonapi_error_params)
self.status = _jsonapi_error_status unless @_status
self.format = :jsonapi if @format.nil?
self.body = document
end
def _jsonapi_error_status
# TODO(beauby): Set HTTP status code accordingly.
400
end
def _jsonapi_error_params
@_jsonapi
end
def _jsonapi_errors
# TODO(beauby): Implement inferrence for Hanami::Validations.
@_jsonapi[:errors]
end
end
end
end