jsonapi.rb 1.3.3 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 28e026528b72f7e8474b8ac1c7dd868aa4dc31c2
4
- data.tar.gz: bef3b693f87d95ea19dee1ea5af22899585153ca
3
+ metadata.gz: b5fdba9a2957365a5a46bcfa292bb65e4f7067ca
4
+ data.tar.gz: 4b2cd03a3045fd835f8f3461e1da9a6878fdc593
5
5
  SHA512:
6
- metadata.gz: bc20a859dfdccfe05188360736c38b5575de904e26b7647218b27f511fc978c6ce6e546698734ba2add7c7dbfe009d32d12d192990e71122c164789be11079c4
7
- data.tar.gz: 4b5e1a141b28cacb86e08c3276791e67cc297803b5de4632c22e5c5e89ad36812810bcb32e396d6c8e04196d1f8299184919444be125734519b6f9fc006107fa
6
+ metadata.gz: 163a80a360a5b29b4703f8086796ceaa08043c0bd44e13b7e0c89de706d7865dcfbb482e1635a05f4bc711bf8acc2155f7722c19b3aae1aa5f23e13f2828bebf
7
+ data.tar.gz: 34a34bc75f47186a076f7a1e1707b73c73cb498320f590010913eb107d2f95d1cde79720ce34d48f9bca1dc0c0eba9067a17ce353f149bc3eaccefbf5c57f741
data/Gemfile.lock CHANGED
@@ -8,7 +8,7 @@ GIT
8
8
  PATH
9
9
  remote: .
10
10
  specs:
11
- jsonapi.rb (1.3.3)
11
+ jsonapi.rb (1.4.0)
12
12
  fast_jsonapi (~> 1.5)
13
13
  rack
14
14
  ransack
