media_types-serialization 2.1.0 → 2.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/.github/workflows/ci.yml +32 -32
- data/.github/workflows/publish-bookworm.yml +34 -34
- data/.github/workflows/publish-sid.yml +34 -34
- data/.github/workflows/publish-trixie.yml +34 -0
- data/.gitignore +22 -22
- data/.idea/.rakeTasks +7 -7
- data/.idea/dictionaries/Derk_Jan.xml +6 -6
- data/.idea/encodings.xml +3 -3
- data/.idea/inspectionProfiles/Project_Default.xml +5 -5
- data/.idea/media_types-serialization.iml +76 -76
- data/.idea/misc.xml +6 -6
- data/.idea/modules.xml +7 -7
- data/.idea/runConfigurations/test.xml +19 -19
- data/.idea/vcs.xml +5 -5
- data/.vscode/settings.json +11 -0
- data/CHANGELOG.md +212 -207
- data/CODE_OF_CONDUCT.md +74 -74
- data/Gemfile +4 -4
- data/Gemfile.lock +182 -176
- data/LICENSE.txt +21 -21
- data/README.md +1058 -1058
- data/Rakefile +10 -10
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/lib/media_types/problem.rb +67 -67
- data/lib/media_types/serialization/base.rb +269 -269
- data/lib/media_types/serialization/error.rb +193 -193
- data/lib/media_types/serialization/fake_validator.rb +53 -53
- data/lib/media_types/serialization/serialization_dsl.rb +139 -139
- data/lib/media_types/serialization/serialization_registration.rb +245 -245
- data/lib/media_types/serialization/serializers/api_viewer.rb +413 -383
- data/lib/media_types/serialization/serializers/common_css.rb +212 -212
- data/lib/media_types/serialization/serializers/endpoint_description_serializer.rb +80 -80
- data/lib/media_types/serialization/serializers/fallback_not_acceptable_serializer.rb +85 -85
- data/lib/media_types/serialization/serializers/fallback_unsupported_media_type_serializer.rb +58 -58
- data/lib/media_types/serialization/serializers/input_validation_error_serializer.rb +95 -95
- data/lib/media_types/serialization/serializers/problem_serializer.rb +111 -111
- data/lib/media_types/serialization/utils/accept_header.rb +77 -77
- data/lib/media_types/serialization/utils/accept_language_header.rb +82 -82
- data/lib/media_types/serialization/version.rb +7 -7
- data/lib/media_types/serialization.rb +689 -689
- data/media_types-serialization.gemspec +48 -48
- metadata +13 -11
@@ -1,383 +1,413 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'media_types/serialization/base'
|
4
|
-
require 'erb'
|
5
|
-
require 'cgi'
|
6
|
-
|
7
|
-
module MediaTypes
|
8
|
-
module Serialization
|
9
|
-
module Serializers
|
10
|
-
class ApiViewer < MediaTypes::Serialization::Base
|
11
|
-
unvalidated 'text/html'
|
12
|
-
|
13
|
-
def self.viewerify(uri, current_host, type: 'last')
|
14
|
-
viewer = URI.parse(uri)
|
15
|
-
|
16
|
-
return uri unless viewer.host == current_host
|
17
|
-
|
18
|
-
query_parts = viewer.query&.split('&') || []
|
19
|
-
query_parts = query_parts.reject { |p| p.starts_with?('api_viewer=') }
|
20
|
-
query_parts.append("api_viewer=#{type}")
|
21
|
-
viewer.query = query_parts.join('&')
|
22
|
-
viewer.to_s
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.to_input_identifiers(serializers)
|
26
|
-
serializers.flat_map do |s|
|
27
|
-
s[:serializer].inputs_for(views: [s[:view]]).registrations.keys
|
28
|
-
end
|
29
|
-
end
|
30
|
-
def self.to_output_identifiers(serializers)
|
31
|
-
serializers.flat_map do |s|
|
32
|
-
s[:serializer].outputs_for(views: [s[:view]]).registrations.keys
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def self.allowed_replies(context, actions)
|
37
|
-
request_path = context.request.original_fullpath.split('?')[0]
|
38
|
-
|
39
|
-
path_prefix = ENV.fetch('RAILS_RELATIVE_URL_ROOT') { '' }
|
40
|
-
request_path = request_path.sub(path_prefix, '')
|
41
|
-
|
42
|
-
my_controller = Rails.application.routes.recognize_path request_path
|
43
|
-
possible_replies = ['POST', 'PUT', 'DELETE']
|
44
|
-
enabled_replies = {}
|
45
|
-
possible_replies.each do |m|
|
46
|
-
begin
|
47
|
-
found_controller = Rails.application.routes.recognize_path request_path, method: m
|
48
|
-
if found_controller[:controller] == my_controller[:controller]
|
49
|
-
enabled_replies[m] = found_controller[:action]
|
50
|
-
end
|
51
|
-
rescue ActionController::RoutingError
|
52
|
-
# not available
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
input_definitions = actions[:input] || {}
|
57
|
-
output_definitions = actions[:output] || {}
|
58
|
-
|
59
|
-
result = {}
|
60
|
-
global_in = input_definitions['all_actions'] || []
|
61
|
-
global_out = output_definitions['all_actions'] || []
|
62
|
-
|
63
|
-
viewer_uri = URI.parse(context.request.original_url)
|
64
|
-
query_parts = viewer_uri.query&.split('&') || []
|
65
|
-
query_parts = query_parts.select { |q| !q.start_with? 'api_viewer=' }
|
66
|
-
viewer_uri.query = (query_parts + ["api_viewer=last"]).join('&')
|
67
|
-
|
68
|
-
enabled_replies.each do |method, action|
|
69
|
-
input_serializers = global_in + (input_definitions[action] || [])
|
70
|
-
output_serializers = global_out + (output_definitions[action] || [])
|
71
|
-
result[method] = {
|
72
|
-
input: to_input_identifiers(input_serializers),
|
73
|
-
output: to_output_identifiers(output_serializers),
|
74
|
-
}
|
75
|
-
end
|
76
|
-
|
77
|
-
result
|
78
|
-
end
|
79
|
-
|
80
|
-
def self.escape_javascript(value)
|
81
|
-
escape_map = {
|
82
|
-
"\\" => "\\\\",
|
83
|
-
"</" => '<\/',
|
84
|
-
"\r\n" => '\n',
|
85
|
-
"\n" => '\n',
|
86
|
-
"\r" => '\n',
|
87
|
-
'"' => '\\"',
|
88
|
-
"'" => "\\'",
|
89
|
-
"`" => "\\`",
|
90
|
-
"$" => "\\$"
|
91
|
-
}
|
92
|
-
escape_map[(+"\342\200\250").force_encoding(Encoding::UTF_8).encode!] = "
"
|
93
|
-
escape_map[(+"\342\200\251").force_encoding(Encoding::UTF_8).encode!] = "
"
|
94
|
-
|
95
|
-
value ||= ""
|
96
|
-
|
97
|
-
return value.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"']|[`]|[$])/u, escape_map).html_safe
|
98
|
-
end
|
99
|
-
|
100
|
-
output_raw do |obj, version, context|
|
101
|
-
original_identifier = obj[:identifier]
|
102
|
-
registrations = obj[:registrations]
|
103
|
-
original_output = obj[:output]
|
104
|
-
original_links = obj[:links]
|
105
|
-
|
106
|
-
api_fied_links = original_links.map do |l|
|
107
|
-
new = l.dup
|
108
|
-
new[:invalid] = false
|
109
|
-
begin
|
110
|
-
uri = viewerify(new[:href], context.request.host)
|
111
|
-
new[:href] = uri.to_s
|
112
|
-
rescue URI::InvalidURIError
|
113
|
-
new[:invalid] = true
|
114
|
-
end
|
115
|
-
|
116
|
-
new
|
117
|
-
end
|
118
|
-
|
119
|
-
media_types = registrations.registrations.keys.map do |identifier|
|
120
|
-
result = {
|
121
|
-
identifier: identifier,
|
122
|
-
href: viewerify(context.request.original_url, context.request.host, type: identifier),
|
123
|
-
selected: identifier == original_identifier
|
124
|
-
}
|
125
|
-
result[:href] = '#output' if identifier == original_identifier
|
126
|
-
|
127
|
-
result
|
128
|
-
end
|
129
|
-
|
130
|
-
escaped_output = original_output
|
131
|
-
&.split("\n")
|
132
|
-
&.map { |l| CGI.escapeHTML(l).gsub(/ (?= )/, ' ') }
|
133
|
-
&.map do |l|
|
134
|
-
l.gsub(/\bhttps?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;{}]*[-A-Z0-9+@#\/%=}~_|](?![a-z]*;)/i) do |m|
|
135
|
-
converted = m
|
136
|
-
invalid = false
|
137
|
-
begin
|
138
|
-
converted = viewerify(m, context.request.host)
|
139
|
-
rescue URI::InvalidURIError
|
140
|
-
invalid = true
|
141
|
-
end
|
142
|
-
style = ''
|
143
|
-
style = ' style="color: red"' if invalid
|
144
|
-
"<a#{style} href=\"#{converted}\">#{m}</a>"
|
145
|
-
end
|
146
|
-
end
|
147
|
-
&.join("<br>\n")
|
148
|
-
|
149
|
-
unviewered_uri = URI.parse(context.request.original_url)
|
150
|
-
query_parts = unviewered_uri.query&.split('&') || []
|
151
|
-
query_parts = query_parts.select { |q| !q.start_with? 'api_viewer=' }
|
152
|
-
unviewered_uri.query = query_parts.join('&')
|
153
|
-
|
154
|
-
input = OpenStruct.new(
|
155
|
-
original_identifier: original_identifier,
|
156
|
-
escaped_output: escaped_output,
|
157
|
-
api_fied_links: api_fied_links,
|
158
|
-
media_types: media_types,
|
159
|
-
css: CommonCSS.css,
|
160
|
-
etag: obj[:etag],
|
161
|
-
allowed_replies: allowed_replies(context, obj[:actions]),
|
162
|
-
escape_javascript: method(:escape_javascript),
|
163
|
-
unviewered_uri: unviewered_uri
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
<
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
<
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
<
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
<
|
200
|
-
|
201
|
-
|
202
|
-
<% end %>
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
<
|
211
|
-
|
212
|
-
<
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
<
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
<label class="form-row"><div class="cell label">
|
229
|
-
|
230
|
-
|
231
|
-
<
|
232
|
-
|
233
|
-
<
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'media_types/serialization/base'
|
4
|
+
require 'erb'
|
5
|
+
require 'cgi'
|
6
|
+
|
7
|
+
module MediaTypes
|
8
|
+
module Serialization
|
9
|
+
module Serializers
|
10
|
+
class ApiViewer < MediaTypes::Serialization::Base
|
11
|
+
unvalidated 'text/html'
|
12
|
+
|
13
|
+
def self.viewerify(uri, current_host, type: 'last')
|
14
|
+
viewer = URI.parse(uri)
|
15
|
+
|
16
|
+
return uri unless viewer.host == current_host
|
17
|
+
|
18
|
+
query_parts = viewer.query&.split('&') || []
|
19
|
+
query_parts = query_parts.reject { |p| p.starts_with?('api_viewer=') }
|
20
|
+
query_parts.append("api_viewer=#{type}")
|
21
|
+
viewer.query = query_parts.join('&')
|
22
|
+
viewer.to_s
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.to_input_identifiers(serializers)
|
26
|
+
serializers.flat_map do |s|
|
27
|
+
s[:serializer].inputs_for(views: [s[:view]]).registrations.keys
|
28
|
+
end
|
29
|
+
end
|
30
|
+
def self.to_output_identifiers(serializers)
|
31
|
+
serializers.flat_map do |s|
|
32
|
+
s[:serializer].outputs_for(views: [s[:view]]).registrations.keys
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.allowed_replies(context, actions = {})
|
37
|
+
request_path = context.request.original_fullpath.split('?')[0]
|
38
|
+
|
39
|
+
path_prefix = ENV.fetch('RAILS_RELATIVE_URL_ROOT') { '' }
|
40
|
+
request_path = request_path.sub(path_prefix, '')
|
41
|
+
|
42
|
+
my_controller = Rails.application.routes.recognize_path request_path
|
43
|
+
possible_replies = ['POST', 'PUT', 'DELETE']
|
44
|
+
enabled_replies = {}
|
45
|
+
possible_replies.each do |m|
|
46
|
+
begin
|
47
|
+
found_controller = Rails.application.routes.recognize_path request_path, method: m
|
48
|
+
if found_controller[:controller] == my_controller[:controller]
|
49
|
+
enabled_replies[m] = found_controller[:action]
|
50
|
+
end
|
51
|
+
rescue ActionController::RoutingError
|
52
|
+
# not available
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
input_definitions = actions[:input] || {}
|
57
|
+
output_definitions = actions[:output] || {}
|
58
|
+
|
59
|
+
result = {}
|
60
|
+
global_in = input_definitions['all_actions'] || []
|
61
|
+
global_out = output_definitions['all_actions'] || []
|
62
|
+
|
63
|
+
viewer_uri = URI.parse(context.request.original_url)
|
64
|
+
query_parts = viewer_uri.query&.split('&') || []
|
65
|
+
query_parts = query_parts.select { |q| !q.start_with? 'api_viewer=' }
|
66
|
+
viewer_uri.query = (query_parts + ["api_viewer=last"]).join('&')
|
67
|
+
|
68
|
+
enabled_replies.each do |method, action|
|
69
|
+
input_serializers = global_in + (input_definitions[action] || [])
|
70
|
+
output_serializers = global_out + (output_definitions[action] || [])
|
71
|
+
result[method] = {
|
72
|
+
input: to_input_identifiers(input_serializers),
|
73
|
+
output: to_output_identifiers(output_serializers),
|
74
|
+
}
|
75
|
+
end
|
76
|
+
|
77
|
+
result
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.escape_javascript(value)
|
81
|
+
escape_map = {
|
82
|
+
"\\" => "\\\\",
|
83
|
+
"</" => '<\/',
|
84
|
+
"\r\n" => '\n',
|
85
|
+
"\n" => '\n',
|
86
|
+
"\r" => '\n',
|
87
|
+
'"' => '\\"',
|
88
|
+
"'" => "\\'",
|
89
|
+
"`" => "\\`",
|
90
|
+
"$" => "\\$"
|
91
|
+
}
|
92
|
+
escape_map[(+"\342\200\250").force_encoding(Encoding::UTF_8).encode!] = "
"
|
93
|
+
escape_map[(+"\342\200\251").force_encoding(Encoding::UTF_8).encode!] = "
"
|
94
|
+
|
95
|
+
value ||= ""
|
96
|
+
|
97
|
+
return value.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"']|[`]|[$])/u, escape_map).html_safe
|
98
|
+
end
|
99
|
+
|
100
|
+
output_raw do |obj, version, context|
|
101
|
+
original_identifier = obj[:identifier]
|
102
|
+
registrations = obj[:registrations]
|
103
|
+
original_output = obj[:output]
|
104
|
+
original_links = obj[:links]
|
105
|
+
|
106
|
+
api_fied_links = original_links.map do |l|
|
107
|
+
new = l.dup
|
108
|
+
new[:invalid] = false
|
109
|
+
begin
|
110
|
+
uri = viewerify(new[:href], context.request.host)
|
111
|
+
new[:href] = uri.to_s
|
112
|
+
rescue URI::InvalidURIError
|
113
|
+
new[:invalid] = true
|
114
|
+
end
|
115
|
+
|
116
|
+
new
|
117
|
+
end
|
118
|
+
|
119
|
+
media_types = registrations.registrations.keys.map do |identifier|
|
120
|
+
result = {
|
121
|
+
identifier: identifier,
|
122
|
+
href: viewerify(context.request.original_url, context.request.host, type: identifier),
|
123
|
+
selected: identifier == original_identifier
|
124
|
+
}
|
125
|
+
result[:href] = '#output' if identifier == original_identifier
|
126
|
+
|
127
|
+
result
|
128
|
+
end
|
129
|
+
|
130
|
+
escaped_output = original_output
|
131
|
+
&.split("\n")
|
132
|
+
&.map { |l| CGI.escapeHTML(l).gsub(/ (?= )/, ' ') }
|
133
|
+
&.map do |l|
|
134
|
+
l.gsub(/\bhttps?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;{}]*[-A-Z0-9+@#\/%=}~_|](?![a-z]*;)/i) do |m|
|
135
|
+
converted = m
|
136
|
+
invalid = false
|
137
|
+
begin
|
138
|
+
converted = viewerify(m, context.request.host)
|
139
|
+
rescue URI::InvalidURIError
|
140
|
+
invalid = true
|
141
|
+
end
|
142
|
+
style = ''
|
143
|
+
style = ' style="color: red"' if invalid
|
144
|
+
"<a#{style} href=\"#{converted}\">#{m}</a>"
|
145
|
+
end
|
146
|
+
end
|
147
|
+
&.join("<br>\n")
|
148
|
+
|
149
|
+
unviewered_uri = URI.parse(context.request.original_url)
|
150
|
+
query_parts = unviewered_uri.query&.split('&') || []
|
151
|
+
query_parts = query_parts.select { |q| !q.start_with? 'api_viewer=' }
|
152
|
+
unviewered_uri.query = query_parts.join('&')
|
153
|
+
|
154
|
+
input = OpenStruct.new(
|
155
|
+
original_identifier: original_identifier,
|
156
|
+
escaped_output: escaped_output,
|
157
|
+
api_fied_links: api_fied_links,
|
158
|
+
media_types: media_types,
|
159
|
+
css: CommonCSS.css,
|
160
|
+
etag: obj[:etag],
|
161
|
+
allowed_replies: allowed_replies(context, obj[:actions]),
|
162
|
+
escape_javascript: method(:escape_javascript),
|
163
|
+
unviewered_uri: unviewered_uri,
|
164
|
+
token: context.try(:form_authenticity_token)
|
165
|
+
)
|
166
|
+
|
167
|
+
template = ERB.new <<-HTML
|
168
|
+
<!DOCTYPE html>
|
169
|
+
<html lang="en">
|
170
|
+
<head>
|
171
|
+
<meta content="width=device-width, initial-scale=1" name="viewport">
|
172
|
+
|
173
|
+
<title>API Viewer [<%= CGI::escapeHTML(original_identifier) %>]</title>
|
174
|
+
<style>
|
175
|
+
<%= css.split("\n").join("\n ") %>
|
176
|
+
</style>
|
177
|
+
</head>
|
178
|
+
<body>
|
179
|
+
<header>
|
180
|
+
<div id="logo"></div>
|
181
|
+
<h1>Api Viewer - <%= CGI::escapeHTML(original_identifier) %></h1>
|
182
|
+
</header>
|
183
|
+
<section id="content">
|
184
|
+
<nav>
|
185
|
+
<section id="representations">
|
186
|
+
<h2>Representations:</h2>
|
187
|
+
<ul>
|
188
|
+
<% media_types.each do |m| %>
|
189
|
+
<li>
|
190
|
+
<a href="<%= m[:href] %>" <%= m[:selected] ? 'class="active" ' : '' %>>
|
191
|
+
<%= CGI::escapeHTML(m[:identifier]) %>
|
192
|
+
</a>
|
193
|
+
</li>
|
194
|
+
<% end %>
|
195
|
+
</ul>
|
196
|
+
<hr>
|
197
|
+
</section>
|
198
|
+
<section id="links">
|
199
|
+
<span class="label">Links: </span>
|
200
|
+
<ul>
|
201
|
+
<% api_fied_links.each do |l| %>
|
202
|
+
<li><a <% if l[:invalid] %> style="color: red" <% end %>href="<%= l[:href] %>"><%= CGI::escapeHTML(l[:rel].to_s) %></a></li>
|
203
|
+
<% end %>
|
204
|
+
</ul>
|
205
|
+
</section>
|
206
|
+
</nav>
|
207
|
+
<% if allowed_replies.any? %>
|
208
|
+
<section id="reply">
|
209
|
+
<details>
|
210
|
+
<summary>Reply</summary>
|
211
|
+
<div class="reply-indent">
|
212
|
+
<noscript>Javascript is required to submit custom responses back to the server</noscript>
|
213
|
+
<form id="reply-form" hidden>
|
214
|
+
<div class="form-table">
|
215
|
+
<label class="form-row">
|
216
|
+
<div class="cell label">Method:</div>
|
217
|
+
<% if allowed_replies.keys.count == 1 %>
|
218
|
+
<input type="hidden" name="method" value="<%= allowed_replies.keys[0] %>">
|
219
|
+
<div class="cell"><%= allowed_replies.keys[0] %></div>
|
220
|
+
<% else %>
|
221
|
+
<select class="cell" name="method">
|
222
|
+
<% allowed_replies.keys.each do |method| %>
|
223
|
+
<option value="<%= method %>"><%= method %></option>
|
224
|
+
<% end %>
|
225
|
+
</select>
|
226
|
+
<% end %>
|
227
|
+
</label>
|
228
|
+
<label class="form-row"><div class="cell label">Send:</div> <select class="cell" name="request-content-type"></select></label>
|
229
|
+
<label class="form-row"><div class="cell label">Receive:</div> <select class="cell" name="response-content-type"></select></label>
|
230
|
+
</div>
|
231
|
+
<textarea name="request-content"></textarea>
|
232
|
+
<% if token %>
|
233
|
+
<input type="hidden" name="authenticity_token" value="<%= token %>">
|
234
|
+
<% end %>
|
235
|
+
<input type="submit" name="submit" value="Reply"><span id="reply-status-code" hidden> - sending...</span>
|
236
|
+
<hr>
|
237
|
+
<code id="reply-response" hidden>
|
238
|
+
</code>
|
239
|
+
</form>
|
240
|
+
<script>
|
241
|
+
{
|
242
|
+
const form = document.getElementById("reply-form");
|
243
|
+
form.removeAttribute('hidden');
|
244
|
+
|
245
|
+
const actionData = JSON.parse("<%= escape_javascript.call(allowed_replies.to_json) %>");
|
246
|
+
|
247
|
+
const methodElem = form.elements["method"];
|
248
|
+
const requestTypeElem = form.elements["request-content-type"];
|
249
|
+
const responseTypeElem = form.elements["response-content-type"];
|
250
|
+
const contentElem = form.elements["request-content"];
|
251
|
+
const submitElem = form.elements["submit"];
|
252
|
+
const tokenElem = form.elements["authenticity_token"];
|
253
|
+
const replyResponseElem = document.getElementById("reply-response");
|
254
|
+
const replyStatusCodeElem = document.getElementById("reply-status-code");
|
255
|
+
|
256
|
+
const selectRequestType = function selectRequestType() {
|
257
|
+
const selected = requestTypeElem.value;
|
258
|
+
|
259
|
+
if (selected == "") {
|
260
|
+
contentElem.setAttribute("hidden", "");
|
261
|
+
} else {
|
262
|
+
contentElem.removeAttribute("hidden");
|
263
|
+
}
|
264
|
+
|
265
|
+
if (methodElem.value == "PUT" && contentElem.value.trim() == "") {
|
266
|
+
const currentRequestType = document.querySelector("#representations .active").textContent.trim()
|
267
|
+
|
268
|
+
if (currentRequestType == requestTypeElem.value) {
|
269
|
+
const outputElem = document.getElementById("output")
|
270
|
+
contentElem.value = outputElem.
|
271
|
+
textContent.
|
272
|
+
trim().
|
273
|
+
replaceAll(String.fromCharCode(160), " ");
|
274
|
+
}
|
275
|
+
}
|
276
|
+
}
|
277
|
+
|
278
|
+
const selectMethod = function selectMethod() {
|
279
|
+
const selected = methodElem.value
|
280
|
+
submitElem.setAttribute("value", selected)
|
281
|
+
|
282
|
+
const mediatypes = actionData[selected]
|
283
|
+
|
284
|
+
while(requestTypeElem.firstChild) {
|
285
|
+
requestTypeElem.removeChild(requestTypeElem.lastChild)
|
286
|
+
}
|
287
|
+
|
288
|
+
mediatypes["input"].forEach((mediatype) => {
|
289
|
+
const option = document.createElement("option");
|
290
|
+
option.setAttribute("value", mediatype);
|
291
|
+
option.textContent = mediatype;
|
292
|
+
|
293
|
+
requestTypeElem.appendChild(option);
|
294
|
+
})
|
295
|
+
|
296
|
+
const noneOption = document.createElement("option");
|
297
|
+
noneOption.setAttribute("value", "");
|
298
|
+
noneOption.textContent = "None";
|
299
|
+
requestTypeElem.appendChild(noneOption);
|
300
|
+
|
301
|
+
while(responseTypeElem.firstChild) {
|
302
|
+
responseTypeElem.removeChild(responseTypeElem.lastChild);
|
303
|
+
}
|
304
|
+
|
305
|
+
mediatypes["output"].forEach((mediatype) => {
|
306
|
+
const option = document.createElement("option");
|
307
|
+
option.setAttribute("value", mediatype);
|
308
|
+
option.textContent = mediatype;
|
309
|
+
|
310
|
+
responseTypeElem.appendChild(option);
|
311
|
+
})
|
312
|
+
|
313
|
+
const anyOption = document.createElement("option");
|
314
|
+
anyOption.setAttribute("value", "");
|
315
|
+
anyOption.textContent = "Any";
|
316
|
+
responseTypeElem.appendChild(anyOption);
|
317
|
+
|
318
|
+
selectRequestType();
|
319
|
+
}
|
320
|
+
|
321
|
+
const onSubmit = async function onSubmit(event) {
|
322
|
+
event.preventDefault();
|
323
|
+
|
324
|
+
submitElem.setAttribute("disabled", "");
|
325
|
+
|
326
|
+
const method = methodElem.value
|
327
|
+
const requestContentType = requestTypeElem.value
|
328
|
+
const requestContent = contentElem.value
|
329
|
+
const token = tokenElem.value
|
330
|
+
|
331
|
+
let responseAccept = responseTypeElem.value + ", application/problem+json; q=0.2, */*; q=0.1"
|
332
|
+
if (responseTypeElem.value == "") {
|
333
|
+
responseAccept = "application/problem+json, */*; q=0.1"
|
334
|
+
}
|
335
|
+
|
336
|
+
const headers = {
|
337
|
+
Accept: responseAccept,
|
338
|
+
"X-Requested-With": "XMLHttpRequest"
|
339
|
+
}
|
340
|
+
|
341
|
+
|
342
|
+
if (method !== "GET" && token) {
|
343
|
+
headers["X-CSRF-Token"] = token
|
344
|
+
}
|
345
|
+
|
346
|
+
if (method == "PUT") {
|
347
|
+
const etag = "<%= escape_javascript.call(etag) %>"
|
348
|
+
if (etag != "") {
|
349
|
+
headers['If-Match'] = etag
|
350
|
+
}
|
351
|
+
}
|
352
|
+
|
353
|
+
let body = undefined
|
354
|
+
if (requestContentType != "") {
|
355
|
+
headers["Content-Type"] = requestContentType
|
356
|
+
body = requestContent
|
357
|
+
}
|
358
|
+
|
359
|
+
replyResponseElem.textContent = ""
|
360
|
+
replyStatusCodeElem.textContent = " - sending..."
|
361
|
+
replyStatusCodeElem.removeAttribute("hidden")
|
362
|
+
|
363
|
+
try {
|
364
|
+
const response = await fetch("<%= escape_javascript.call(unviewered_uri.to_s) %>", {
|
365
|
+
method: method,
|
366
|
+
mode: "same-origin",
|
367
|
+
credentials: "same-origin",
|
368
|
+
redirect: "follow",
|
369
|
+
headers: headers,
|
370
|
+
body: body
|
371
|
+
});
|
372
|
+
|
373
|
+
replyStatusCodeElem.textContent = " - Status " + response.status + " " + response.statusText;
|
374
|
+
replyResponseElem.removeAttribute("hidden");
|
375
|
+
replyResponseElem.textContent = await response.text();
|
376
|
+
replyResponseElem.innerHTML = replyResponseElem.
|
377
|
+
innerHTML.
|
378
|
+
replaceAll("\\n", "<br>\\n").
|
379
|
+
replaceAll(" ", " ");
|
380
|
+
} catch (error) {
|
381
|
+
replyStatusCodeElem.textContent = " - Failed: " + error.message
|
382
|
+
} finally {
|
383
|
+
submitElem.removeAttribute("disabled");
|
384
|
+
}
|
385
|
+
}
|
386
|
+
|
387
|
+
requestTypeElem.addEventListener("change", () => selectRequestType());
|
388
|
+
methodElem.addEventListener("change", () => selectMethod());
|
389
|
+
form.addEventListener("submit", (e) => onSubmit(e));
|
390
|
+
|
391
|
+
addEventListener("DOMContentLoaded", (event) => selectMethod());
|
392
|
+
}
|
393
|
+
</script>
|
394
|
+
</div>
|
395
|
+
</details>
|
396
|
+
</section>
|
397
|
+
<% end %>
|
398
|
+
<main>
|
399
|
+
<code id="output">
|
400
|
+
<%= escaped_output %>
|
401
|
+
</code>
|
402
|
+
</main>
|
403
|
+
</section>
|
404
|
+
<!-- API viewer made with ❤ by: https://delftsolutions.com -->
|
405
|
+
</body>
|
406
|
+
</html>
|
407
|
+
HTML
|
408
|
+
template.result(input.instance_eval { binding })
|
409
|
+
end
|
410
|
+
end
|
411
|
+
end
|
412
|
+
end
|
413
|
+
end
|