roda 3.108.0 → 3.110.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.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/lib/roda/cache.rb +0 -2
  3. data/lib/roda/opts.rb +22 -0
  4. data/lib/roda/plugins/Integer_matcher_max.rb +1 -1
  5. data/lib/roda/plugins/_add_script_name.rb +14 -0
  6. data/lib/roda/plugins/_json.rb +22 -0
  7. data/lib/roda/plugins/_symbol_class_matchers.rb +8 -10
  8. data/lib/roda/plugins/additional_render_engines.rb +5 -1
  9. data/lib/roda/plugins/additional_view_directories.rb +7 -3
  10. data/lib/roda/plugins/assets.rb +179 -151
  11. data/lib/roda/plugins/assets_preloading.rb +26 -10
  12. data/lib/roda/plugins/autoload_hash_branches.rb +9 -3
  13. data/lib/roda/plugins/autoload_named_routes.rb +9 -3
  14. data/lib/roda/plugins/backtracking_array.rb +2 -2
  15. data/lib/roda/plugins/branch_locals.rb +4 -4
  16. data/lib/roda/plugins/break.rb +2 -2
  17. data/lib/roda/plugins/caching.rb +27 -17
  18. data/lib/roda/plugins/capture_erb.rb +19 -12
  19. data/lib/roda/plugins/chunked.rb +42 -34
  20. data/lib/roda/plugins/class_level_routing.rb +10 -5
  21. data/lib/roda/plugins/class_matchers.rb +14 -7
  22. data/lib/roda/plugins/common_logger.rb +27 -5
  23. data/lib/roda/plugins/conditional_sessions.rb +4 -4
  24. data/lib/roda/plugins/content_for.rb +12 -7
  25. data/lib/roda/plugins/content_security_policy.rb +17 -14
  26. data/lib/roda/plugins/cookie_flags.rb +17 -7
  27. data/lib/roda/plugins/cookies.rb +12 -6
  28. data/lib/roda/plugins/csrf.rb +2 -1
  29. data/lib/roda/plugins/custom_block_results.rb +11 -10
  30. data/lib/roda/plugins/custom_matchers.rb +4 -2
  31. data/lib/roda/plugins/default_headers.rb +4 -7
  32. data/lib/roda/plugins/default_status.rb +1 -0
  33. data/lib/roda/plugins/delay_build.rb +1 -1
  34. data/lib/roda/plugins/delegate.rb +7 -6
  35. data/lib/roda/plugins/direct_call.rb +5 -1
  36. data/lib/roda/plugins/disallow_file_uploads.rb +2 -3
  37. data/lib/roda/plugins/drop_body.rb +3 -6
  38. data/lib/roda/plugins/each_part.rb +23 -29
  39. data/lib/roda/plugins/early_hints.rb +4 -4
  40. data/lib/roda/plugins/empty_root.rb +5 -5
  41. data/lib/roda/plugins/environments.rb +11 -15
  42. data/lib/roda/plugins/erb_h.rb +4 -4
  43. data/lib/roda/plugins/error_email.rb +23 -20
  44. data/lib/roda/plugins/error_handler.rb +28 -34
  45. data/lib/roda/plugins/error_mail.rb +31 -16
  46. data/lib/roda/plugins/exception_page.rb +41 -35
  47. data/lib/roda/plugins/filter_common_logger.rb +6 -4
  48. data/lib/roda/plugins/flash.rb +16 -13
  49. data/lib/roda/plugins/freeze_request_response.rb +24 -0
  50. data/lib/roda/plugins/hash_branch_view_subdir.rb +6 -4
  51. data/lib/roda/plugins/hash_branches.rb +7 -5
  52. data/lib/roda/plugins/hash_paths.rb +7 -5
  53. data/lib/roda/plugins/hash_public.rb +11 -7
  54. data/lib/roda/plugins/hash_public_cache.rb +9 -7
  55. data/lib/roda/plugins/hash_routes.rb +6 -4
  56. data/lib/roda/plugins/heartbeat.rb +6 -2
  57. data/lib/roda/plugins/hmac_paths.rb +9 -3
  58. data/lib/roda/plugins/hooks.rb +13 -10
  59. data/lib/roda/plugins/host_authorization.rb +7 -2
  60. data/lib/roda/plugins/host_routing.rb +7 -2
  61. data/lib/roda/plugins/invalid_request_body.rb +20 -4
  62. data/lib/roda/plugins/ip_from_header.rb +5 -1
  63. data/lib/roda/plugins/json.rb +10 -8
  64. data/lib/roda/plugins/json_parser.rb +26 -12
  65. data/lib/roda/plugins/mail_processor.rb +9 -6
  66. data/lib/roda/plugins/mailer.rb +5 -3
  67. data/lib/roda/plugins/match_affix.rb +6 -1
  68. data/lib/roda/plugins/match_hook_args.rb +7 -5
  69. data/lib/roda/plugins/middleware.rb +13 -7
  70. data/lib/roda/plugins/module_include.rb +1 -1
  71. data/lib/roda/plugins/multi_public.rb +6 -2
  72. data/lib/roda/plugins/multi_route.rb +1 -1
  73. data/lib/roda/plugins/multi_run.rb +13 -17
  74. data/lib/roda/plugins/named_routes.rb +8 -6
  75. data/lib/roda/plugins/named_templates.rb +5 -3
  76. data/lib/roda/plugins/path.rb +16 -14
  77. data/lib/roda/plugins/path_rewriter.rb +8 -5
  78. data/lib/roda/plugins/permissions_policy.rb +21 -19
  79. data/lib/roda/plugins/public.rb +22 -10
  80. data/lib/roda/plugins/recheck_precompiled_assets.rb +30 -8
  81. data/lib/roda/plugins/redirect_http_to_https.rb +6 -3
  82. data/lib/roda/plugins/render.rb +37 -49
  83. data/lib/roda/plugins/render_coverage.rb +6 -3
  84. data/lib/roda/plugins/render_locals.rb +13 -7
  85. data/lib/roda/plugins/request_aref.rb +1 -1
  86. data/lib/roda/plugins/response_content_type.rb +7 -3
  87. data/lib/roda/plugins/route_csrf.rb +7 -2
  88. data/lib/roda/plugins/run_append_slash.rb +5 -1
  89. data/lib/roda/plugins/sec_fetch_site_csrf.rb +6 -2
  90. data/lib/roda/plugins/sessions.rb +164 -139
  91. data/lib/roda/plugins/shape_friendly.rb +3 -1
  92. data/lib/roda/plugins/sinatra_helpers.rb +7 -3
  93. data/lib/roda/plugins/status_handler.rb +5 -3
  94. data/lib/roda/plugins/strip_path_prefix.rb +4 -2
  95. data/lib/roda/plugins/symbol_matchers.rb +11 -6
  96. data/lib/roda/plugins/symbol_views.rb +1 -1
  97. data/lib/roda/plugins/timestamp_public.rb +8 -4
  98. data/lib/roda/plugins/type_routing.rb +9 -5
  99. data/lib/roda/plugins/typecast_params.rb +2 -2
  100. data/lib/roda/plugins/typecast_params_sized_integers.rb +1 -1
  101. data/lib/roda/plugins/unescape_path.rb +22 -4
  102. data/lib/roda/plugins/url_escape.rb +2 -14
  103. data/lib/roda/plugins.rb +34 -0
  104. data/lib/roda/request.rb +8 -7
  105. data/lib/roda/response.rb +9 -7
  106. data/lib/roda/version.rb +1 -1
  107. data/lib/roda.rb +37 -23
  108. metadata +7 -3
