media_types-serialization 0.2.0 → 0.3.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/.idea/media_types-serialization.iml +2 -2
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +2 -2
- data/README.md +14 -2
- data/lib/media_types/serialization/base.rb +11 -1
- data/lib/media_types/serialization/renderer.rb +3 -3
- data/lib/media_types/serialization/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: d00191a59e5d1c2df575e66ade4f41fcf525cb4c1ea5878eeeb8a8cc2b86b3a5
|
4
|
+
data.tar.gz: 27c6b628de9e717955a1d3f9fcc548ac5f3d253cc392ea7cf5d1ed50a1b851fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7486b226c973e8e2aba5b3ed4c1b0054530717dec192fecd17412a10def1436718337d851dac0defeaae58cd95e1f494811b4bd42faa967c8fba1f8ce27984ee
|
7
|
+
data.tar.gz: 0c88f619c963cc0feb0a8dea38cc567bbd2c541211b245a06fa48a55f6aac221e0d8885ab7253a227f0f90d9f29e9d0791e2d285b0afb7799164416098c30d4a
|
@@ -36,14 +36,14 @@
|
|
36
36
|
<orderEntry type="library" scope="PROVIDED" name="erubi (v1.8.0, ruby-2.5.3-p105) [gem]" level="application" />
|
37
37
|
<orderEntry type="library" scope="PROVIDED" name="http_headers-accept (v0.2.1, ruby-2.5.3-p105) [gem]" level="application" />
|
38
38
|
<orderEntry type="library" scope="PROVIDED" name="http_headers-link (v0.2.1, ruby-2.5.3-p105) [gem]" level="application" />
|
39
|
-
<orderEntry type="library" scope="PROVIDED" name="http_headers-utils (v0.
|
39
|
+
<orderEntry type="library" scope="PROVIDED" name="http_headers-utils (v0.2.0, ruby-2.5.3-p105) [gem]" level="application" />
|
40
40
|
<orderEntry type="library" scope="PROVIDED" name="i18n (v1.5.3, ruby-2.5.3-p105) [gem]" level="application" />
|
41
41
|
<orderEntry type="library" scope="PROVIDED" name="loofah (v2.2.3, ruby-2.5.3-p105) [gem]" level="application" />
|
42
42
|
<orderEntry type="library" scope="PROVIDED" name="media_types (v0.6.0, ruby-2.5.3-p105) [gem]" level="application" />
|
43
43
|
<orderEntry type="library" scope="PROVIDED" name="mini_portile2 (v2.4.0, ruby-2.5.3-p105) [gem]" level="application" />
|
44
44
|
<orderEntry type="library" scope="PROVIDED" name="minitest (v5.11.3, ruby-2.5.3-p105) [gem]" level="application" />
|
45
45
|
<orderEntry type="library" scope="PROVIDED" name="nokogiri (v1.10.1, ruby-2.5.3-p105) [gem]" level="application" />
|
46
|
-
<orderEntry type="library" scope="PROVIDED" name="oj (v3.7.
|
46
|
+
<orderEntry type="library" scope="PROVIDED" name="oj (v3.7.9, ruby-2.5.3-p105) [gem]" level="application" />
|
47
47
|
<orderEntry type="library" scope="PROVIDED" name="rack (v2.0.6, ruby-2.5.3-p105) [gem]" level="application" />
|
48
48
|
<orderEntry type="library" scope="PROVIDED" name="rack-test (v1.1.0, ruby-2.5.3-p105) [gem]" level="application" />
|
49
49
|
<orderEntry type="library" scope="PROVIDED" name="rails-dom-testing (v2.0.3, ruby-2.5.3-p105) [gem]" level="application" />
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
media_types-serialization (0.
|
4
|
+
media_types-serialization (0.3.0)
|
5
5
|
actionpack (>= 4.0.0)
|
6
6
|
activesupport (>= 4.0.0)
|
7
7
|
http_headers-accept (< 1.0.0)
|
@@ -49,7 +49,7 @@ GEM
|
|
49
49
|
minitest (5.11.3)
|
50
50
|
nokogiri (1.10.1-x64-mingw32)
|
51
51
|
mini_portile2 (~> 2.4.0)
|
52
|
-
oj (3.7.
|
52
|
+
oj (3.7.9)
|
53
53
|
rack (2.0.6)
|
54
54
|
rack-test (1.1.0)
|
55
55
|
rack (>= 1.0, < 3)
|
data/README.md
CHANGED
@@ -81,11 +81,12 @@ By default, the media renderer will automatically detect and inject the followin
|
|
81
81
|
- suffix `+xml` if you define `to_xml`
|
82
82
|
- type `text/html` if you define `to_html`
|
83
83
|
|
84
|
-
The only limitation is that these methods need to be defined on the serializer class directly, and not in a super class.
|
85
|
-
|
86
84
|
If you do _not_ define these methods, only the `default` suffix / type will be used, `accepts_html` for the `text/html`
|
87
85
|
content-type.
|
88
86
|
|
87
|
+
If you don't define `to_html`, but try to make a serializer output `html`, it will be rendered in the layout at:
|
88
|
+
`serializers/wrapper/html_wrapper.html.erb` (or any other templating extension).
|
89
|
+
|
89
90
|
#### Migrations (versions)
|
90
91
|
If the serializer can serialize multiple _versions_, you can supply them through `additional_versions: [2, 3]`. A way to
|
91
92
|
handle this is via backward migrations, meaning you'll migrate from the current version back to an older version.
|
@@ -205,6 +206,17 @@ but planned for `xml` as well.
|
|
205
206
|
|
206
207
|
This behaviour can not be turned of as of writing.
|
207
208
|
|
209
|
+
### Link header
|
210
|
+
|
211
|
+
You can use `to_link_header` to generate a header value for the `Link` header.
|
212
|
+
|
213
|
+
```ruby
|
214
|
+
entries = @last_media_serializer.to_link_header
|
215
|
+
if entries.present?
|
216
|
+
response.header[HEADER_LINK] = entries
|
217
|
+
end
|
218
|
+
```
|
219
|
+
|
208
220
|
### Related
|
209
221
|
|
210
222
|
- [`MediaTypes`](https://github.com/SleeplessByte/media-types-ruby): :gem: Library to create media type definitions, schemes and validations
|
@@ -5,6 +5,9 @@ require 'uri'
|
|
5
5
|
require 'media_types/serialization/mime_type_support'
|
6
6
|
require 'media_types/serialization/migrations_support'
|
7
7
|
|
8
|
+
require 'http_headers/link'
|
9
|
+
require 'http_headers/utils/list'
|
10
|
+
|
8
11
|
module MediaTypes
|
9
12
|
module Serialization
|
10
13
|
class Base
|
@@ -22,7 +25,14 @@ module MediaTypes
|
|
22
25
|
end
|
23
26
|
|
24
27
|
def to_link_header
|
25
|
-
|
28
|
+
entries = header_links.each_with_index.map do |(rel, opts), index|
|
29
|
+
href = opts.is_a?(String) ? opts : opts.delete(:href)
|
30
|
+
parameters = { rel: rel }.merge(opts.is_a?(String) ? {} : opts)
|
31
|
+
HttpHeaders::Link::Entry.new("<#{href}>", index: index, parameters: parameters)
|
32
|
+
end
|
33
|
+
return nil unless entries.present?
|
34
|
+
|
35
|
+
HttpHeaders::Utils::List.to_header(entries)
|
26
36
|
end
|
27
37
|
|
28
38
|
COMMON_DERIVED_CALLERS = [:to_h, :to_hash, :to_json, :to_text, :to_xml, :to_html, :to_body, :extract_self].freeze
|
@@ -12,11 +12,11 @@ module MediaTypes
|
|
12
12
|
self.content_type ||= content_type
|
13
13
|
|
14
14
|
if content_type.ends_with?('+json') || Mime::Type.lookup(content_type) == Mime[:json]
|
15
|
-
obj.
|
15
|
+
obj.respond_to?(:to_json) ? obj.to_json(options) : obj.to_hash.to_json(options)
|
16
16
|
elsif content_type.ends_with?('+xml') || Mime::Type.lookup(content_type) == Mime[:xml]
|
17
|
-
obj.
|
17
|
+
obj.respond_to?(:to_xml) ? obj.to_xml(options) : obj.to_hash.to_xml(options)
|
18
18
|
elsif Mime::Type.lookup(content_type) == Mime[:html]
|
19
|
-
obj.
|
19
|
+
obj.respond_to?(:to_html) ? obj.to_html : obj.to_s
|
20
20
|
else
|
21
21
|
obj.to_body(content_type: options.delete(:content_type) || content_type, **options)
|
22
22
|
end
|