Module: JSONAPI::Utils::Response::Renders

Included in:
JSONAPI::Utils::Response
Defined in:
lib/jsonapi/utils/response/renders.rb

Instance Method Summary collapse

Instance Method Details

#jsonapi_render(json:, status: nil, options: {}) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/jsonapi/utils/response/renders.rb', line 5

def jsonapi_render(json:, status: nil, options: {})
  body = jsonapi_format(json, options)
  render json: body, status: status || @_response_document.status
rescue => e
  handle_exceptions(e)
ensure
  correct_media_type
end

#jsonapi_render_bad_requestObject



26
27
28
# File 'lib/jsonapi/utils/response/renders.rb', line 26

def jsonapi_render_bad_request
  jsonapi_render_errors(::JSONAPI::Utils::Exceptions::BadRequest.new)
end

#jsonapi_render_errors(exception = nil, json: nil, status: nil) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/jsonapi/utils/response/renders.rb', line 14

def jsonapi_render_errors(exception = nil, json: nil, status: nil)
  body   = jsonapi_format_errors(exception || json)
  status = status || body.try(:first).try(:[], :status)
  render json: { errors: body }, status: status
ensure
  correct_media_type
end

#jsonapi_render_internal_server_errorObject



22
23
24
# File 'lib/jsonapi/utils/response/renders.rb', line 22

def jsonapi_render_internal_server_error
  jsonapi_render_errors(::JSONAPI::Utils::Exceptions::InternalServerError.new)
end

#jsonapi_render_not_found(exception) ⇒ Object



30
31
32
33
# File 'lib/jsonapi/utils/response/renders.rb', line 30

def jsonapi_render_not_found(exception)
  id = exception.message.match(/=([\w-]+)/).try(:[], 1) || '(no identifier)'
  jsonapi_render_errors(JSONAPI::Exceptions::RecordNotFound.new(id))
end

#jsonapi_render_not_found_with_nullObject



35
36
37
# File 'lib/jsonapi/utils/response/renders.rb', line 35

def jsonapi_render_not_found_with_null
  render json: { data: nil }, status: 200
end