data/lib/roda.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  # frozen-string-literal: true
2
2
 
3
- require "thread"
4
3
  require_relative "roda/request"
5
4
  require_relative "roda/response"
6
5
  require_relative "roda/plugins"
7
6
  require_relative "roda/cache"
7
+ require_relative "roda/opts"
8
8
  require_relative "roda/version"
9
9
 
10
10
  # The main class for Roda. Roda is built completely out of plugins, with the
@@ -18,7 +18,8 @@ class Roda
18
18
  @inherit_middleware = true
19
19
  @middleware = []
20
20
  @plugins = []
21
- @opts = {}
21
+ @opts = RodaOpts.new
22
+ @opts.roda_class = self
22
23
  @raw_route_block = nil
23
24
  @route_block = nil
24
25
  @rack_app_route_block = nil
@@ -30,6 +31,8 @@ class Roda
30
31
  module Base
31
32
  # Class methods for the Roda class.
32
33
  module ClassMethods
34
+ RodaPlugins.opt_attr_reader(self, :root, name: :app_root)
35
+
33
36
  # The rack application that this class uses.
34
37
  def app
35
38
  @app || build_rack_app
@@ -92,6 +95,7 @@ class Roda
92
95
  # Support for the :check_arity and :check_dynamic_arity options will be
93
96
  # removed in Roda 4.
94
97
  def define_roda_method(meth, expected_arity, &block)
98
+ # RODA4: Make expected_arity optional, remove from callers
95
99
  if meth.is_a?(String)
96
100
  meth = roda_method_name(meth)
97
101
  end
@@ -140,7 +144,7 @@ class Roda
140
144
  # Fallback to instance_exec in this case.
141
145
  b = block
142
146
  block = if RUBY_VERSION >= '2.7'
