media_types-serialization 1.3.4 → 1.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +11 -12
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +6 -6
- data/README.md +140 -4
- data/lib/media_types/serialization/version.rb +1 -1
- data/media_types-serialization.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f668e10a02f800afc30da2c1438f99e4a50fb2b726cc829d490998f2f1997933
|
4
|
+
data.tar.gz: f0d711b09c45a03c7cfbac9238ac6f1e6f6f24b701d631f236b1986f19d80ca2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f6e2dec7c6409279f226dc0c6b3dff609273bd72330f055397c0e2f7be54eab5b032d33192590486f898d9cca3f8cedd69c6c1a519e104a56942a40ae4b2565
|
7
|
+
data.tar.gz: bda722326eca550e19cc8ab656896423f092a8b8c14436a37b4e51c88f0c5191b85a3b682dc3ead1c9e38b277e5a233c4741eb6cddeff5af68b89c3ed0a22230
|
data/.github/workflows/ci.yml
CHANGED
@@ -13,21 +13,20 @@ on:
|
|
13
13
|
|
14
14
|
jobs:
|
15
15
|
build:
|
16
|
-
|
17
16
|
runs-on: ubuntu-latest
|
18
17
|
|
19
18
|
strategy:
|
20
19
|
matrix:
|
21
|
-
ruby-version: [2.7.x, 2.6.x]
|
20
|
+
ruby-version: [2.7.x, 2.6.x, 2.5.x]
|
22
21
|
|
23
22
|
steps:
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
23
|
+
- uses: actions/checkout@v1
|
24
|
+
- name: Set up Ruby ${{ matrix.ruby-version }}
|
25
|
+
uses: actions/setup-ruby@v1
|
26
|
+
with:
|
27
|
+
ruby-version: ${{ matrix.ruby-version }}
|
28
|
+
- name: Build and test with Rake
|
29
|
+
run: |
|
30
|
+
gem install bundler
|
31
|
+
bundle install --jobs 4 --retry 3
|
32
|
+
bundle exec rake
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
media_types-serialization (1.3.
|
4
|
+
media_types-serialization (1.3.5)
|
5
5
|
actionpack (>= 4.0.0)
|
6
6
|
activesupport (>= 4.0.0)
|
7
|
-
media_types (>= 2.1.
|
7
|
+
media_types (>= 2.1.1, < 3.0.0)
|
8
8
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
@@ -66,7 +66,7 @@ GEM
|
|
66
66
|
mail (2.7.1)
|
67
67
|
mini_mime (>= 0.1.1)
|
68
68
|
marcel (1.0.1)
|
69
|
-
media_types (2.1.
|
69
|
+
media_types (2.1.1)
|
70
70
|
method_source (1.0.0)
|
71
71
|
mini_mime (1.1.0)
|
72
72
|
minitest (5.14.4)
|
@@ -75,7 +75,7 @@ GEM
|
|
75
75
|
racc (~> 1.4)
|
76
76
|
nokogiri (1.12.3-x86_64-linux)
|
77
77
|
racc (~> 1.4)
|
78
|
-
oj (3.13.
|
78
|
+
oj (3.13.2)
|
79
79
|
racc (1.5.2)
|
80
80
|
rack (2.2.3)
|
81
81
|
rack-test (1.1.0)
|
@@ -96,7 +96,7 @@ GEM
|
|
96
96
|
rails-dom-testing (2.0.3)
|
97
97
|
activesupport (>= 4.2.0)
|
98
98
|
nokogiri (>= 1.6)
|
99
|
-
rails-html-sanitizer (1.
|
99
|
+
rails-html-sanitizer (1.4.1)
|
100
100
|
loofah (~> 2.3)
|
101
101
|
railties (5.2.6)
|
102
102
|
actionpack (= 5.2.6)
|
@@ -134,4 +134,4 @@ DEPENDENCIES
|
|
134
134
|
rake (~> 13.0)
|
135
135
|
|
136
136
|
BUNDLED WITH
|
137
|
-
2.2.
|
137
|
+
2.2.17
|
data/README.md
CHANGED
@@ -224,7 +224,7 @@ There are convenience methods for serializing arrays of objects based on a templ
|
|
224
224
|
#### Indexes
|
225
225
|
|
226
226
|
An index is a collection of urls that point to members of the array.
|
227
|
-
The index method automatically generates it based on the self links defined in the default view of the
|
227
|
+
The index method automatically generates it based on the `self` links defined in the default view (`view: nil`) of the given version.
|
228
228
|
|
229
229
|
```ruby
|
230
230
|
class BookSerializer < MediaTypes::Serialization::Base
|
@@ -264,6 +264,88 @@ BookSerializer.serialize([book], BookValidator.view(:index).version(3), context:
|
|
264
264
|
# }
|
265
265
|
```
|
266
266
|
|
267
|
+
##### How to validate?
|
268
|
+
|
269
|
+
The `index` dsl does _not_ exist in the validation gem. This is how you validate indices:
|
270
|
+
|
271
|
+
```ruby
|
272
|
+
class BookValidator
|
273
|
+
include MediaTypes::Dsl
|
274
|
+
|
275
|
+
def self.organisation
|
276
|
+
'acme'
|
277
|
+
end
|
278
|
+
|
279
|
+
use_name 'book'
|
280
|
+
|
281
|
+
validations do
|
282
|
+
view :index do
|
283
|
+
version 3 do
|
284
|
+
attribute :books do
|
285
|
+
link :self
|
286
|
+
|
287
|
+
collection :_index, allow_empty: true do
|
288
|
+
attribute :href, String
|
289
|
+
end
|
290
|
+
end
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|
294
|
+
end
|
295
|
+
```
|
296
|
+
|
297
|
+
If the `:self` link contains _more attributes_, they will show up here too.
|
298
|
+
|
299
|
+
```ruby
|
300
|
+
class BookSerializer < MediaTypes::Serialization::Base
|
301
|
+
validator BookValidator
|
302
|
+
|
303
|
+
output versions: [1, 2, 3] do |obj, version, context|
|
304
|
+
attribute :book do
|
305
|
+
link :self, href: context.book_url(obj), isbn: obj.isbn if version >= 3
|
306
|
+
|
307
|
+
attribute :title, obj.title
|
308
|
+
attribute :description, obj.description if version >= 2
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
output view: :index, version: 3 do |arr, version, context|
|
313
|
+
attribute :books do
|
314
|
+
link :self, href: context.book_index_url
|
315
|
+
|
316
|
+
index arr, version: version
|
317
|
+
end
|
318
|
+
end
|
319
|
+
end
|
320
|
+
```
|
321
|
+
|
322
|
+
```ruby
|
323
|
+
class BookValidator
|
324
|
+
include MediaTypes::Dsl
|
325
|
+
|
326
|
+
def self.organisation
|
327
|
+
'acme'
|
328
|
+
end
|
329
|
+
|
330
|
+
use_name 'book'
|
331
|
+
|
332
|
+
validations do
|
333
|
+
view :index do
|
334
|
+
version 3 do
|
335
|
+
attribute :books do
|
336
|
+
link :self
|
337
|
+
|
338
|
+
collection :_index, allow_empty: true do
|
339
|
+
attribute :href, String
|
340
|
+
attribute :isbn, AllowNil(String)
|
341
|
+
end
|
342
|
+
end
|
343
|
+
end
|
344
|
+
end
|
345
|
+
end
|
346
|
+
end
|
347
|
+
```
|
348
|
+
|
267
349
|
#### Collections
|
268
350
|
|
269
351
|
A collection inlines the member objects.
|
@@ -321,6 +403,37 @@ BookSerializer.serialize([book], BookValidator.view(:collection).version(3), con
|
|
321
403
|
# }
|
322
404
|
```
|
323
405
|
|
406
|
+
The `collection` dsl is _not_ the same as the one in the validation gem. This is how you could validate collections:
|
407
|
+
|
408
|
+
```ruby
|
409
|
+
class BookValidator
|
410
|
+
include MediaTypes::Dsl
|
411
|
+
|
412
|
+
def self.organisation
|
413
|
+
'acme'
|
414
|
+
end
|
415
|
+
|
416
|
+
use_name 'book'
|
417
|
+
|
418
|
+
validations do
|
419
|
+
view :collection do
|
420
|
+
version 3 do
|
421
|
+
attribute :books do
|
422
|
+
link :self
|
423
|
+
|
424
|
+
collection :_embedded, allow_empty: true do
|
425
|
+
link :self
|
426
|
+
|
427
|
+
attribute :title, String
|
428
|
+
attribute :description, AllowNil(String)
|
429
|
+
end
|
430
|
+
end
|
431
|
+
end
|
432
|
+
end
|
433
|
+
end
|
434
|
+
end
|
435
|
+
```
|
436
|
+
|
324
437
|
### Input deserialization
|
325
438
|
|
326
439
|
You can mark a media type as something that's allowed to be sent along with a PUT request as follows:
|
@@ -460,6 +573,9 @@ While this is not a good practise as this makes it hard to add new fields or rem
|
|
460
573
|
class BookSerializer < MediaTypes::Serialization::Base
|
461
574
|
validator BookValidator
|
462
575
|
|
576
|
+
# applicaton/vnd.acme.book.v3+json
|
577
|
+
# applicaton/vnd.acme.book.v2+json
|
578
|
+
# applicaton/vnd.acme.book.v1+json
|
463
579
|
output versions: [1, 2, 3] do |obj, version, context|
|
464
580
|
attribute :book do
|
465
581
|
link :self, href: context.book_url(obj) if version >= 3
|
@@ -468,8 +584,18 @@ class BookSerializer < MediaTypes::Serialization::Base
|
|
468
584
|
attribute :description, obj.description if version >= 2
|
469
585
|
end
|
470
586
|
end
|
471
|
-
|
587
|
+
|
588
|
+
# applicaton/vnd.acme.book+json
|
589
|
+
output version: nil do |obj, version, context|
|
590
|
+
attribute :book do
|
591
|
+
attribute :title, obj.title
|
592
|
+
end
|
593
|
+
end
|
594
|
+
output_alias 'application/json' # maps application/json to to applicaton/vnd.acme.book+json
|
472
595
|
|
596
|
+
# applicaton/vnd.acme.book.v3.create+json
|
597
|
+
# applicaton/vnd.acme.book.v2.create+json
|
598
|
+
# applicaton/vnd.acme.book.v1.create+json
|
473
599
|
input view: :create, versions: [1, 2, 3] do |json, version, context|
|
474
600
|
book = Book.new
|
475
601
|
book.title = json['book']['title']
|
@@ -479,10 +605,20 @@ class BookSerializer < MediaTypes::Serialization::Base
|
|
479
605
|
# Make sure not to save here but only save in the controller
|
480
606
|
book
|
481
607
|
end
|
482
|
-
|
608
|
+
|
609
|
+
# applicaton/vnd.acme.book.create+json
|
610
|
+
input view: :create, version: nil do |json, version, context|
|
611
|
+
book = Book.new
|
612
|
+
book.title = json['book']['title']
|
613
|
+
book.description = 'Not available'
|
614
|
+
|
615
|
+
# Make sure not to save here but only save in the controller
|
616
|
+
book
|
617
|
+
end
|
618
|
+
input_alias 'application/json', view: :create # maps application/json to to applicaton/vnd.acme.book.create+json
|
483
619
|
```
|
484
620
|
|
485
|
-
Validation will be done using the remapped validator. Aliasses map to version `nil
|
621
|
+
Validation will be done using the remapped validator. Aliasses map to version `nil`. It is not possible to configure this version.
|
486
622
|
|
487
623
|
### HTML
|
488
624
|
|
@@ -37,7 +37,7 @@ Gem::Specification.new do |spec|
|
|
37
37
|
|
38
38
|
spec.add_dependency 'actionpack', '>= 4.0.0'
|
39
39
|
spec.add_dependency 'activesupport', '>= 4.0.0'
|
40
|
-
spec.add_dependency 'media_types', '>= 2.1.
|
40
|
+
spec.add_dependency 'media_types', '>= 2.1.1', '< 3.0.0'
|
41
41
|
|
42
42
|
spec.add_development_dependency 'awesome_print'
|
43
43
|
spec.add_development_dependency 'bundler'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: media_types-serialization
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Derk-Jan Karrenbeld
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2021-08-
|
12
|
+
date: 2021-08-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: actionpack
|
@@ -45,7 +45,7 @@ dependencies:
|
|
45
45
|
requirements:
|
46
46
|
- - ">="
|
47
47
|
- !ruby/object:Gem::Version
|
48
|
-
version: 2.1.
|
48
|
+
version: 2.1.1
|
49
49
|
- - "<"
|
50
50
|
- !ruby/object:Gem::Version
|
51
51
|
version: 3.0.0
|
@@ -55,7 +55,7 @@ dependencies:
|
|
55
55
|
requirements:
|
56
56
|
- - ">="
|
57
57
|
- !ruby/object:Gem::Version
|
58
|
-
version: 2.1.
|
58
|
+
version: 2.1.1
|
59
59
|
- - "<"
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: 3.0.0
|