Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/coverage/
/doc/
/pkg/
/spec/log/
/spec/test_db
/spec/reports/
/tmp/
*.bundle
Expand Down
63 changes: 29 additions & 34 deletions lib/jsonapi/utils.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'jsonapi/utils/version'
require 'jsonapi/utils/exceptions'

module JSONAPI
module Utils
Expand All @@ -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)'
Expand Down
40 changes: 40 additions & 0 deletions lib/jsonapi/utils/exceptions.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/jsonapi/utils/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module JSONAPI
module Utils
VERSION = '0.2.2'
VERSION = '0.3.0'
end
end