jsonapi-realizer 4.0.1 → 4.1.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/README.md +121 -4
- data/lib/jsonapi/realizer.rb +1 -1
- data/lib/jsonapi/realizer/action.rb +1 -1
- data/lib/jsonapi/realizer/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8cb7f214558b3e9926ec20b9156c10d8e4b00bf29c1b8ef2bbade489a066ece2
|
4
|
+
data.tar.gz: 29abee993505d8728b4092542207541864baec6dfee5e8d5739996b6d58367d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ff7e820e1a2b5222afd5076e4822d31f706302501a1a020c9dd4ae5cb8c327d2cb741b3f687d2e376b876166d1aadd85bfab770542604f76e3ea4ab9d32f26f
|
7
|
+
data.tar.gz: cf2485faba6a11e199580c619aed6eb888be5a72cb2582ace2c3c29efda84fd6859c4ebc1cde89af841395e59594fa524e94ddf4c874154fd5da0d6a3cbbcbc9
|
data/README.md
CHANGED
@@ -63,7 +63,7 @@ class PhotosController < ApplicationController
|
|
63
63
|
|
64
64
|
ProcessPhotosService.new(realization.model)
|
65
65
|
|
66
|
-
render json: JSONAPI::Serializer.serialize(
|
66
|
+
render json: JSONAPI::Serializer.serialize(realization.model)
|
67
67
|
end
|
68
68
|
|
69
69
|
def index
|
@@ -128,6 +128,7 @@ end
|
|
128
128
|
``` ruby
|
129
129
|
class PhotosController < ApplicationController
|
130
130
|
def index
|
131
|
+
# See: pundit for `policy_scope()`
|
131
132
|
realization = JSONAPI::Realizer.index(
|
132
133
|
policy(Photo).sanitize(:index, params),
|
133
134
|
headers: request.headers,
|
@@ -135,9 +136,10 @@ class PhotosController < ApplicationController
|
|
135
136
|
relation: policy_scope(Photo)
|
136
137
|
)
|
137
138
|
|
138
|
-
# See: pundit for `policy_scope()`
|
139
139
|
# See: pundit for `authorize()`
|
140
|
-
|
140
|
+
authorize(realization.relation)
|
141
|
+
|
142
|
+
render json: JSONAPI::Serializer.serialize(realization.models, is_collection: true)
|
141
143
|
end
|
142
144
|
end
|
143
145
|
```
|
@@ -181,6 +183,121 @@ class PhotoRealizer
|
|
181
183
|
end
|
182
184
|
```
|
183
185
|
|
186
|
+
### rails and jsonapi-realizer and jsonapi-serializers and pundit
|
187
|
+
|
188
|
+
While this gem contains nothing specifically targeting Rails or pundit or [jsonapi-serializers](https://github.com/fotinakis/jsonapi-serializers) (a fantastic gem) I've already written some seamless integration code. This root controller will handle exceptions in a graceful way and also give you access to a clean interface for serializing:
|
189
|
+
|
190
|
+
``` ruby
|
191
|
+
module V1
|
192
|
+
class ApplicationController < ::ApplicationController
|
193
|
+
include Pundit
|
194
|
+
|
195
|
+
after_action :verify_authorized, except: :index
|
196
|
+
after_action :verify_policy_scoped, only: :index
|
197
|
+
|
198
|
+
rescue_from JSONAPI::Realizer::Error::MissingAcceptHeader, with: :missing_accept_header
|
199
|
+
rescue_from JSONAPI::Realizer::Error::InvalidAcceptHeader, with: :invalid_accept_header
|
200
|
+
rescue_from Pundit::NotAuthorizedError, with: :access_not_authorized
|
201
|
+
|
202
|
+
private def missing_accept_header
|
203
|
+
head :not_acceptable
|
204
|
+
end
|
205
|
+
|
206
|
+
private def invalid_accept_header
|
207
|
+
head :not_acceptable
|
208
|
+
end
|
209
|
+
|
210
|
+
private def access_not_authorized
|
211
|
+
head :unauthorized
|
212
|
+
end
|
213
|
+
|
214
|
+
private def pundit_user
|
215
|
+
current_account
|
216
|
+
end
|
217
|
+
|
218
|
+
private def serialize(realization)
|
219
|
+
JSONAPI::Serializer.serialize(
|
220
|
+
if realization.respond_to?(:models) then realization.models else realization.model end,
|
221
|
+
is_collection: realization.respond_to?(:models),
|
222
|
+
meta: serialized_metadata,
|
223
|
+
links: serialized_links,
|
224
|
+
jsonapi: serialized_jsonapi,
|
225
|
+
fields: serialized_fields(realization),
|
226
|
+
include: serialized_includes(realization),
|
227
|
+
namespace: ::V1
|
228
|
+
)
|
229
|
+
end
|
230
|
+
|
231
|
+
private def serialized_metadata
|
232
|
+
{
|
233
|
+
api: {
|
234
|
+
version: "1"
|
235
|
+
}
|
236
|
+
}
|
237
|
+
end
|
238
|
+
|
239
|
+
private def serialized_links
|
240
|
+
{
|
241
|
+
discovery: {
|
242
|
+
href: "/"
|
243
|
+
}
|
244
|
+
}
|
245
|
+
end
|
246
|
+
|
247
|
+
private def serialized_jsonapi
|
248
|
+
{
|
249
|
+
version: "1.0"
|
250
|
+
}
|
251
|
+
end
|
252
|
+
|
253
|
+
private def serialized_fields(realization)
|
254
|
+
realization.fields if realization.fields.any?
|
255
|
+
end
|
256
|
+
|
257
|
+
private def serialized_includes(realization)
|
258
|
+
realization.includes if realization.includes.any?
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
262
|
+
```
|
263
|
+
|
264
|
+
You can see this resource controller used below:
|
265
|
+
|
266
|
+
``` ruby
|
267
|
+
module V1
|
268
|
+
class AccountsController < ::V1::ApplicationController
|
269
|
+
def index
|
270
|
+
realization = JSONAPI::Realizer.index(
|
271
|
+
policy(Account).sanitize(:index, params),
|
272
|
+
headers: request.headers,
|
273
|
+
scope: policy_scope(Account),
|
274
|
+
type: :accounts
|
275
|
+
)
|
276
|
+
|
277
|
+
authorize realization.relation
|
278
|
+
|
279
|
+
render json: serialize(realization)
|
280
|
+
end
|
281
|
+
|
282
|
+
def create
|
283
|
+
realization = JSONAPI::Realizer.create(
|
284
|
+
policy(Account).sanitize(:create, params),
|
285
|
+
headers: request.headers,
|
286
|
+
scope: policy_scope(Account)
|
287
|
+
)
|
288
|
+
|
289
|
+
authorize realization.relation
|
290
|
+
|
291
|
+
render json: serialize(realization)
|
292
|
+
end
|
293
|
+
end
|
294
|
+
end
|
295
|
+
```
|
296
|
+
|
297
|
+
### jsonapi-home
|
298
|
+
|
299
|
+
I'm already using jsonapi-realizer and it's sister project jsonapi-serializers in a new gem of mine that allows services to be discoverable: [jsonapi-home](https://github.com/krainboltgreene/jsonapi-home).
|
300
|
+
|
184
301
|
### Notes
|
185
302
|
|
186
303
|
A successful JSON:API request can be annotated as:
|
@@ -206,7 +323,7 @@ BusinessLayer -> JSONAPIRequest -> (Record | Array<Record>)
|
|
206
323
|
|
207
324
|
Add this line to your application's Gemfile:
|
208
325
|
|
209
|
-
gem "jsonapi-realizer", "
|
326
|
+
gem "jsonapi-realizer", "4.1.0"
|
210
327
|
|
211
328
|
And then execute:
|
212
329
|
|
data/lib/jsonapi/realizer.rb
CHANGED
@@ -4,7 +4,7 @@ require "active_support/core_ext/enumerable"
|
|
4
4
|
require "active_support/core_ext/string"
|
5
5
|
|
6
6
|
module JSONAPI
|
7
|
-
MEDIA_TYPE = "application/vnd.api+json"
|
7
|
+
MEDIA_TYPE = "application/vnd.api+json" unless const_defined?("MEDIA_TYPE")
|
8
8
|
|
9
9
|
module Realizer
|
10
10
|
require_relative "realizer/version"
|