Module: JSONAPI::Hanami::Rendering

Defined in:
lib/jsonapi/hanami/rendering.rb,
lib/jsonapi/hanami/rendering/dsl.rb

Defined Under Namespace

Modules: DSL

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/jsonapi/hanami/rendering.rb', line 7

def self.included(base)
  base.class_eval do
    include JSONAPI::Hanami::Rendering::DSL

    after do
      _jsonapi_render if @_body.nil?
    end
  end
end

Instance Method Details

#_jsonapi_error_paramsObject



57
58
59
# File 'lib/jsonapi/hanami/rendering.rb', line 57

def _jsonapi_error_params
  @_jsonapi
end

#_jsonapi_error_statusObject



52
53
54
55
# File 'lib/jsonapi/hanami/rendering.rb', line 52

def _jsonapi_error_status
  # TODO(beauby): Set HTTP status code accordingly.
  400
end

#_jsonapi_errorsObject



61
62
63
64
# File 'lib/jsonapi/hanami/rendering.rb', line 61

def _jsonapi_errors
  # TODO(beauby): Implement inferrence for Hanami::Validations.
  @_jsonapi[:errors]
end

#_jsonapi_exposuresObject



39
40
41
# File 'lib/jsonapi/hanami/rendering.rb', line 39

def _jsonapi_exposures
  { routes: routes }.merge!(exposures)
end

#_jsonapi_paramsObject

NOTE(beauby): It might be worth factoring those methods out into a

class.


34
35
36
37
# File 'lib/jsonapi/hanami/rendering.rb', line 34

def _jsonapi_params
  # TODO(beauby): Inject global params (toplevel jsonapi, etc.).
  @_jsonapi.dup.merge!(expose: _jsonapi_exposures)
end

#_jsonapi_renderObject



17
18
19
20
21
22
23
# File 'lib/jsonapi/hanami/rendering.rb', line 17

def _jsonapi_render
  if @_jsonapi.key?(:errors)
    _jsonapi_render_error
  else
    _jsonapi_render_success
  end
end

#_jsonapi_render_errorObject



43
44
45
46
47
48
49
50
# File 'lib/jsonapi/hanami/rendering.rb', line 43

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

#_jsonapi_render_successObject



25
26
27
28
29
30
# File 'lib/jsonapi/hanami/rendering.rb', line 25

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