143
- eval('lambda{|*a, **kw| instance_exec(*a, **kw, &b)}', nil, __FILE__, __LINE__) # Keyword arguments fallback
147
+ lambda{|*a, **kw| instance_exec(*a, **kw, &b)} # Keyword arguments fallback
144
148
  else
145
149
  # simplecov:disable
146
150
  lambda{|*a| instance_exec(*a, &b)} # Keyword arguments fallback
@@ -189,7 +193,7 @@ class Roda
189
193
  end
190
194
 
191
195
  # Expand the given path, using the root argument as the base directory.
192
- def expand_path(path, root=opts[:root])
196
+ def expand_path(path, root=app_root)
193
197
  ::File.expand_path(path, root)
194
198
  end
195
199
 
@@ -207,9 +211,9 @@ class Roda
207
211
  # If the _roda_run_main_route instance method has not been overridden,
208
212
  # make it an alias to _roda_main_route for performance
209
213
  if instance_method(:_roda_run_main_route).owner == InstanceMethods
210
- class_eval("alias _roda_run_main_route _roda_main_route")
214
+ alias_method :_roda_run_main_route, :_roda_main_route
211
215
  end
212
- self::RodaResponse.class_eval do
216
+ self::RodaResponse.class_exec do
213
217
  if instance_method(:set_default_headers).owner == ResponseMethods &&
214
218
  instance_method(:default_headers).owner == ResponseMethods
215
219
 
@@ -253,21 +257,28 @@ class Roda
253
257
  def inherited(subclass)
254
258
  raise RodaError, "Cannot subclass a frozen Roda class" if frozen?
255
259
 
256
- # Mark current class as having been subclassed, as some optimizations
257
- # depend on the class not being subclassed
258
- opts[:subclassed] = true
259
-
260
260
  super
261
261
  subclass.instance_variable_set(:@inherit_middleware, @inherit_middleware)
262
262
  subclass.instance_variable_set(:@middleware, @inherit_middleware ? @middleware.dup : [])
263
263
  subclass.instance_variable_set(:@plugins, @plugins.dup)
264
- subclass.instance_variable_set(:@opts, opts.dup)
265
- subclass.opts.delete(:subclassed)
266
- subclass.opts.to_a.each do |k,v|
267
- if (v.is_a?(Array) || v.is_a?(Hash)) && !v.frozen?
268
- subclass.opts[k] = v.dup
264
+
265
+ opts = RodaOpts.new
266
+ opts.roda_class = subclass
267
+ @opts.each do |k,v|
268
+ opts[k] = if k == :subclassed
269
+ false
270
+ elsif (v.is_a?(Array) || v.is_a?(Hash)) && !v.frozen?
271
+ v.dup
272
+ else
273
+ v
269
274
  end
270
275
  end
276
+ subclass.instance_variable_set(:@opts, opts)
277
+
278
+ # Mark current class as having been subclassed, as some optimizations
279
+ # depend on the class not being subclassed
280
+ @opts[:subclassed] = true
281
+
271
282
  if block = @raw_route_block
272
283
  subclass.route(&block)
273
284
  end
@@ -290,15 +301,14 @@ class Roda
290
301
  # class has been subclassed, as doing so can break the subclasses.
291
302
  #
292
303
  # Roda.plugin PluginModule
293
- # Roda.plugin :csrf
304
+ # Roda.plugin :route_csrf
294
305
  def plugin(plugin, *args, &block)
295
306
  raise RodaError, "Cannot add a plugin to a frozen Roda class" if frozen?
296
307
  plugin = RodaPlugins.load_plugin(plugin) if plugin.is_a?(Symbol)
297
308
  raise RodaError, "Invalid plugin type: #{plugin.class.inspect}" unless plugin.is_a?(Module)
298
309
 
299
310
  if !plugin.respond_to?(:load_dependencies) && !plugin.respond_to?(:configure) && (!args.empty? || block)
300
- # RODA4: switch from warning to error
301
- RodaPlugins.warn("Plugin #{plugin} does not accept arguments or a block, but arguments or a block was passed when loading this. This will raise an error in Roda 4.")
311
+ RodaPlugins.warn("Plugin #{plugin} does not accept arguments or a block, but arguments or a block was passed when loading this. This will raise an error in Roda 4.", uplevel: 1)
302
312
  end
303
313
 
304
314
  plugin.load_dependencies(self, *args, &block) if plugin.respond_to?(:load_dependencies)
@@ -331,7 +341,7 @@ class Roda
331
341
  # times will overwrite the previous routing.
332
342
  def route(&block)
333
343
  unless block
334
- RodaPlugins.warn "no block passed to Roda.route"
344
+ RodaPlugins.warn("no block passed to Roda.route", uplevel: 1)
335
345
  return
336
346
  end
