module JSONAPI::Hanami::Rendering

Public Class Methods

included(base) click to toggle source
# 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

Public Instance Methods

_jsonapi_error_params() click to toggle source
# File lib/jsonapi/hanami/rendering.rb, line 57
def _jsonapi_error_params
  @_jsonapi
end
_jsonapi_error_status() click to toggle source
# File lib/jsonapi/hanami/rendering.rb, line 52
def _jsonapi_error_status
  # TODO(beauby): Set HTTP status code accordingly.
  400
end
_jsonapi_errors() click to toggle source
# File lib/jsonapi/hanami/rendering.rb, line 61
def _jsonapi_errors
  # TODO(beauby): Implement inference for Hanami::Validations.
  @_jsonapi[:errors]
end
_jsonapi_exposures() click to toggle source
# File lib/jsonapi/hanami/rendering.rb, line 39
def _jsonapi_exposures
  { routes: routes }.merge!(exposures)
end
_jsonapi_params() click to toggle source

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

class.
# 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_render() click to toggle source
# 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_error() click to toggle source
# 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_success() click to toggle source
# 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