media_types-serialization 0.2.0 → 0.3.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: 8a38d269c583007ef55081c452341cf6a6f57403ed698102f5757afa18e2841e
4
- data.tar.gz: 70bc3b062800391332e97aefae2f95c5dae789a88e9ec6005f217401b2804f57
3
+ metadata.gz: d00191a59e5d1c2df575e66ade4f41fcf525cb4c1ea5878eeeb8a8cc2b86b3a5
4
+ data.tar.gz: 27c6b628de9e717955a1d3f9fcc548ac5f3d253cc392ea7cf5d1ed50a1b851fe
5
5
  SHA512:
6
- metadata.gz: f8a4358361d693f6397d156c9f1466b729a3bac48b00d5b395afade20e9463d0cdcd8a4db660742110bca2320a36a0be3866330c4a7de20c18fd9c3cfc1482f3
7
- data.tar.gz: f91169a81c5527068576bd0f200a6178795a3e818951c7fee5748f4cee8374d5e5a3516bb1b9cbbe252f2727856b2ee8eab6889258bb0e9e8d3576f641a74a5a
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.1.1, ruby-2.5.3-p105) [gem]" level="application" />
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.8, ruby-2.5.3-p105) [gem]" level="application" />
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
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.0
4
+
5
+ - Change `to_link_header` to return a string ready to be used as header
6
+ - Change `header_links` to actually return the links for the header in object form
7
+
3
8
  ## 0.2.0
4
9
 
5
10
  - Change `method_missing` and base methods for `Base`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- media_types-serialization (0.2.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.8)
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.class.instance_methods(false).include?(:to_json) ? obj.to_json(options) : obj.to_hash.to_json(options)
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.class.instance_methods(false).include?(:to_xml) ? obj.to_xml(options) : obj.to_hash.to_xml(options)
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.class.instance_methods(false).include?(:to_html) ? obj.to_html : obj.to_s
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
@@ -1,5 +1,5 @@
1
1
  module MediaTypes
2
2
  module Serialization
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
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: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derk-Jan Karrenbeld