Module: JSONAPI::Utils::Response::Renders
- Included in:
- JSONAPI::Utils::Response
- Defined in:
- lib/jsonapi/utils/response/renders.rb
Instance Method Summary collapse
-
#jsonapi_render(json:, status: nil, options: {}) ⇒ String
Helper method to render JSON API-compliant responses.
-
#jsonapi_render_bad_request ⇒ Object
Helper method to render HTTP 400 Bad Request.
-
#jsonapi_render_errors(error = nil, json: nil, status: nil) ⇒ String
Helper method to render JSON API-compliant error responses.
-
#jsonapi_render_internal_server_error ⇒ Object
Helper method to render HTTP 500 Interval Server Error.
-
#jsonapi_render_not_found(exception) ⇒ Object
Helper method to render HTTP 404 Bad Request.
-
#jsonapi_render_not_found_with_null ⇒ Object
Helper method to render HTTP 404 Bad Request with null “data”.
Instance Method Details
#jsonapi_render(json:, status: nil, options: {}) ⇒ String
Helper method to render JSON API-compliant responses.
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, ) 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_request ⇒ Object
Helper method to render HTTP 400 Bad Request.
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.
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_error ⇒ Object
Helper method to render HTTP 500 Interval Server Error.
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.
78 79 80 81 |
# File 'lib/jsonapi/utils/response/renders.rb', line 78 def jsonapi_render_not_found(exception) id = exception. =~ /=([\w-]+)/ && $1 || '(no identifier)' jsonapi_render_errors(JSONAPI::Exceptions::RecordNotFound.new(id)) end |
#jsonapi_render_not_found_with_null ⇒ Object
Helper method to render HTTP 404 Bad Request with null “data”.
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 |