media_types-serialization 0.1.0 → 0.2.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 +6 -6
- data/README.md +17 -1
- data/lib/media_types/serialization/base.rb +18 -59
- data/lib/media_types/serialization/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a38d269c583007ef55081c452341cf6a6f57403ed698102f5757afa18e2841e
|
4
|
+
data.tar.gz: 70bc3b062800391332e97aefae2f95c5dae789a88e9ec6005f217401b2804f57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8a4358361d693f6397d156c9f1466b729a3bac48b00d5b395afade20e9463d0cdcd8a4db660742110bca2320a36a0be3866330c4a7de20c18fd9c3cfc1482f3
|
7
|
+
data.tar.gz: f91169a81c5527068576bd0f200a6178795a3e818951c7fee5748f4cee8374d5e5a3516bb1b9cbbe252f2727856b2ee8eab6889258bb0e9e8d3576f641a74a5a
|
@@ -34,8 +34,8 @@
|
|
34
34
|
<orderEntry type="library" scope="PROVIDED" name="concurrent-ruby (v1.1.4, ruby-2.5.3-p105) [gem]" level="application" />
|
35
35
|
<orderEntry type="library" scope="PROVIDED" name="crass (v1.0.4, ruby-2.5.3-p105) [gem]" level="application" />
|
36
36
|
<orderEntry type="library" scope="PROVIDED" name="erubi (v1.8.0, ruby-2.5.3-p105) [gem]" level="application" />
|
37
|
-
<orderEntry type="library" scope="PROVIDED" name="http_headers-accept (v0.1
|
38
|
-
<orderEntry type="library" scope="PROVIDED" name="http_headers-link (v0.1
|
37
|
+
<orderEntry type="library" scope="PROVIDED" name="http_headers-accept (v0.2.1, ruby-2.5.3-p105) [gem]" level="application" />
|
38
|
+
<orderEntry type="library" scope="PROVIDED" name="http_headers-link (v0.2.1, ruby-2.5.3-p105) [gem]" level="application" />
|
39
39
|
<orderEntry type="library" scope="PROVIDED" name="http_headers-utils (v0.1.1, 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" />
|
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.2.0)
|
5
5
|
actionpack (>= 4.0.0)
|
6
6
|
activesupport (>= 4.0.0)
|
7
7
|
http_headers-accept (< 1.0.0)
|
@@ -34,11 +34,11 @@ GEM
|
|
34
34
|
concurrent-ruby (1.1.4)
|
35
35
|
crass (1.0.4)
|
36
36
|
erubi (1.8.0)
|
37
|
-
http_headers-accept (0.1
|
38
|
-
http_headers-utils (< 1.0.0)
|
39
|
-
http_headers-link (0.1
|
40
|
-
http_headers-utils (< 1.0.0)
|
41
|
-
http_headers-utils (0.
|
37
|
+
http_headers-accept (0.2.1)
|
38
|
+
http_headers-utils (>= 0.2.0, < 1.0.0)
|
39
|
+
http_headers-link (0.2.1)
|
40
|
+
http_headers-utils (>= 0.2.0, < 1.0.0)
|
41
|
+
http_headers-utils (0.2.0)
|
42
42
|
i18n (1.5.3)
|
43
43
|
concurrent-ruby (~> 1.0)
|
44
44
|
loofah (2.2.3)
|
data/README.md
CHANGED
@@ -147,11 +147,27 @@ class BookController < ApiController
|
|
147
147
|
freeze_accepted_media!
|
148
148
|
|
149
149
|
def show
|
150
|
-
|
150
|
+
# If you do NOT pass in the content_type, it will re-use the current content_type of the response if set or
|
151
|
+
# use the default content type of the serializer. This is fine if you only output one Content-Type in the
|
152
|
+
# action, but not if you are relying on content-negotiation.
|
153
|
+
|
154
|
+
render media: serialize_media(@book), content_type: request.format.to_s
|
151
155
|
end
|
152
156
|
end
|
153
157
|
```
|
154
158
|
|
159
|
+
If you have normalized your resources (e.g. into `@resource`), you may render resources like so:
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
class ApiController < ActionController::API
|
163
|
+
def render_media(**opts)
|
164
|
+
render media: serialize_media(@resource), content_type: request.format.to_s, **opts
|
165
|
+
end
|
166
|
+
end
|
167
|
+
```
|
168
|
+
|
169
|
+
And then call `render_media` whenever you're ready to render
|
170
|
+
|
155
171
|
### HTML output
|
156
172
|
|
157
173
|
You can define HTML outputs for example by creating a serializer that accepts `text/html`. At this moment, there may
|
@@ -25,58 +25,27 @@ module MediaTypes
|
|
25
25
|
{}
|
26
26
|
end
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
def to_hash
|
43
|
-
raise NotImplementedError, format(
|
44
|
-
'In %<class>s, to_hash is not implemented.',
|
45
|
-
class: self.class.name
|
46
|
-
)
|
47
|
-
end
|
48
|
-
|
49
|
-
def to_text
|
50
|
-
raise NotImplementedError, format(
|
51
|
-
'In %<class>s, to_text is not implemented/',
|
52
|
-
class: self.class.name
|
53
|
-
)
|
54
|
-
end
|
28
|
+
COMMON_DERIVED_CALLERS = [:to_h, :to_hash, :to_json, :to_text, :to_xml, :to_html, :to_body, :extract_self].freeze
|
29
|
+
|
30
|
+
def method_missing(symbol, *args, &block)
|
31
|
+
if COMMON_DERIVED_CALLERS.include?(symbol)
|
32
|
+
raise NotImplementedError, format(
|
33
|
+
'In %<class>s, %<symbol>s is not implemented. ' \
|
34
|
+
'Implement it or deny the MediaType[s] %<media_types>s for %<model>s',
|
35
|
+
symbol: symbol,
|
36
|
+
class: self.class.name,
|
37
|
+
model: serializable.class.name,
|
38
|
+
media_types: self.class.media_types(view: '[view]').to_s
|
39
|
+
)
|
40
|
+
end
|
55
41
|
|
56
|
-
|
57
|
-
raise NotImplementedError, format(
|
58
|
-
'In %<class>s, to_json is not implemented/',
|
59
|
-
class: self.class.name
|
60
|
-
)
|
61
|
-
end
|
62
|
-
|
63
|
-
def to_h
|
64
|
-
raise NotImplementedError, format(
|
65
|
-
'In %<class>s, to_h is not implemented. Missing alias to_h to_hash.',
|
66
|
-
class: self.class.name
|
67
|
-
)
|
68
|
-
end
|
69
|
-
|
70
|
-
def to_body
|
71
|
-
raise NotImplementedError, format(
|
72
|
-
'In %<class>s, to_body is not implemented. This is a general purpose catch all renderer',
|
73
|
-
class: self.class.name
|
74
|
-
)
|
42
|
+
super
|
75
43
|
end
|
76
44
|
|
77
|
-
def
|
78
|
-
|
79
|
-
|
45
|
+
def respond_to_missing?(method_name, include_private = false)
|
46
|
+
if COMMON_DERIVED_CALLERS.include?(method_name)
|
47
|
+
return false
|
48
|
+
end
|
80
49
|
|
81
50
|
super
|
82
51
|
end
|
@@ -85,16 +54,6 @@ module MediaTypes
|
|
85
54
|
|
86
55
|
attr_accessor :context, :current_media_type, :current_view
|
87
56
|
|
88
|
-
def extract_self
|
89
|
-
raise NotImplementedError, format(
|
90
|
-
'In %<class>s, extract_self is not implemented, thus a self link for %<model>s can not be generated. ' \
|
91
|
-
'Implement extract_self on %<class>s or deny the MediaType[s] %<media_types>s for this request.',
|
92
|
-
class: self.class.name,
|
93
|
-
model: serializable.class.name,
|
94
|
-
media_types: self.class.media_types(view: '[view]').to_s
|
95
|
-
)
|
96
|
-
end
|
97
|
-
|
98
57
|
def extract_links
|
99
58
|
{}
|
100
59
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: media_types-serialization
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Derk-Jan Karrenbeld
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-02-
|
11
|
+
date: 2019-02-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|