337
347
 
@@ -342,8 +352,7 @@ class Roda
342
352
  @app = nil
343
353
  end
344
354
 
345
- # Add a middleware to use for the rack application. Must be
346
- # called before calling #route to have an effect. Example:
355
+ # Add a middleware to use for the rack application. Example:
347
356
  #
348
357
  # Roda.use Rack::ShowExceptions
349
358
  def use(*args, &block)
@@ -361,6 +370,7 @@ class Roda
361
370
  # and whether the callable accepts keyword arguments (true, false
362
371
  # or :required).
363
372
  def _define_roda_method_arg_numbers(callable)
373
+ # RODA4: Remove
364
374
  optional_args = 0
365
375
  rest = false
366
376
  keyword = false
@@ -392,7 +402,9 @@ class Roda
392
402
 
393
403
  # The base rack app to use, before middleware is added.
394
404
  def base_rack_app_callable(new_api=true)
405
+ # RODA4.1: Remove
395
406
  if new_api
407
+ # RODA4: Inline this behavior
396
408
  lambda{|env| new(env)._roda_handle_main_route}
397
409
  else
398
410
  block = @rack_app_route_block
@@ -417,6 +429,7 @@ class Roda
417
429
  # which can include route blocks that are delegated to by the main route block.
418
430
  # Can be modified by plugins.
419
431
  def convert_route_block(block)
432
+ # RODA4.1: Remove
420
433
  block
421
434
  end
422
435
 
@@ -460,6 +473,7 @@ class Roda
460
473
  # if any before hooks are defined.
461
474
  # Can be modified by plugins.
462
475
  def rack_app_route_block(block)
476
+ # RODA4.1: Remove
463
477
  block
464
478
  end
465
479
 
@@ -470,7 +484,7 @@ class Roda
470
484
  break if mod == InstanceMethods
471
485
  meths = mod.instance_methods(false)
472
486
  if meths.include?(:call) && !(meths.include?(:_roda_handle_main_route) || meths.include?(:_roda_run_main_route))
473
- RodaPlugins.warn <<WARNING
487
+ RodaPlugins.warn(<<WARNING, uplevel: 1)
474
488
  Falling back to using #call for dispatching for #{self}, due to #call override in #{mod}.
475
489
  #{mod} should be fixed to adjust to Roda's new dispatch API, and override _roda_handle_main_route or _roda_run_main_route
476
490
  WARNING
@@ -556,7 +570,7 @@ WARNING
556
570
  @_request.env
557
571
  end
558
572
 
559
- # The class-level options hash. This should probably not be
573
+ # The class-level options hash. This should not be
560
574
  # modified at the instance level. Example:
561
575
  #
562
576
  # Roda.plugin :render
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roda
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.108.0
4
+ version: 3.110.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Evans
@@ -159,11 +159,14 @@ files:
159
159
  - MIT-LICENSE
160
160
  - lib/roda.rb
161
161
  - lib/roda/cache.rb
162
+ - lib/roda/opts.rb
162
163
  - lib/roda/plugins.rb
163
164
  - lib/roda/plugins/Integer_matcher_max.rb
165
+ - lib/roda/plugins/_add_script_name.rb
164
166
  - lib/roda/plugins/_after_hook.rb
165
167
  - lib/roda/plugins/_base64.rb
166
168
  - lib/roda/plugins/_before_hook.rb
169
+ - lib/roda/plugins/_json.rb
167
170
  - lib/roda/plugins/_optimized_matching.rb
168
171
  - lib/roda/plugins/_symbol_class_matchers.rb
169
172
  - lib/roda/plugins/_symbol_regexp_matchers.rb
@@ -212,6 +215,7 @@ files:
212
215
  - lib/roda/plugins/exception_page.rb
213
216
  - lib/roda/plugins/filter_common_logger.rb
214
217
  - lib/roda/plugins/flash.rb
218
+ - lib/roda/plugins/freeze_request_response.rb
215
219
  - lib/roda/plugins/h.rb
216
220
  - lib/roda/plugins/halt.rb
217
221
  - lib/roda/plugins/hash_branch_view_subdir.rb
@@ -334,14 +338,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
334
338
  requirements:
335
339
  - - ">="
336
340
  - !ruby/object:Gem::Version
337
- version: 1.9.2
341
+ version: '2.0'
338
342
  required_rubygems_version: !ruby/object:Gem::Requirement
339
343
  requirements:
340
344
  - - ">="
341
345
  - !ruby/object:Gem::Version
342
346
  version: '0'
343
347
  requirements: []
344
- rubygems_version: 4.0.16
348
+ rubygems_version: 4.0.20
345
349
  specification_version: 4
346
350
  summary: Routing tree web toolkit
347
351
  test_files: []