Module: JSONAPI::Utils
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/jsonapi/utils.rb,
lib/jsonapi/utils/version.rb
Defined Under Namespace
Modules: Exceptions
Constant Summary collapse
- VERSION =
'0.4.1'
Instance Method Summary collapse
- #jsonapi_format_errors(exception) ⇒ Object
- #jsonapi_render(options) ⇒ Object
- #jsonapi_render_bad_request ⇒ Object
- #jsonapi_render_errors(exception) ⇒ 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_format_errors(exception) ⇒ Object
32 33 34 |
# File 'lib/jsonapi/utils.rb', line 32 def jsonapi_format_errors(exception) JSONAPI::ErrorsOperationResult.new(exception.errors[0].code, exception.errors) end |
#jsonapi_render(options) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/jsonapi/utils.rb', line 13 def jsonapi_render() if .has_key?(:json) response = jsonapi_serialize([:json], [:options] || {}) render json: response, status: [:status] || :ok else raise ArgumentError.new('":json" key must be set to JSONAPI::Utils#jsonapi_render') end rescue => e raise e unless e.class.name.starts_with?('JSONAPI::Exceptions') handle_exceptions(e) ensure headers['Content-Type'] = JSONAPI::MEDIA_TYPE end |
#jsonapi_render_bad_request ⇒ Object
40 41 42 |
# File 'lib/jsonapi/utils.rb', line 40 def jsonapi_render_bad_request jsonapi_render_errors(::JSONAPI::Utils::Exceptions::BadRequest.new) end |
#jsonapi_render_errors(exception) ⇒ Object
27 28 29 30 |
# File 'lib/jsonapi/utils.rb', line 27 def jsonapi_render_errors(exception) error = jsonapi_format_errors(exception) render json: { errors: error.errors }, status: error.code end |
#jsonapi_render_internal_server_error ⇒ Object
36 37 38 |
# File 'lib/jsonapi/utils.rb', line 36 def jsonapi_render_internal_server_error jsonapi_render_errors(::JSONAPI::Utils::Exceptions::InternalServerError.new) end |
#jsonapi_render_not_found ⇒ Object
44 45 46 47 48 |
# File 'lib/jsonapi/utils.rb', line 44 def jsonapi_render_not_found setup_request id = extract_ids(@request.params) jsonapi_render_errors(JSONAPI::Exceptions::RecordNotFound.new(id)) end |
#jsonapi_render_not_found_with_null ⇒ Object
50 51 52 |
# File 'lib/jsonapi/utils.rb', line 50 def jsonapi_render_not_found_with_null render json: { data: nil }, status: 200 end |
#jsonapi_serialize(records, options = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/jsonapi/utils.rb', line 54 def jsonapi_serialize(records, = {}) setup_request 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 |