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: {}) ⇒ String

Helper method to render JSON API-compliant responses.

Parameters:

  • Object to be serialized into JSON e.g.: User.first, User.all, { data: { id: 1, first_name: ‘Tiago’ } },

    { data: { id: 1, first_name: ‘Tiago’ } }
  • (defaults to: nil)

    HTTP status code e.g.: 201, ‘201’, :created

  • (defaults to: {})

    a customizable set of options

Options Hash (options:):

  • resource: (JSONAPI::Resource)

    it tells the render which resource class to be used rather than use an infered one (default behaviour)

  • model: (ActiveRecord::Base)

    ActiveRecord model class to be instantiated when a Hash or Array of Hashes is passed to the “json” key argument

  • count: (Integer)

    if it’s rendering a collection of resources, the default gem’s counting method can be bypassed by the use of this options. It’s shows then the total records resulting from that request and also calculates the pagination.

Returns:

API:

  • public



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

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) # http://bit.ly/2sEEGTN
ensure
  correct_media_type
end

#jsonapi_render_bad_requestObject

Helper method to render HTTP 400 Bad Request.

API:

  • public



71
72
73
# File 'lib/jsonapi/utils/response/renders.rb', line 71

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

#jsonapi_render_errors(error = nil, json: nil, status: nil) ⇒ String

Helper method to render JSON API-compliant error responses.

Parameters:

  • (defaults to: nil)

    Error object to be serialized into JSON e.g.: User.new(name: nil).tap(&:save), MyErrorDecorator.new(invalid_object)

  • (defaults to: nil)

    Error object to be serialized into JSON e.g.: User.new(name: nil).tap(&:save), MyErrorDecorator.new(invalid_object)

  • (defaults to: nil)

    HTTP status code e.g.: 422, ‘422’, :unprocessable_entity

Returns:

API:

  • public



53
54
55
56
57
58
59
# File 'lib/jsonapi/utils/response/renders.rb', line 53

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

#jsonapi_render_internal_server_errorObject

Helper method to render HTTP 500 Interval Server Error.

API:

  • public



64
65
66
# File 'lib/jsonapi/utils/response/renders.rb', line 64

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

#jsonapi_render_not_found(exception) ⇒ Object

Helper method to render HTTP 404 Bad Request.

API:

  • public



78
79
80
81
# File 'lib/jsonapi/utils/response/renders.rb', line 78

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

#jsonapi_render_not_found_with_nullObject

Helper method to render HTTP 404 Bad Request with null “data”.

API:

  • public



86
87
88
# File 'lib/jsonapi/utils/response/renders.rb', line 86

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