media_types-serialization 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f81671e23c69f1315a0bd9d07ad55ddf0d5e9f2a2c46f1597a905525bcb19bef
4
- data.tar.gz: 3dc428be52dafa3a00538b947f4126cb884dd1a6ae1613fe0e6e2dd1e307177e
3
+ metadata.gz: 8a38d269c583007ef55081c452341cf6a6f57403ed698102f5757afa18e2841e
4
+ data.tar.gz: 70bc3b062800391332e97aefae2f95c5dae789a88e9ec6005f217401b2804f57
5
5
  SHA512:
6
- metadata.gz: 276c8eed325348f3fd7b270e363349698d1ca94fd68b3804e74ff0c1f52791dc6215f92be8837588eca805fe7437ebac312e636df6e8c014bd86739334054572
7
- data.tar.gz: 1cd405531bba571c8f78626444bfb239f5d112ce2f19726319c8eda7ce87b5865043096a3146be5ed045dc0287ca038ebe5b86f1b9b9029833133d3eb59165ac
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.0, ruby-2.5.3-p105) [gem]" level="application" />
38
- <orderEntry type="library" scope="PROVIDED" name="http_headers-link (v0.1.0, ruby-2.5.3-p105) [gem]" level="application" />
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
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.0
4
+
5
+ - Change `method_missing` and base methods for `Base`
6
+ - Add test for `HtmlWrapper`
7
+
3
8
  ## 0.1.0
4
9
 
5
10
  :baby: initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- media_types-serialization (0.1.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.0)
38
- http_headers-utils (< 1.0.0)
39
- http_headers-link (0.1.0)
40
- http_headers-utils (< 1.0.0)
41
- http_headers-utils (0.1.1)
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
- render media: serialize_media(@book)
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
- def to_html
29
- raise NotImplementedError, format(
30
- 'In %<class>s, to_html is not implemented.',
31
- class: self.class.name
32
- )
33
- end
34
-
35
- def to_xml
36
- raise NotImplementedError, format(
37
- 'In %<class>s, to_xml is not implemented.',
38
- class: self.class.name
39
- )
40
- end
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
- def to_json
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 respond_to?(sym, include_all = false)
78
- return false if [:to_h, :to_hash, :to_json, :to_text, :to_xml, :to_html, :to_body, :extract_self].include?(sym)
79
- return true if sym == :to_link_header
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
@@ -1,5 +1,5 @@
1
1
  module MediaTypes
2
2
  module Serialization
3
- VERSION = "0.1.0"
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  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.1.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-18 00:00:00.000000000 Z
11
+ date: 2019-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack