media_types-serialization 0.8.0 → 1.1.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/.github/workflows/ci.yml +16 -3
- data/.prettierrc +1 -0
- data/CHANGELOG.md +42 -0
- data/CODE_OF_CONDUCT.md +74 -74
- data/Gemfile.lock +74 -83
- data/README.md +691 -179
- data/lib/media_types/problem.rb +64 -0
- data/lib/media_types/serialization.rb +497 -173
- data/lib/media_types/serialization/base.rb +115 -91
- data/lib/media_types/serialization/error.rb +186 -0
- data/lib/media_types/serialization/fake_validator.rb +52 -0
- data/lib/media_types/serialization/serialization_dsl.rb +117 -0
- data/lib/media_types/serialization/serialization_registration.rb +245 -0
- data/lib/media_types/serialization/serializers/api_viewer.rb +133 -0
- data/lib/media_types/serialization/serializers/common_css.rb +168 -0
- data/lib/media_types/serialization/serializers/endpoint_description_serializer.rb +80 -0
- data/lib/media_types/serialization/serializers/fallback_not_acceptable_serializer.rb +85 -0
- data/lib/media_types/serialization/serializers/fallback_unsupported_media_type_serializer.rb +58 -0
- data/lib/media_types/serialization/serializers/input_validation_error_serializer.rb +89 -0
- data/lib/media_types/serialization/serializers/problem_serializer.rb +100 -0
- data/lib/media_types/serialization/utils/accept_header.rb +77 -0
- data/lib/media_types/serialization/utils/accept_language_header.rb +82 -0
- data/lib/media_types/serialization/utils/header_list.rb +89 -0
- data/lib/media_types/serialization/version.rb +1 -1
- data/media_types-serialization.gemspec +48 -50
- metadata +48 -79
- data/.travis.yml +0 -17
- data/lib/generators/media_types/serialization/api_viewer/api_viewer_generator.rb +0 -25
- data/lib/generators/media_types/serialization/api_viewer/templates/api_viewer.html.erb +0 -98
- data/lib/generators/media_types/serialization/api_viewer/templates/initializer.rb +0 -33
- data/lib/generators/media_types/serialization/api_viewer/templates/template_controller.rb +0 -23
- data/lib/media_types/serialization/media_type/register.rb +0 -4
- data/lib/media_types/serialization/migrations_command.rb +0 -38
- data/lib/media_types/serialization/migrations_support.rb +0 -50
- data/lib/media_types/serialization/mime_type_support.rb +0 -64
- data/lib/media_types/serialization/no_content_type_given.rb +0 -11
- data/lib/media_types/serialization/no_media_type_serializers.rb +0 -11
- data/lib/media_types/serialization/no_serializer_for_content_type.rb +0 -15
- data/lib/media_types/serialization/renderer.rb +0 -41
- data/lib/media_types/serialization/renderer/register.rb +0 -4
- data/lib/media_types/serialization/wrapper.rb +0 -13
- data/lib/media_types/serialization/wrapper/html_wrapper.rb +0 -45
- data/lib/media_types/serialization/wrapper/media_collection_wrapper.rb +0 -59
- data/lib/media_types/serialization/wrapper/media_index_wrapper.rb +0 -59
- data/lib/media_types/serialization/wrapper/media_object_wrapper.rb +0 -55
- data/lib/media_types/serialization/wrapper_support.rb +0 -38
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: media_types-serialization
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Derk-Jan Karrenbeld
|
8
|
-
|
8
|
+
- Max Maton
|
9
|
+
autorequire:
|
9
10
|
bindir: exe
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2021-06-29 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: actionpack
|
@@ -44,62 +45,20 @@ dependencies:
|
|
44
45
|
requirements:
|
45
46
|
- - ">="
|
46
47
|
- !ruby/object:Gem::Version
|
47
|
-
version: 0.
|
48
|
-
type: :runtime
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: 0.6.2
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: oj
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: 3.5.0
|
62
|
-
type: :runtime
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: 3.5.0
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: http_headers-accept
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: 0.2.2
|
48
|
+
version: 2.0.0
|
76
49
|
- - "<"
|
77
50
|
- !ruby/object:Gem::Version
|
78
|
-
version:
|
51
|
+
version: 3.0.0
|
79
52
|
type: :runtime
|
80
53
|
prerelease: false
|
81
54
|
version_requirements: !ruby/object:Gem::Requirement
|
82
55
|
requirements:
|
83
56
|
- - ">="
|
84
57
|
- !ruby/object:Gem::Version
|
85
|
-
version: 0.
|
86
|
-
- - "<"
|
87
|
-
- !ruby/object:Gem::Version
|
88
|
-
version: 1.0.0
|
89
|
-
- !ruby/object:Gem::Dependency
|
90
|
-
name: http_headers-link
|
91
|
-
requirement: !ruby/object:Gem::Requirement
|
92
|
-
requirements:
|
93
|
-
- - "<"
|
94
|
-
- !ruby/object:Gem::Version
|
95
|
-
version: 1.0.0
|
96
|
-
type: :runtime
|
97
|
-
prerelease: false
|
98
|
-
version_requirements: !ruby/object:Gem::Requirement
|
99
|
-
requirements:
|
58
|
+
version: 2.0.0
|
100
59
|
- - "<"
|
101
60
|
- !ruby/object:Gem::Version
|
102
|
-
version:
|
61
|
+
version: 3.0.0
|
103
62
|
- !ruby/object:Gem::Dependency
|
104
63
|
name: awesome_print
|
105
64
|
requirement: !ruby/object:Gem::Requirement
|
@@ -118,16 +77,16 @@ dependencies:
|
|
118
77
|
name: bundler
|
119
78
|
requirement: !ruby/object:Gem::Requirement
|
120
79
|
requirements:
|
121
|
-
- - "
|
80
|
+
- - ">="
|
122
81
|
- !ruby/object:Gem::Version
|
123
|
-
version: '
|
82
|
+
version: '0'
|
124
83
|
type: :development
|
125
84
|
prerelease: false
|
126
85
|
version_requirements: !ruby/object:Gem::Requirement
|
127
86
|
requirements:
|
128
|
-
- - "
|
87
|
+
- - ">="
|
129
88
|
- !ruby/object:Gem::Version
|
130
|
-
version: '
|
89
|
+
version: '0'
|
131
90
|
- !ruby/object:Gem::Dependency
|
132
91
|
name: rails
|
133
92
|
requirement: !ruby/object:Gem::Requirement
|
@@ -148,14 +107,14 @@ dependencies:
|
|
148
107
|
requirements:
|
149
108
|
- - "~>"
|
150
109
|
- !ruby/object:Gem::Version
|
151
|
-
version: '
|
110
|
+
version: '13.0'
|
152
111
|
type: :development
|
153
112
|
prerelease: false
|
154
113
|
version_requirements: !ruby/object:Gem::Requirement
|
155
114
|
requirements:
|
156
115
|
- - "~>"
|
157
116
|
- !ruby/object:Gem::Version
|
158
|
-
version: '
|
117
|
+
version: '13.0'
|
159
118
|
- !ruby/object:Gem::Dependency
|
160
119
|
name: minitest
|
161
120
|
requirement: !ruby/object:Gem::Requirement
|
@@ -170,9 +129,24 @@ dependencies:
|
|
170
129
|
- - "~>"
|
171
130
|
- !ruby/object:Gem::Version
|
172
131
|
version: '5.0'
|
173
|
-
|
132
|
+
- !ruby/object:Gem::Dependency
|
133
|
+
name: oj
|
134
|
+
requirement: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
type: :development
|
140
|
+
prerelease: false
|
141
|
+
version_requirements: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
description:
|
174
147
|
email:
|
175
148
|
- derk-jan@xpbytes.com
|
149
|
+
- max@delftsolutions.nl
|
176
150
|
executables: []
|
177
151
|
extensions: []
|
178
152
|
extra_rdoc_files: []
|
@@ -188,7 +162,7 @@ files:
|
|
188
162
|
- ".idea/modules.xml"
|
189
163
|
- ".idea/runConfigurations/test.xml"
|
190
164
|
- ".idea/vcs.xml"
|
191
|
-
- ".
|
165
|
+
- ".prettierrc"
|
192
166
|
- CHANGELOG.md
|
193
167
|
- CODE_OF_CONDUCT.md
|
194
168
|
- Gemfile
|
@@ -198,29 +172,24 @@ files:
|
|
198
172
|
- Rakefile
|
199
173
|
- bin/console
|
200
174
|
- bin/setup
|
201
|
-
- lib/
|
202
|
-
- lib/generators/media_types/serialization/api_viewer/templates/api_viewer.html.erb
|
203
|
-
- lib/generators/media_types/serialization/api_viewer/templates/initializer.rb
|
204
|
-
- lib/generators/media_types/serialization/api_viewer/templates/template_controller.rb
|
175
|
+
- lib/media_types/problem.rb
|
205
176
|
- lib/media_types/serialization.rb
|
206
177
|
- lib/media_types/serialization/base.rb
|
207
178
|
- lib/media_types/serialization/error.rb
|
208
|
-
- lib/media_types/serialization/
|
209
|
-
- lib/media_types/serialization/
|
210
|
-
- lib/media_types/serialization/
|
211
|
-
- lib/media_types/serialization/
|
212
|
-
- lib/media_types/serialization/
|
213
|
-
- lib/media_types/serialization/
|
214
|
-
- lib/media_types/serialization/
|
215
|
-
- lib/media_types/serialization/
|
216
|
-
- lib/media_types/serialization/
|
179
|
+
- lib/media_types/serialization/fake_validator.rb
|
180
|
+
- lib/media_types/serialization/serialization_dsl.rb
|
181
|
+
- lib/media_types/serialization/serialization_registration.rb
|
182
|
+
- lib/media_types/serialization/serializers/api_viewer.rb
|
183
|
+
- lib/media_types/serialization/serializers/common_css.rb
|
184
|
+
- lib/media_types/serialization/serializers/endpoint_description_serializer.rb
|
185
|
+
- lib/media_types/serialization/serializers/fallback_not_acceptable_serializer.rb
|
186
|
+
- lib/media_types/serialization/serializers/fallback_unsupported_media_type_serializer.rb
|
187
|
+
- lib/media_types/serialization/serializers/input_validation_error_serializer.rb
|
188
|
+
- lib/media_types/serialization/serializers/problem_serializer.rb
|
189
|
+
- lib/media_types/serialization/utils/accept_header.rb
|
190
|
+
- lib/media_types/serialization/utils/accept_language_header.rb
|
191
|
+
- lib/media_types/serialization/utils/header_list.rb
|
217
192
|
- lib/media_types/serialization/version.rb
|
218
|
-
- lib/media_types/serialization/wrapper.rb
|
219
|
-
- lib/media_types/serialization/wrapper/html_wrapper.rb
|
220
|
-
- lib/media_types/serialization/wrapper/media_collection_wrapper.rb
|
221
|
-
- lib/media_types/serialization/wrapper/media_index_wrapper.rb
|
222
|
-
- lib/media_types/serialization/wrapper/media_object_wrapper.rb
|
223
|
-
- lib/media_types/serialization/wrapper_support.rb
|
224
193
|
- media_types-serialization.gemspec
|
225
194
|
homepage: https://github.com/XPBytes/media_types-serialization
|
226
195
|
licenses:
|
@@ -229,7 +198,7 @@ metadata:
|
|
229
198
|
homepage_uri: https://github.com/XPBytes/media_types-serialization
|
230
199
|
source_code_uri: https://github.com/XPBytes/media_types-serialization
|
231
200
|
changelog_uri: https://github.com/XPBytes/media_types-serialization/CHANGELOG.md
|
232
|
-
post_install_message:
|
201
|
+
post_install_message:
|
233
202
|
rdoc_options: []
|
234
203
|
require_paths:
|
235
204
|
- lib
|
@@ -244,8 +213,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
244
213
|
- !ruby/object:Gem::Version
|
245
214
|
version: '0'
|
246
215
|
requirements: []
|
247
|
-
rubygems_version: 3.
|
248
|
-
signing_key:
|
216
|
+
rubygems_version: 3.1.6
|
217
|
+
signing_key:
|
249
218
|
specification_version: 4
|
250
219
|
summary: Add media types supported serialization using your favourite serializer
|
251
220
|
test_files: []
|
data/.travis.yml
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sudo: false
|
3
|
-
language: ruby
|
4
|
-
cache: bundler
|
5
|
-
rvm:
|
6
|
-
- 2.5
|
7
|
-
- 2.6
|
8
|
-
- ruby-head
|
9
|
-
before_install:
|
10
|
-
- gem install bundler -v 2.0.1
|
11
|
-
- gem update --system
|
12
|
-
- gem --version
|
13
|
-
matrix:
|
14
|
-
allow_failures:
|
15
|
-
- rvm: ruby-head
|
16
|
-
install:
|
17
|
-
- bundle install --with development --jobs=3 --retry=3 --path=${BUNDLE_PATH:-vendor/bundle}
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'rails/generators/base'
|
2
|
-
|
3
|
-
module MediaTypes
|
4
|
-
module Serialization
|
5
|
-
class ApiViewerGenerator < Rails::Generators::Base
|
6
|
-
source_root File.expand_path('templates', __dir__)
|
7
|
-
|
8
|
-
def copy_controllers
|
9
|
-
copy_file "template_controller.rb", "app/controllers/api/template_controller.rb"
|
10
|
-
end
|
11
|
-
|
12
|
-
def copy_views
|
13
|
-
copy_file "api_viewer.html.erb", "app/views/serializers/wrapper/html_wrapper.html.erb"
|
14
|
-
end
|
15
|
-
|
16
|
-
def copy_initializer
|
17
|
-
copy_file "initializer.rb", "config/initializers/media_types_serialization.rb"
|
18
|
-
end
|
19
|
-
|
20
|
-
def add_route
|
21
|
-
route "namespace :api do\n match '/template', controller: :template, action: :create, via: %i[get post], as: :template\nend\n\n"
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,98 +0,0 @@
|
|
1
|
-
<!DOCTYPE html>
|
2
|
-
<html lang="en">
|
3
|
-
<head>
|
4
|
-
<title>API Viewer</title>
|
5
|
-
<%= csrf_meta_tags %>
|
6
|
-
<%= csp_meta_tag %>
|
7
|
-
|
8
|
-
<% # The following expects webpacker to be active, but you can replace these if you use the sprockets pipeline %>
|
9
|
-
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
|
10
|
-
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
|
11
|
-
</head>
|
12
|
-
<%
|
13
|
-
def replace_link(full_match)
|
14
|
-
href = full_match[9...-1]
|
15
|
-
'"href": "%s"' % link_to(href.gsub('%7B', '{').gsub('%7D', '}'), href).html_safe
|
16
|
-
rescue
|
17
|
-
full_match
|
18
|
-
end
|
19
|
-
%>
|
20
|
-
<body class="container h-100" style="overflow: auto; justify-content: start">
|
21
|
-
|
22
|
-
<%# Remove the # below to show a logo %>
|
23
|
-
<%# link_to Rails.application.routes.url_helpers.root_path do %>
|
24
|
-
<%# render 'shared/logo', assigns: { width: 300, height: 63, style: "flex-shrink: 0; margin: 20px 0 40px" } %>
|
25
|
-
<%# end %>
|
26
|
-
|
27
|
-
<div class="h-auto w-100 mb-4">
|
28
|
-
<% links = @serializer.send(:header_links) %>
|
29
|
-
<% if links.present? %>
|
30
|
-
<nav class="card w-100 mb-4 card--inverse">
|
31
|
-
<div class="card-header">
|
32
|
-
<h5 class="card-title my-auto">HTTP Links</h5>
|
33
|
-
</div>
|
34
|
-
<ul class="list-group list-group-flush">
|
35
|
-
<% links.each do |key, link| %>
|
36
|
-
<% href = link && link[:href] || next %>
|
37
|
-
<li class="list-group-item">
|
38
|
-
<% if link[:templated] %>
|
39
|
-
<%# You need to add this route to your routes file; Use the provided generator %>
|
40
|
-
<%# rails g api_viewer %>
|
41
|
-
<%= form_tag Rails.application.routes.url_helpers.api_template_path, { method: :get, class: 'link-template-form' } do %>
|
42
|
-
<%= hidden_field_tag 'template[href]', href %>
|
43
|
-
<button type="submit"><strong><%= key %></strong></button>
|
44
|
-
<code><%= href.gsub(/:([a-z][^\/]*)|(?:{|%7B)([a-z][^\/{}]*)(%7D|})/) do |match|
|
45
|
-
tag = match.tr(':{}', '').delete('%7B').delete('%7D')
|
46
|
-
text_field_tag('template[%s]' % tag, nil, placeholder: tag)
|
47
|
-
end.html_safe %></code>
|
48
|
-
<% end %>
|
49
|
-
<% else %>
|
50
|
-
<%= link_to href do %>
|
51
|
-
<strong><%= key %></strong>
|
52
|
-
<code><%= href %></code>
|
53
|
-
<% end %>
|
54
|
-
<% end %>
|
55
|
-
</li>
|
56
|
-
<% end %>
|
57
|
-
</ul>
|
58
|
-
</nav>
|
59
|
-
<% end %>
|
60
|
-
|
61
|
-
<article class="card w-100 mb-4 card--inverse">
|
62
|
-
<div class="card-body">
|
63
|
-
<h5 class="card-title"><%= @mime_type %></h5>
|
64
|
-
<small class="card-subtitle mb-2 text-muted">Serialized by: <%= @serializer.class.name %></small>
|
65
|
-
<pre><code class="code" style="
|
66
|
-
white-space: pre-wrap;
|
67
|
-
white-space: -moz-pre-wrap;
|
68
|
-
white-space: -o-pre-wrap;
|
69
|
-
word-wrap: break-word;"><% code = JSON.pretty_generate(@serializer.to_hash).truncate(50_000)
|
70
|
-
.gsub('</h1>', "</h1>\n")
|
71
|
-
.gsub('</div>', "</div>\n")
|
72
|
-
.gsub('<', '<')
|
73
|
-
.gsub('>', '>')
|
74
|
-
.gsub('\n', "<br>")%>
|
75
|
-
<%= code.gsub(/"href": "(http(?:s?:\/\/.*?))"/, &method(:replace_link)).html_safe %></code></pre>
|
76
|
-
</div>
|
77
|
-
</article>
|
78
|
-
|
79
|
-
<nav class="card w-100 mb-4 card--inverse">
|
80
|
-
<div class="card-header">
|
81
|
-
<h5 class="card-title my-auto">Endpoint representations</h5>
|
82
|
-
</div>
|
83
|
-
<ul class="list-group list-group-flush">
|
84
|
-
<% @representations.each do |representation| %>
|
85
|
-
<% mime_type = Mime::Type.lookup(representation) || next %>
|
86
|
-
<li class="list-group-item">
|
87
|
-
<%= link_to (@url_context + ".#{mime_type.symbol}").sub('/.', '.') do %>
|
88
|
-
<strong><%= representation %></strong>
|
89
|
-
<code><%= mime_type.symbol %></code>
|
90
|
-
<% end %>
|
91
|
-
</li>
|
92
|
-
<% end %>
|
93
|
-
</ul>
|
94
|
-
</nav>
|
95
|
-
</div>
|
96
|
-
</body>
|
97
|
-
</html>
|
98
|
-
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'media_types/serialization'
|
2
|
-
|
3
|
-
# This registers the renderer as side-effect
|
4
|
-
require 'media_types/serialization/renderer/register'
|
5
|
-
|
6
|
-
# This registers the media type as side-effect
|
7
|
-
require 'media_types/serialization/media_type/register'
|
8
|
-
|
9
|
-
##
|
10
|
-
# The following options are breaking and therefore disabled by default.
|
11
|
-
#
|
12
|
-
# When these are true, the +header_links+ and +extract_links+ methods is called
|
13
|
-
# when dealing with a .collection or .index view, respectively. It allows you
|
14
|
-
# to define +_links+ for the root level from your serializer.
|
15
|
-
#
|
16
|
-
#
|
17
|
-
MediaTypes::Serialization.collect_links_for_collection = true
|
18
|
-
MediaTypes::Serialization.collect_links_for_index = true
|
19
|
-
|
20
|
-
##
|
21
|
-
# The API Viewer template is provided if you used the generator. You can change
|
22
|
-
# the view it renders by changing the path below.
|
23
|
-
#
|
24
|
-
#
|
25
|
-
# ::MediaTypes::Serialization.api_viewer_layout = '/path/to/wrapper/layout'
|
26
|
-
|
27
|
-
##
|
28
|
-
# When .to_html is not provided by a serializer, it will fall back to render
|
29
|
-
# the API Viewer, but this template can be changed by changing the path
|
30
|
-
# below.
|
31
|
-
#
|
32
|
-
#
|
33
|
-
# ::MediaTypes::Serialization.html_wrapper_layout = '/path/to/wrapper/layout'
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module Api
|
2
|
-
class TemplateController < ApiController
|
3
|
-
def create
|
4
|
-
href = params[:template].delete(:href)
|
5
|
-
templated_values = params[:template].permit!.to_h
|
6
|
-
|
7
|
-
response["Location"] = templated_values.reduce(href) do |result, (key, value)|
|
8
|
-
result.sub!(%r{:#{key}|{#{key}}|%7B#{key}%7D}, value) || invalid_parameter(key, href)
|
9
|
-
end
|
10
|
-
head :temporary_redirect
|
11
|
-
end
|
12
|
-
|
13
|
-
private
|
14
|
-
|
15
|
-
def invalid_parameter(key, href)
|
16
|
-
raise ActionController::BadRequest, format(
|
17
|
-
'Received templated value for "%<key>s" which does not exist in templated link "%<href>s"',
|
18
|
-
key: key,
|
19
|
-
href: href.gsub('%7B', '{').gsub('%7D', '}')
|
20
|
-
)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
#
|
3
|
-
module MediaTypes
|
4
|
-
module Serialization
|
5
|
-
class MigrationsCommand
|
6
|
-
def initialize(serializer)
|
7
|
-
self.serializer = serializer
|
8
|
-
self.migrations = {}
|
9
|
-
end
|
10
|
-
|
11
|
-
def call(result, mime_type, view)
|
12
|
-
return result if mime_type.is_a?(String) || matches_current_mime_type?(view: view, mime_type: mime_type)
|
13
|
-
|
14
|
-
migrations.reduce(result) do |migrated, (version, migration)|
|
15
|
-
migrated = migration.call(migrated)
|
16
|
-
next migrated unless matches_mime_type?(mime_type.version(version), mime_type)
|
17
|
-
break migrated
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
private
|
22
|
-
|
23
|
-
attr_accessor :serializer, :migrations
|
24
|
-
|
25
|
-
def version(version, &block)
|
26
|
-
migrations[version] = ->(result) { serializer.instance_exec(result, &block) }
|
27
|
-
end
|
28
|
-
|
29
|
-
def matches_current_mime_type?(view:, mime_type:)
|
30
|
-
serializer.class.current_mime_type(view: view) == mime_type.to_s
|
31
|
-
end
|
32
|
-
|
33
|
-
def matches_mime_type?(left, right)
|
34
|
-
left.to_s == right.to_s
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|