jsonapi.rb 1.1.3 → 1.2.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 +5 -5
- data/Gemfile.lock +3 -2
- data/jsonapi.rb.gemspec +1 -0
- data/lib/jsonapi/active_model_error_serializer.rb +11 -3
- data/lib/jsonapi/errors.rb +4 -4
- data/lib/jsonapi/rails.rb +25 -13
- data/lib/jsonapi/version.rb +1 -1
- metadata +18 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7b246a81ea3e2cbc309f751a2c618e2bfc898a74
|
4
|
+
data.tar.gz: c3096701eecc967d3c125668d977baaf92fc8b40
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be7de562824d255df5ff03d6bee8dc7b9e4f89f6a6fb5c0bee0cbf149f15716ae3ad17225db0262021f9d43c512a89d4e60b6880d8390e897e4f1e7305daf41a
|
7
|
+
data.tar.gz: f227a7b8891cc6389682ed3ef478f229d8ba8375f206403e22b49fb596f3fb7e9da22d57cdf3f8a3dc8daf92706a5f7454e12cb115d04ab3c130b907920b4c29
|
data/Gemfile.lock
CHANGED
data/jsonapi.rb.gemspec
CHANGED
@@ -11,18 +11,26 @@ module JSONAPI
|
|
11
11
|
end
|
12
12
|
|
13
13
|
attribute :title do
|
14
|
-
|
14
|
+
Rack::Utils::HTTP_STATUS_CODES[422]
|
15
15
|
end
|
16
16
|
|
17
17
|
attribute :code do |object|
|
18
18
|
_, error_hash = object
|
19
|
-
error_hash[:error]
|
19
|
+
error_hash[:error] || error_hash[:message]
|
20
|
+
.to_s.delete("''").parameterize('_')
|
20
21
|
end
|
21
22
|
|
22
23
|
attribute :detail do |object, params|
|
23
24
|
error_key, error_hash = object
|
24
25
|
errors_object = params[:model].errors
|
25
|
-
|
26
|
+
|
27
|
+
# Rails 4 provides just the message.
|
28
|
+
if error_hash[:error].present?
|
29
|
+
message = errors_object.generate_message(error_key, error_hash[:error])
|
30
|
+
else
|
31
|
+
message = error_hash[:message]
|
32
|
+
end
|
33
|
+
|
26
34
|
errors_object.full_message(error_key, message)
|
27
35
|
end
|
28
36
|
|
data/lib/jsonapi/errors.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'rack/utils'
|
2
2
|
require 'active_support/concern'
|
3
3
|
|
4
4
|
module JSONAPI
|
@@ -11,7 +11,7 @@ module JSONAPI
|
|
11
11
|
|
12
12
|
included do
|
13
13
|
rescue_from StandardError do |exception|
|
14
|
-
error = { status: '500', title:
|
14
|
+
error = { status: '500', title: Rack::Utils::HTTP_STATUS_CODES[500] }
|
15
15
|
render jsonapi_errors: [error], status: :internal_server_error
|
16
16
|
end
|
17
17
|
|
@@ -19,7 +19,7 @@ module JSONAPI
|
|
19
19
|
ActiveRecord::RecordNotFound
|
20
20
|
].each do |exception_class|
|
21
21
|
rescue_from exception_class do |exception|
|
22
|
-
error = { status: '404', title:
|
22
|
+
error = { status: '404', title: Rack::Utils::HTTP_STATUS_CODES[404] }
|
23
23
|
render jsonapi_errors: [error], status: :not_found
|
24
24
|
end
|
25
25
|
end
|
@@ -36,7 +36,7 @@ module JSONAPI
|
|
36
36
|
|
37
37
|
error = {
|
38
38
|
status: '422',
|
39
|
-
title:
|
39
|
+
title: Rack::Utils::HTTP_STATUS_CODES[422],
|
40
40
|
source: source
|
41
41
|
}
|
42
42
|
|
data/lib/jsonapi/rails.rb
CHANGED
@@ -17,8 +17,7 @@ module JSONAPI
|
|
17
17
|
parser = ActionDispatch::Request.parameter_parsers[:json]
|
18
18
|
ActionDispatch::Request.parameter_parsers[:jsonapi] = parser
|
19
19
|
else
|
20
|
-
|
21
|
-
ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = parser
|
20
|
+
ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = :json
|
22
21
|
end
|
23
22
|
|
24
23
|
self.add_renderer!
|
@@ -32,17 +31,24 @@ module JSONAPI
|
|
32
31
|
ActionController::Renderers.add(:jsonapi_errors) do |resource, options|
|
33
32
|
self.content_type ||= Mime[:jsonapi]
|
34
33
|
|
35
|
-
|
34
|
+
many = JSONAPI::Rails.is_collection?(resource, options[:is_collection])
|
35
|
+
resource = [resource] unless many
|
36
36
|
|
37
37
|
return JSONAPI::ErrorSerializer.new(resource, options)
|
38
38
|
.serialized_json unless resource.is_a?(ActiveModel::Errors)
|
39
39
|
|
40
40
|
errors = []
|
41
|
-
model = resource.
|
42
|
-
model_serializer = JSONAPI::Rails.serializer_class(model)
|
41
|
+
model = resource.instance_variable_get('@base')
|
42
|
+
model_serializer = JSONAPI::Rails.serializer_class(model, false)
|
43
43
|
|
44
|
-
|
44
|
+
details = resource.messages
|
45
|
+
details = resource.details if resource.respond_to?(:details)
|
46
|
+
|
47
|
+
details.each do |error_key, error_hashes|
|
45
48
|
error_hashes.each do |error_hash|
|
49
|
+
# Rails 4 provides just the message.
|
50
|
+
error_hash = { message: error_hash } unless error_hash.is_a?(Hash)
|
51
|
+
|
46
52
|
errors << [ error_key, error_hash ]
|
47
53
|
end
|
48
54
|
end
|
@@ -67,7 +73,8 @@ module JSONAPI
|
|
67
73
|
)
|
68
74
|
|
69
75
|
# If it's an empty collection, return it directly.
|
70
|
-
|
76
|
+
many = JSONAPI::Rails.is_collection?(resource, options[:is_collection])
|
77
|
+
if many && !resource.any?
|
71
78
|
return options.slice(:meta, :links).merge(data: []).to_json
|
72
79
|
end
|
73
80
|
|
@@ -76,25 +83,30 @@ module JSONAPI
|
|
76
83
|
options[:include] ||= (
|
77
84
|
jsonapi_include if respond_to?(:jsonapi_include, true))
|
78
85
|
|
79
|
-
serializer_class = JSONAPI::Rails.serializer_class(resource)
|
86
|
+
serializer_class = JSONAPI::Rails.serializer_class(resource, many)
|
80
87
|
serializer_class.new(resource, options).serialized_json
|
81
88
|
end
|
82
89
|
end
|
83
90
|
|
84
91
|
# Checks if an object is a collection
|
85
92
|
#
|
86
|
-
#
|
93
|
+
# Stollen from [FastJsonapi::ObjectSerializer], instance method.
|
94
|
+
#
|
95
|
+
# @param resource [Object] to check
|
96
|
+
# @param force_is_collection [NilClass] flag to overwrite
|
87
97
|
# @return [TrueClass] upon success
|
88
|
-
def self.is_collection?(
|
89
|
-
|
98
|
+
def self.is_collection?(resource, force_is_collection = nil)
|
99
|
+
return force_is_collection unless force_is_collection.nil?
|
100
|
+
|
101
|
+
resource.respond_to?(:size) && !resource.respond_to?(:each_pair)
|
90
102
|
end
|
91
103
|
|
92
104
|
# Resolves resource serializer class
|
93
105
|
#
|
94
106
|
# @return [Class]
|
95
|
-
def self.serializer_class(resource)
|
107
|
+
def self.serializer_class(resource, is_collection)
|
96
108
|
klass = resource.class
|
97
|
-
klass = resource.first.class if
|
109
|
+
klass = resource.first.class if is_collection
|
98
110
|
|
99
111
|
"#{klass.name}Serializer".constantize
|
100
112
|
end
|
data/lib/jsonapi/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi.rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stas Suscov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-01-
|
11
|
+
date: 2019-01-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fast_jsonapi
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rack
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: bundler
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -224,7 +238,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
224
238
|
- !ruby/object:Gem::Version
|
225
239
|
version: '0'
|
226
240
|
requirements: []
|
227
|
-
|
241
|
+
rubyforge_project:
|
242
|
+
rubygems_version: 2.5.2.2
|
228
243
|
signing_key:
|
229
244
|
specification_version: 4
|
230
245
|
summary: So you say you need JSON:API support in your API...
|