Module: JSONAPI::Utils
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/jsonapi/utils.rb,
lib/jsonapi/utils/version.rb,
lib/jsonapi/utils/exceptions.rb
Defined Under Namespace
Modules: Exceptions
Constant Summary collapse
- VERSION =
'0.3.0'
Instance Method Summary collapse
- #jsonapi_errors(exception) ⇒ Object
- #jsonapi_render(options) ⇒ Object
- #jsonapi_render_bad_request ⇒ Object
- #jsonapi_render_internal_server_error ⇒ Object
- #jsonapi_render_not_found ⇒ Object
- #jsonapi_render_not_found_with_null ⇒ Object
- #jsonapi_serialize(records, options = {}) ⇒ Object
Instance Method Details
#jsonapi_errors(exception) ⇒ Object
19 20 21 |
# File 'lib/jsonapi/utils.rb', line 19 def jsonapi_errors(exception) JSONAPI::ErrorsOperationResult.new(exception.errors[0].code, exception.errors).as_json end |
#jsonapi_render(options) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/jsonapi/utils.rb', line 12 def jsonapi_render() if .has_key?(:json) response = jsonapi_serialize([:json], [:options] || {}) render json: response, status: [:status] || :ok end end |
#jsonapi_render_bad_request ⇒ Object
28 29 30 31 |
# File 'lib/jsonapi/utils.rb', line 28 def jsonapi_render_bad_request errors = ::JSONAPI::Utils::Exceptions::BadRequest.new render json: jsonapi_errors(errors), status: 400 end |
#jsonapi_render_internal_server_error ⇒ Object
23 24 25 26 |
# File 'lib/jsonapi/utils.rb', line 23 def jsonapi_render_internal_server_error errors = ::JSONAPI::Utils::Exceptions::InternalServerError.new render json: jsonapi_errors(errors), status: 500 end |
#jsonapi_render_not_found ⇒ Object
33 34 35 36 37 |
# File 'lib/jsonapi/utils.rb', line 33 def jsonapi_render_not_found id = extract_ids(@request.params) exception = JSONAPI::Exceptions::RecordNotFound.new(id) render json: jsonapi_errors(exception), status: 404 end |
#jsonapi_render_not_found_with_null ⇒ Object
39 40 41 |
# File 'lib/jsonapi/utils.rb', line 39 def jsonapi_render_not_found_with_null render json: { data: nil }, status: 200 end |
#jsonapi_serialize(records, options = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/jsonapi/utils.rb', line 43 def jsonapi_serialize(records, = {}) results = JSONAPI::OperationResults.new (params, records) if records.respond_to?(:to_ary) records = fix_when_hash(records, ) if needs_to_be_fixed?(records) @resources = build_collection(records, ) results.add_result(JSONAPI::ResourcesOperationResult.new(:ok, @resources, ())) else @resource = turn_into_resource(records, ) results.add_result(JSONAPI::ResourceOperationResult.new(:ok, @resource)) end create_response_document(results).contents end |