data/README.md CHANGED
@@ -92,6 +92,25 @@ Please follow the
92
92
  [Fast JSON API guide](https://github.com/Netflix/fast_jsonapi#serializer-definition)
93
93
  on how to define a serializer.
94
94
 
95
+ To provide a different naming scheme implement the `jsonapi_serializer_class`
96
+ method in your resource or application controller.
97
+
98
+ Here's an example:
99
+ ```ruby
100
+ class CustomNamingController < ActionController::Base
101
+
102
+ # ...
103
+
104
+ private
105
+
106
+ def jsonapi_serializer_class(resource, is_collection)
107
+ JSONAPI::Rails.serializer_class(resource, is_collection)
108
+ rescue NameError
109
+ # your serializer class naming implementation
110
+ end
111
+ end
112
+ ```
113
+
95
114
  #### Collection Meta
96
115
 
97
116
  To provide meta information for a collection, provide the `jsonapi_meta`
@@ -121,6 +140,9 @@ when a record is not found.
121
140
 
122
141
  To render the validation errors, just pass it to the error renderer.
123
142
 
143
+ To use an exception notifier, overwrite the
144
+ `render_jsonapi_internal_server_error` method in your controller.
145
+
124
146
  Here's an example:
125
147
 
126
148
  ```ruby
@@ -136,6 +158,14 @@ class MyController < ActionController::Base
136
158
  render jsonapi_errors: record.errors, status: :unprocessable_entity
137
159
  end
138
160
  end
161
+
162
+ private
163
+
164
+ def render_jsonapi_internal_server_error(exception)
165
+ # Call your exception notifier here. Example:
166
+ # Raven.capture_exception(exception)
167
+ super(exception)
168
+ end
139
169
  end
140
170
  ```
141
171
 
@@ -10,39 +10,52 @@ module JSONAPI
10
10
  extend ActiveSupport::Concern
11
11
 
12
12
  included do
13
- rescue_from StandardError do |exception|
14
- error = { status: '500', title: Rack::Utils::HTTP_STATUS_CODES[500] }
15
- render jsonapi_errors: [error], status: :internal_server_error
16
- end
13
+ rescue_from StandardError, with: :render_jsonapi_internal_server_error
14
+ rescue_from ActiveRecord::RecordNotFound, with: :render_jsonapi_not_found
15
+ rescue_from(
16
+ ActionController::ParameterMissing,
17
+ with: :render_jsonapi_unprocessable_entity
18
+ )
19
+ end
17
20
 
18
- [
19
- ActiveRecord::RecordNotFound
20
- ].each do |exception_class|
21
- rescue_from exception_class do |exception|
22
- error = { status: '404', title: Rack::Utils::HTTP_STATUS_CODES[404] }
23
- render jsonapi_errors: [error], status: :not_found
24
- end
25
- end
21
+ private
26
22
 
27
- [
28
- ActionController::ParameterMissing
29
- ].each do |exception_class|
30
- rescue_from exception_class do |exception|
31
- source = { pointer: '' }
23
+ # Generic error handler callback
24
+ #
25
+ # @param exception [Exception] instance to handle
26
+ # @return [String] JSONAPI error response
27
+ def render_jsonapi_internal_server_error(exception)
28
+ error = { status: '500', title: Rack::Utils::HTTP_STATUS_CODES[500] }
29
+ render jsonapi_errors: [error], status: :internal_server_error
30
+ end
32
31
 
33
- if !%w{data attributes relationships}.include?(exception.param.to_s)
34
- source[:pointer] = "/data/attributes/#{exception.param}"
35
- end
32
+ # Not found (404) error handler callback
33
+ #
34
+ # @param exception [Exception] instance to handle
35
+ # @return [String] JSONAPI error response
36
+ def render_jsonapi_not_found(exception)
37
+ error = { status: '404', title: Rack::Utils::HTTP_STATUS_CODES[404] }
38
+ render jsonapi_errors: [error], status: :not_found
39
+ end
36
40
 
37
- error = {
38
- status: '422',
39
- title: Rack::Utils::HTTP_STATUS_CODES[422],
40
- source: source
41
- }
41
+ # Unprocessable entity (422) error handler callback
42
+ #
43
+ # @param exception [Exception] instance to handle
44
+ # @return [String] JSONAPI error response
45
+ def render_jsonapi_unprocessable_entity(exception)
46
+ source = { pointer: '' }
42
47
 
43
- render jsonapi_errors: [error], status: :unprocessable_entity
44
- end
48
+ if !%w{data attributes relationships}.include?(exception.param.to_s)
49
+ source[:pointer] = "/data/attributes/#{exception.param}"
45
50
  end
51
+
52
+ error = {
53
+ status: '422',
54
+ title: Rack::Utils::HTTP_STATUS_CODES[422],
55
+ source: source
56
+ }
57
+
58
+ render jsonapi_errors: [error], status: :unprocessable_entity
46
59
  end
47
60
  end
48
61
  end
data/lib/jsonapi/rails.rb CHANGED
@@ -39,7 +39,12 @@ module JSONAPI
39
39
 
40
40
  errors = []
41
41
  model = resource.instance_variable_get('@base')
42
- model_serializer = JSONAPI::Rails.serializer_class(model, false)
42
+
43
+ if respond_to?(:jsonapi_serializer_class, true)
44
+ model_serializer = jsonapi_serializer_class(resource, false)
45
+ else
46
+ model_serializer = JSONAPI::Rails.serializer_class(model, false)
47
+ end
43
48
 
44
49
  details = resource.messages
45
50
  details = resource.details if resource.respond_to?(:details)
@@ -83,7 +88,12 @@ module JSONAPI
83
88
  options[:include] ||= (
84
89
  jsonapi_include if respond_to?(:jsonapi_include, true))
85
90
 
86
- serializer_class = JSONAPI::Rails.serializer_class(resource, many)
91
+ if respond_to?(:jsonapi_serializer_class, true)
92
+ serializer_class = jsonapi_serializer_class(resource, many)
93
+ else
94
+ serializer_class = JSONAPI::Rails.serializer_class(resource, many)
95
+ end
96
+
87
97
  serializer_class.new(resource, options).serialized_json
88
98
  end
89
99
  end
@@ -1,3 +1,3 @@
1
1
  module JSONAPI
2
- VERSION = '1.3.3'
2
+ VERSION = '1.4.0'
3
3
  end
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.3.3
4
+ version: 1.4.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-30 00:00:00.000000000 Z
11
+ date: 2019-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fast_jsonapi