diff --git a/.gitignore b/.gitignore index ba564ee..db9fde9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,8 @@ /coverage/ /doc/ /pkg/ +/spec/log/ +/spec/test_db /spec/reports/ /tmp/ *.bundle diff --git a/lib/jsonapi/utils.rb b/lib/jsonapi/utils.rb index 7cf640a..81ee2a0 100644 --- a/lib/jsonapi/utils.rb +++ b/lib/jsonapi/utils.rb @@ -1,4 +1,5 @@ require 'jsonapi/utils/version' +require 'jsonapi/utils/exceptions' module JSONAPI module Utils @@ -10,60 +11,54 @@ module Utils def jsonapi_render(options) if options.has_key?(:json) - response = build_response(options) - render json: response[:body], status: options[:status] || response[:status] || :ok + response = jsonapi_serialize(options[:json], options[:options] || {}) + render json: response, status: options[:status] || :ok end end + def jsonapi_errors(exception) + JSONAPI::ErrorsOperationResult.new(exception.errors[0].code, exception.errors).as_json + end + + def jsonapi_render_internal_server_error + errors = ::JSONAPI::Utils::Exceptions::InternalServerError.new + render json: jsonapi_errors(errors), status: 500 + end + + def jsonapi_render_bad_request + errors = ::JSONAPI::Utils::Exceptions::BadRequest.new + render json: jsonapi_errors(errors), status: 400 + end + def jsonapi_render_not_found - jsonapi_render json: nil + id = extract_ids(@request.params) + exception = JSONAPI::Exceptions::RecordNotFound.new(id) + render json: jsonapi_errors(exception), status: 404 end def jsonapi_render_not_found_with_null - jsonapi_render json: nil, options: { allow_null: true } + render json: { data: nil }, status: 200 end def jsonapi_serialize(records, options = {}) - return build_nil if records.nil? && options[:allow_null] results = JSONAPI::OperationResults.new - if records.nil? - id = extract_ids(@request.params) - record_not_found = JSONAPI::Exceptions::RecordNotFound.new(id) - results.add_result(JSONAPI::ErrorsOperationResult.new(record_not_found.errors[0].code, record_not_found.errors)) + fix_request_options(params, records) + + if records.respond_to?(:to_ary) + records = fix_when_hash(records, options) if needs_to_be_fixed?(records) + @resources = build_collection(records, options) + results.add_result(JSONAPI::ResourcesOperationResult.new(:ok, @resources, result_options(options))) else - fix_request_options(params, records) - - if records.respond_to?(:to_ary) - records = fix_when_hash(records, options) if needs_to_be_fixed?(records) - @resources = build_collection(records, options) - results.add_result(JSONAPI::ResourcesOperationResult.new(:ok, @resources, result_options(options))) - else - @resource = turn_into_resource(records, options) - results.add_result(JSONAPI::ResourceOperationResult.new(:ok, @resource)) - end + @resource = turn_into_resource(records, options) + results.add_result(JSONAPI::ResourceOperationResult.new(:ok, @resource)) end create_response_document(results).contents end - def jsonapi_error(exception) - JSONAPI::ErrorsOperationResult.new(exception.errors[0].code, exception.errors).as_json - end - private - def build_response(options) - { - body: jsonapi_serialize(options[:json], options[:options] || {}), - status: options[:json].nil? && !options[:allow_null] ? :not_found : :ok - } - end - - def build_nil - { data: nil } - end - def extract_ids(hash) ids = hash.keys.select { |e| e =~ /id$/i }.map { |e| hash[e] } ids.first rescue '(id not identified)' diff --git a/lib/jsonapi/utils/exceptions.rb b/lib/jsonapi/utils/exceptions.rb new file mode 100644 index 0000000..5edd7ba --- /dev/null +++ b/lib/jsonapi/utils/exceptions.rb @@ -0,0 +1,40 @@ +require 'jsonapi/utils/version' + +module JSONAPI + module Utils + module Exceptions + class BadRequest < ::JSONAPI::Exceptions::Error + def code; 400 end + + def errors + [JSONAPI::Error.new(code: 400, + status: :bad_request, + title: 'Bad Request', + detail: 'This request is not supported.')] + end + end + + class NotFoundError < ::JSONAPI::Exceptions::Error + def code; 404 end + + def errors + [JSONAPI::Error.new(code: 404, + status: :not_found, + title: 'Not Found', + detail: 'The requested resource was not found.')] + end + end + + class InternalServerError < ::JSONAPI::Exceptions::Error + def code; 500 end + + def errors + [JSONAPI::Error.new(code: 500, + status: :internal_server_error, + title: 'Internal Server Error', + detail: 'An internal error ocurred while processing the request.')] + end + end + end + end +end diff --git a/lib/jsonapi/utils/version.rb b/lib/jsonapi/utils/version.rb index e97839b..b2f499a 100644 --- a/lib/jsonapi/utils/version.rb +++ b/lib/jsonapi/utils/version.rb @@ -1,5 +1,5 @@ module JSONAPI module Utils - VERSION = '0.2.2' + VERSION = '0.3.0' end end