jsonapi-utils 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/lib/jsonapi/utils.rb +29 -34
- data/lib/jsonapi/utils/exceptions.rb +40 -0
- data/lib/jsonapi/utils/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b973c25edc256609e9a73af0eb75610ccbdc73c
|
4
|
+
data.tar.gz: 2041dfe51b129f936b4bceae828c536f56b60884
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee52a5cc6aef9f83190b72ce202fca841dc0f394923f32f371a4703a9dca53b5a5067f6910ed5dba5d0ca30a6df057c0711f5ca8b9660ea692efd36c2d476c4a
|
7
|
+
data.tar.gz: d32f1cc5923ab3c916040089cdee77c7270b6d1c7c8916c9e90739540f67434880cdb49487679848a7d428f0070d241e309f4728444264f6f822cb353207c281
|
data/.gitignore
CHANGED
data/lib/jsonapi/utils.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'jsonapi/utils/version'
|
2
|
+
require 'jsonapi/utils/exceptions'
|
2
3
|
|
3
4
|
module JSONAPI
|
4
5
|
module Utils
|
@@ -10,60 +11,54 @@ module JSONAPI
|
|
10
11
|
|
11
12
|
def jsonapi_render(options)
|
12
13
|
if options.has_key?(:json)
|
13
|
-
response =
|
14
|
-
render json: response
|
14
|
+
response = jsonapi_serialize(options[:json], options[:options] || {})
|
15
|
+
render json: response, status: options[:status] || :ok
|
15
16
|
end
|
16
17
|
end
|
17
18
|
|
19
|
+
def jsonapi_errors(exception)
|
20
|
+
JSONAPI::ErrorsOperationResult.new(exception.errors[0].code, exception.errors).as_json
|
21
|
+
end
|
22
|
+
|
23
|
+
def jsonapi_render_internal_server_error
|
24
|
+
errors = ::JSONAPI::Utils::Exceptions::InternalServerError.new
|
25
|
+
render json: jsonapi_errors(errors), status: 500
|
26
|
+
end
|
27
|
+
|
28
|
+
def jsonapi_render_bad_request
|
29
|
+
errors = ::JSONAPI::Utils::Exceptions::BadRequest.new
|
30
|
+
render json: jsonapi_errors(errors), status: 400
|
31
|
+
end
|
32
|
+
|
18
33
|
def jsonapi_render_not_found
|
19
|
-
|
34
|
+
id = extract_ids(@request.params)
|
35
|
+
exception = JSONAPI::Exceptions::RecordNotFound.new(id)
|
36
|
+
render json: jsonapi_errors(exception), status: 404
|
20
37
|
end
|
21
38
|
|
22
39
|
def jsonapi_render_not_found_with_null
|
23
|
-
|
40
|
+
render json: { data: nil }, status: 200
|
24
41
|
end
|
25
42
|
|
26
43
|
def jsonapi_serialize(records, options = {})
|
27
|
-
return build_nil if records.nil? && options[:allow_null]
|
28
44
|
results = JSONAPI::OperationResults.new
|
29
45
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
46
|
+
fix_request_options(params, records)
|
47
|
+
|
48
|
+
if records.respond_to?(:to_ary)
|
49
|
+
records = fix_when_hash(records, options) if needs_to_be_fixed?(records)
|
50
|
+
@resources = build_collection(records, options)
|
51
|
+
results.add_result(JSONAPI::ResourcesOperationResult.new(:ok, @resources, result_options(options)))
|
34
52
|
else
|
35
|
-
|
36
|
-
|
37
|
-
if records.respond_to?(:to_ary)
|
38
|
-
records = fix_when_hash(records, options) if needs_to_be_fixed?(records)
|
39
|
-
@resources = build_collection(records, options)
|
40
|
-
results.add_result(JSONAPI::ResourcesOperationResult.new(:ok, @resources, result_options(options)))
|
41
|
-
else
|
42
|
-
@resource = turn_into_resource(records, options)
|
43
|
-
results.add_result(JSONAPI::ResourceOperationResult.new(:ok, @resource))
|
44
|
-
end
|
53
|
+
@resource = turn_into_resource(records, options)
|
54
|
+
results.add_result(JSONAPI::ResourceOperationResult.new(:ok, @resource))
|
45
55
|
end
|
46
56
|
|
47
57
|
create_response_document(results).contents
|
48
58
|
end
|
49
59
|
|
50
|
-
def jsonapi_error(exception)
|
51
|
-
JSONAPI::ErrorsOperationResult.new(exception.errors[0].code, exception.errors).as_json
|
52
|
-
end
|
53
|
-
|
54
60
|
private
|
55
61
|
|
56
|
-
def build_response(options)
|
57
|
-
{
|
58
|
-
body: jsonapi_serialize(options[:json], options[:options] || {}),
|
59
|
-
status: options[:json].nil? && !options[:allow_null] ? :not_found : :ok
|
60
|
-
}
|
61
|
-
end
|
62
|
-
|
63
|
-
def build_nil
|
64
|
-
{ data: nil }
|
65
|
-
end
|
66
|
-
|
67
62
|
def extract_ids(hash)
|
68
63
|
ids = hash.keys.select { |e| e =~ /id$/i }.map { |e| hash[e] }
|
69
64
|
ids.first rescue '(id not identified)'
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'jsonapi/utils/version'
|
2
|
+
|
3
|
+
module JSONAPI
|
4
|
+
module Utils
|
5
|
+
module Exceptions
|
6
|
+
class BadRequest < ::JSONAPI::Exceptions::Error
|
7
|
+
def code; 400 end
|
8
|
+
|
9
|
+
def errors
|
10
|
+
[JSONAPI::Error.new(code: 400,
|
11
|
+
status: :bad_request,
|
12
|
+
title: 'Bad Request',
|
13
|
+
detail: 'This request is not supported.')]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class NotFoundError < ::JSONAPI::Exceptions::Error
|
18
|
+
def code; 404 end
|
19
|
+
|
20
|
+
def errors
|
21
|
+
[JSONAPI::Error.new(code: 404,
|
22
|
+
status: :not_found,
|
23
|
+
title: 'Not Found',
|
24
|
+
detail: 'The requested resource was not found.')]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class InternalServerError < ::JSONAPI::Exceptions::Error
|
29
|
+
def code; 500 end
|
30
|
+
|
31
|
+
def errors
|
32
|
+
[JSONAPI::Error.new(code: 500,
|
33
|
+
status: :internal_server_error,
|
34
|
+
title: 'Internal Server Error',
|
35
|
+
detail: 'An internal error ocurred while processing the request.')]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tiago Guedes
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-10-
|
12
|
+
date: 2015-10-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -73,6 +73,7 @@ files:
|
|
73
73
|
- bin/setup
|
74
74
|
- jsonapi-utils.gemspec
|
75
75
|
- lib/jsonapi/utils.rb
|
76
|
+
- lib/jsonapi/utils/exceptions.rb
|
76
77
|
- lib/jsonapi/utils/version.rb
|
77
78
|
homepage: https://github.com/b2beauty/jsonapi-utils
|
78
79
|
licenses:
|