jsonapi-serializers 0.14.0 → 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -0
- data/lib/jsonapi-serializers/serializer.rb +9 -0
- data/lib/jsonapi-serializers/version.rb +1 -1
- data/spec/serializer_spec.rb +8 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b8e2b1a0281f387fc8e6e3ee0c24747ada99067
|
4
|
+
data.tar.gz: 2ce4804f76c29485607f6738bee21dd00dac9db0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7d32a4701376557f93e848ee32c7a77f80ed1214a4de326a0c8dc85530f6211ed75476b060a8716162391928af36cb7ac6e551fbf29f99c40e3d5a305cf87f3
|
7
|
+
data.tar.gz: bcb13f91e195f15b052d055957e904bd4e4704365292bf36e3c9c76d88bcab74903752b9670fde309169e525ff64389a444e205fed80841fc270f9518c9e8aa7
|
data/README.md
CHANGED
@@ -18,6 +18,7 @@ This library is up-to-date with the finalized v1 JSON API spec.
|
|
18
18
|
* [Custom attributes](#custom-attributes)
|
19
19
|
* [More customizations](#more-customizations)
|
20
20
|
* [Base URL](#base-url)
|
21
|
+
* [Root jsonapi object](#root-jsonapi-object)
|
21
22
|
* [Root metadata](#root-metadata)
|
22
23
|
* [Root links](#root-links)
|
23
24
|
* [Root errors](#root-errors)
|
@@ -230,6 +231,12 @@ def base_url
|
|
230
231
|
end
|
231
232
|
```
|
232
233
|
```ruby
|
234
|
+
# Override this to provide a resource-object jsonapi object containing the version in use.
|
235
|
+
# http://jsonapi.org/format/#document-jsonapi-object
|
236
|
+
def jsonapi
|
237
|
+
end
|
238
|
+
```
|
239
|
+
```ruby
|
233
240
|
def self_link
|
234
241
|
"#{base_url}/#{type}/#{id}"
|
235
242
|
end
|
@@ -311,6 +318,14 @@ JSONAPI::Serializer.serialize(post, base_url: 'http://example.com')
|
|
311
318
|
|
312
319
|
Note: if you override `self_link` in your serializer and leave out `base_url`, it will not be included.
|
313
320
|
|
321
|
+
### Root 'jsonapi' object
|
322
|
+
|
323
|
+
You can pass a `jsonapi` argument to specify a [top-level "jsonapi" key](http://jsonapi.org/format/#document-jsonapi-object) containing the version of JSON:API in use:
|
324
|
+
|
325
|
+
```ruby
|
326
|
+
JSONAPI::Serializer.serialize(post, jsonapi: {version: '1.0'})
|
327
|
+
```
|
328
|
+
|
314
329
|
### Root metadata
|
315
330
|
|
316
331
|
You can pass a `meta` argument to specify top-level metadata:
|
@@ -62,6 +62,11 @@ module JSONAPI
|
|
62
62
|
attribute_name.to_s.underscore
|
63
63
|
end
|
64
64
|
|
65
|
+
# Override this to provide resource-object jsonapi object containing the version in use.
|
66
|
+
# http://jsonapi.org/format/#document-jsonapi-object
|
67
|
+
def jsonapi
|
68
|
+
end
|
69
|
+
|
65
70
|
# Override this to provide resource-object metadata.
|
66
71
|
# http://jsonapi.org/format/#document-structure-resource-objects
|
67
72
|
def meta
|
@@ -244,6 +249,7 @@ module JSONAPI
|
|
244
249
|
options[:context] = options.delete('context') || options[:context] || {}
|
245
250
|
options[:skip_collection_check] = options.delete('skip_collection_check') || options[:skip_collection_check] || false
|
246
251
|
options[:base_url] = options.delete('base_url') || options[:base_url]
|
252
|
+
options[:jsonapi] = options.delete('jsonapi') || options[:jsonapi]
|
247
253
|
options[:meta] = options.delete('meta') || options[:meta]
|
248
254
|
options[:links] = options.delete('links') || options[:links]
|
249
255
|
|
@@ -299,6 +305,7 @@ module JSONAPI
|
|
299
305
|
result = {
|
300
306
|
'data' => primary_data,
|
301
307
|
}
|
308
|
+
result['jsonapi'] = options[:jsonapi] if options[:jsonapi]
|
302
309
|
result['meta'] = options[:meta] if options[:meta]
|
303
310
|
result['links'] = options[:links] if options[:links]
|
304
311
|
result['errors'] = options[:errors] if options[:errors]
|
@@ -382,10 +389,12 @@ module JSONAPI
|
|
382
389
|
attributes = serializer.attributes
|
383
390
|
links = serializer.links
|
384
391
|
relationships = serializer.relationships
|
392
|
+
jsonapi = serializer.jsonapi
|
385
393
|
meta = serializer.meta
|
386
394
|
data['attributes'] = attributes if !attributes.empty?
|
387
395
|
data['links'] = links if !links.empty?
|
388
396
|
data['relationships'] = relationships if !relationships.empty?
|
397
|
+
data['jsonapi'] = jsonapi if !jsonapi.nil?
|
389
398
|
data['meta'] = meta if !meta.nil?
|
390
399
|
data
|
391
400
|
end
|
data/spec/serializer_spec.rb
CHANGED
@@ -518,6 +518,14 @@ describe JSONAPI::Serializer do
|
|
518
518
|
'data' => serialize_primary(post, {serializer: MyApp::PostSerializer}),
|
519
519
|
})
|
520
520
|
end
|
521
|
+
it 'can include a top level jsonapi node' do
|
522
|
+
post = create(:post)
|
523
|
+
jsonapi_version = {'version' => '1.0'}
|
524
|
+
expect(JSONAPI::Serializer.serialize(post, jsonapi: jsonapi_version)).to eq({
|
525
|
+
'jsonapi' => {'version' => '1.0'},
|
526
|
+
'data' => serialize_primary(post, {serializer: MyApp::PostSerializer}),
|
527
|
+
})
|
528
|
+
end
|
521
529
|
it 'can include a top level meta node' do
|
522
530
|
post = create(:post)
|
523
531
|
meta = {authors: ['Yehuda Katz', 'Steve Klabnik'], copyright: 'Copyright 2015 Example Corp.'}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-serializers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.15.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Fotinakis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|