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:

  • json (ActiveRecord::Base, ActiveRecord::Relation, Hash, Array<Hash>)

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

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

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

  • options (Hash) (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)

  • source_resource: (JSONAPI::Resource)

    it tells the render that this response is from a related resource and the result should be interpreted as a related resources response

  • relationship_type: (String, Symbol)

    it tells that the render which relationship the data is from

  • 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:

  • (String)


33
34
35
36
37
38
39
40
# File 'lib/jsonapi/utils/response/renders.rb', line 33

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.



76
77
78
# File 'lib/jsonapi/utils/response/renders.rb', line 76

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:

  • error (ActiveRecord::Base or any object that responds to #errors) (defaults to: nil)

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

  • json (ActiveRecord::Base or any object that responds to #errors) (defaults to: nil)

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

  • status (Integer, String, Symbol) (defaults to: nil)

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

Returns:

  • (String)


58
59
60
61
62
63
64
# File 'lib/jsonapi/utils/response/renders.rb', line 58

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.



69
70
71
# File 'lib/jsonapi/utils/response/renders.rb', line 69

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.



83
84
85
86
# File 'lib/jsonapi/utils/response/renders.rb', line 83

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”.



91
92
93
# File 'lib/jsonapi/utils/response/renders.rb', line 91

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