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
@@ -11,11 +11,11 @@ class Roda
11
11
  # For a list of compatible browsers, see
12
12
  # http://caniuse.com/#search=link-rel-preload
13
13
  #
14
- # The plugin provides two functions - preload_assets_link_header and
15
- # preload_assets_link_tags. The resulting preloading should be
16
- # identical, it is up to you which system you prefer.
14
+ # The plugin provides two functions: +preload_assets_link_header+ and
15
+ # +preload_assets_link_tags+. The resulting preloading should be
16
+ # identical, it is up to you which approach you prefer.
17
17
  #
18
- # preload_assets_link_header returns a string suitable for populating
18
+ # +preload_assets_link_header+ returns a string suitable for populating
19
19
  # the response Link header:
20
20
  #
21
21
  # response.headers['Link'] = preload_assets_link_header(:css)
@@ -43,7 +43,6 @@ class Roda
43
43
  #
44
44
  # # generate Link header for css assets and js asset groups app and bar
45
45
  # preload_assets_link_header(:css, [:js, :app])
46
- #
47
46
  module AssetsPreloading
48
47
  TYPE_AS = {
49
48
  :css => 'style'.freeze,
@@ -59,27 +58,44 @@ class Roda
59
58
  # Return a string of <link> tags for the given asset
60
59
  # types/groups.
61
60
  def preload_assets_link_tags(*args)
62
- _preload_assets_array(args).map{|path, as| "<link href=\"#{h(path)}\" rel=\"preload\" as=\"#{as}\">"}.join("\n")
61
+ _each_preload_asset(args, "\n") do |path, as|
62
+ "<link href=\"#{h(path)}\" rel=\"preload\" as=\"#{as}\">"
63
+ end
63
64
  end
64
65
 
65
66
  # Return a string suitable for a Link header for the
66
67
  # given asset types/groups.
67
68
  def preload_assets_link_header(*args)
68
- _preload_assets_array(args).map{|path, as| "<#{path}>;rel=preload;as=#{as}"}.join(",")
69
+ _each_preload_asset(args, ",") do |path, as|
70
+ "<#{path}>;rel=preload;as=#{as}"
71
+ end
69
72
  end
70
73
 
71
74
  private
72
75
 
73
76
  # Return an array of paths/as pairs for the given asset
74
77
  # types and/or groups.
75
- def _preload_assets_array(assets)
76
- assets.flat_map do |type|
78
+ def _each_preload_asset(assets, sep)
79
+ output = String.new
80
+ append_sep = false
81
+
82
+ assets.each do |type|
83
+ if append_sep
84
+ output << sep
85
+ else
86
+ append_sep = sep
87
+ end
88
+
77
89
  paths = assets_paths(type)
78
90
  type = type[0] if type.is_a?(Array)
79
91
  as = TYPE_AS[type]
80
92
 
81
- paths.map{|path| [path, as]}
93
+ paths.each do |path|
94
+ output << yield(path, as)
95
+ end
82
96
  end
97
+
98
+ output
83
99
  end
84
100
  end
85
101
  end
@@ -38,13 +38,15 @@ class Roda
38
38
  end
39
39
 
40
40
  module ClassMethods
41
+ RodaPlugins.opt_attr_reader(self, :autoload_hash_branch_files)
42
+
41
43
  # Autoload the given file when there is request for the hash branch.
42
44
  # The given file should configure the hash branch specified.
43
45
  def autoload_hash_branch(namespace='', segment, file)
44
46
  segment = "/#{segment}"
45
47
  file = File.expand_path(file)
46
- opts[:autoload_hash_branch_files] << file
47
- routes = opts[:hash_branches][namespace] ||= {}
48
+ autoload_hash_branch_files << file
49
+ routes = hash_branches[namespace] ||= {}
48
50
  meth = routes[segment] = define_roda_method(routes[segment] || "hash_branch_#{namespace}_#{segment}", 1) do |r|
49
51
  loc = method(routes[segment]).source_location
50
52
  require file
@@ -68,7 +70,11 @@ class Roda
68
70
 
69
71
  # Eagerly load all hash branches when freezing the application.
70
72
  def freeze
71
- opts.delete(:autoload_hash_branch_files).each{|file| require file} unless opts.frozen?
73
+ unless opts.frozen?
74
+ autoload_hash_branch_files.each{|file| require file}
75
+ opts[:autoload_hash_branch_files] = nil
76
+ opts.delete(:autoload_hash_branch_files)
77
+ end
72
78
  super
73
79
  end
74
80
  end
@@ -35,12 +35,14 @@ class Roda
35
35
  end
36
36
 
37
37
  module ClassMethods
38
+ RodaPlugins.opt_attr_reader(self, :autoload_named_route_files)
39
+
38
40
  # Autoload the given file when there is request for the named route.
39
41
  # The given file should configure the named route specified.
40
42
  def autoload_named_route(namespace=nil, name, file)
41
43
  file = File.expand_path(file)
42
- opts[:autoload_named_route_files] << file
43
- routes = opts[:namespaced_routes][namespace] ||= {}
44
+ autoload_named_route_files << file
45
+ routes = namespaced_routes[namespace] ||= {}
44
46
  meth = routes[name] = define_roda_method(routes[name] || "named_routes_#{namespace}_#{name}", 1) do |r|
45
47
  loc = method(routes[name]).source_location
46
48
  require file
@@ -54,7 +56,11 @@ class Roda
54
56
 
55
57
  # Eagerly load all autoloaded named routes when freezing the application.
56
58
  def freeze
57
- opts.delete(:autoload_named_route_files).each{|file| require file} unless opts.frozen?
59
+ unless opts.frozen?
60
+ autoload_named_route_files.each{|file| require file}
61
+ opts[:autoload_named_route_files] = nil
62
+ opts.delete(:autoload_named_route_files)
63
+ end
58
64
  super
59
65
  end
60
66
  end
@@ -16,8 +16,8 @@ class Roda
16
16
  # This is because the <tt>'a'</tt> entry in the array matches, which
17
17
  # makes the array match. However, the next matcher is the
18
18
  # terminal matcher (since +r.is+ was used), and since the
19
- # path is not terminal as it still contains +/b+ after
20
- # matching <tt>'a'</tt>.
19
+ # remaining path is not empty as it still contains +/b+ after
20
+ # matching <tt>'a'</tt>, matching fails.
21
21
  #
22
22
  # With the backtracking_array plugin, when the terminal matcher
23
23
  # fails, matching will go on to the next entry in the array,
@@ -17,9 +17,9 @@ class Roda
17
17
  # end
18
18
  # end
19
19
  #
20
- # The locals you specify in the set_layout_locals and set_view_locals methods
20
+ # The locals you specify in the +set_layout_locals+ and +set_view_locals+ methods
21
21
  # have higher precedence than the render_locals plugin options, but lower precedence
22
- # than options you directly pass to the view/render methods.
22
+ # than options you directly pass to the +view+/+render+ methods.
23
23
  module BranchLocals
24
24
  SCOPE_INSTANCE_VARIABLES = [:@_layout_locals, :@_view_locals].freeze
25
25
 
@@ -54,7 +54,7 @@ class Roda
54
54
  def render_locals
55
55
  locals = super
56
56
  if @_view_locals
57
- locals = Hash[locals].merge!(@_view_locals)
57
+ locals = locals.merge(@_view_locals)
58
58
  end
59
59
  locals
60
60
  end
@@ -63,7 +63,7 @@ class Roda
63
63
  def layout_locals
64
64
  locals = super
65
65
  if @_layout_locals
66
- locals = Hash[locals].merge!(@_layout_locals)
66
+ locals = locals.merge(@_layout_locals)
67
67
  end
68
68
  locals
69
69
  end
@@ -3,7 +3,7 @@
3
3
  #
4
4
  class Roda
5
5
  module RodaPlugins
6
- # The break plugin supports calling break inside a match block, to
6
+ # The break plugin supports calling +break+ inside a match block, to
7
7
  # return from the block and continue in the routing tree, restoring
8
8
  # the remaining path so that future matchers operating on the path
9
9
  # operate as expected.
@@ -28,7 +28,7 @@ class Roda
28
28
  module RequestMethods
29
29
  private
30
30
 
31
- # Handle passing inside the match block.
31
+ # Handle break inside match blocks, restoring remaining path.
32
32
  def always
33
33
  rp = @remaining_path
34
34
  super
@@ -100,33 +100,31 @@ class Roda
100
100
  end
101
101
  end
102
102
 
103
- # Set the response entity tag using the ETag header.
103
+ # Set the response entity tag using the +ETag+ header.
104
104
  #
105
105
  # The +value+ argument is an identifier that uniquely
106
106
  # identifies the current version of the resource.
107
- # Options:
108
- # :weak :: Use a weak cache validator (a strong cache validator is the default)
109
- # :new_resource :: Whether this etag should match an etag of * (true for POST, false otherwise)
107
+ # Keyword Arguments:
108
+ # +weak+ :: Use a weak cache validator (a strong cache validator is the default)
109
+ # +new_resource+ :: Whether this etag should match an etag of * (true for POST, false otherwise)
110
110
  #
111
- # When the current request includes an If-None-Match header with a
111
+ # When the current request includes an <tt>If-None-Match</tt> header with a
112
112
  # matching etag, immediately returns a response with a 304 or 412 status,
113
113
  # depending on the request method.
114
114
  #
115
- # When the current request includes an If-Match header with an etag that
116
- # doesn't match, or where the :weak option was used, immediately returns a
115
+ # When the current request includes an <tt>If-Match</tt> header with an etag that
116
+ # doesn't match, or where the +weak+ argument was used, immediately returns a
117
117
  # response with a 412 status.
118
- def etag(value, opts=OPTS)
119
- # Before touching this code, please double check RFC 2616 14.24 and 14.26.
120
- weak = opts[:weak]
121
- new_resource = opts.fetch(:new_resource){post?}
122
-
118
+ def etag(value, opts=(no_positional_opts = true; OPTS), weak: opts[:weak], new_resource: opts.fetch(:new_resource){post?}, **kw)
119
+ RodaPlugins.check_keywords(__callee__, no_positional_opts, opts, kw)
120
+
123
121
  res = response
124
122
  e = env
125
123
  res[RodaResponseHeaders::ETAG] = etag = "#{'W/' if weak}\"#{value}\""
126
124
  status = res.status
127
125
 
128
126
  if (!status || (status >= 200 && status < 300) || status == 304)
129
- if etag_matches?(e['HTTP_IF_NONE_MATCH'], etag, new_resource)
127
+ if etag_matches?(e['HTTP_IF_NONE_MATCH'], etag, new_resource, weak: true)
130
128
  res.status = (request_method =~ /\AGET|HEAD|OPTIONS|TRACE\z/i ? 304 : 412)
131
129
  halt
132
130
  end
@@ -143,10 +141,21 @@ class Roda
143
141
  private
144
142
 
145
143
  # Helper method checking if a ETag value list includes the current ETag.
146
- def etag_matches?(list, etag, new_resource)
144
+ # If +weak+ is true, uses the weak comparison that <tt>If-None-Match</tt> requires
145
+ # (RFC 9110 section 13.1.2), which ignores a W/ prefix on both sides.
146
+ def etag_matches?(list, etag, new_resource, weak: false)
147
147
  return unless list
148
148
  return !new_resource if list == '*'
149
- list.to_s.split(/\s*,\s*/).include?(etag)
149
+ list = list.to_s.split(/\s*,\s*/)
150
+ if weak
151
+ etag = etag.sub(/\AW\//, '')
152
+ list.any? do |tag|
153
+ tag.sub!(/\AW\//, '')
154
+ tag == etag
155
+ end
156
+ else
157
+ list.include?(etag)
158
+ end
150
159
  end
151
160
 
152
161
  # Helper method parsing a time value from an HTTP header, returning the
@@ -154,11 +163,12 @@ class Roda
154
163
  def time_from_header(t)
155
164
  Time.httpdate(t).to_i if t
156
165
  rescue ArgumentError
166
+ nil
157
167
  end
158
168
  end
159
169
 
160
170
  module ResponseMethods
161
- # Specify response freshness policy for using the Cache-Control header.
171
+ # Specify response freshness policy for using the <tt>Cache-Control</tt> header.
162
172
  # Options can can any non-value directives (:public, :private, :no_cache,
163
173
  # :no_store, :must_revalidate, :proxy_revalidate), with true as the value.
164
174
  # Options can also contain value directives (:max_age, :s_maxage).
@@ -184,7 +194,7 @@ class Roda
184
194
  # cached for. Also sets the Expires header, useful if you have
185
195
  # HTTP 1.0 clients (Cache-Control is an HTTP 1.1 header).
186
196
  def expires(max_age, opts=OPTS)
187
- cache_control(Hash[opts].merge!(:max_age=>max_age))
197
+ cache_control(opts.merge(:max_age=>max_age))
188
198
  @headers[RodaResponseHeaders::EXPIRES] = (Time.now + max_age).httpdate
189
199
  end
190
200
 
@@ -34,7 +34,7 @@ class Roda
34
34
  #
35
35
  # In this case, the block may return nil, instead of the content of
36
36
  # the template. To handle this case, you can provide the
37
- # <tt>returns: :buffer</tt> option when calling the method (to handle
37
+ # <tt>returns: :buffer</tt> argument when calling the method (to handle
38
38
  # that specific call, or when loading the plugin (to default to that
39
39
  # behavior). Note that if the output buffer object responds to
40
40
  # +capture+ and is not an instance of String, the <tt>returns: :buffer</tt>
@@ -46,9 +46,14 @@ class Roda
46
46
 
47
47
  # Support <tt>returns: :buffer</tt> to default to returning buffer
48
48
  # object.
49
- def self.configure(app, opts=OPTS)
49
+ def self.configure(app, opts=(no_positional_opts = true; OPTS), returns: (opts.has_key?(:returns) ? opts[:returns] : (returns_not_set = true; nil)), **kw)
50
+ RodaPlugins.check_keywords("the capture_erb plugin", no_positional_opts, opts, kw)
50
51
  # RODA4: make returns: :buffer the default behavior
51
- app.opts[:capture_erb_returns] = opts[:returns] if opts.has_key?(:returns)
52
+ app.opts[:capture_erb_returns] = returns unless returns_not_set
53
+ end
54
+
55
+ module ClassMethods
56
+ RodaPlugins.opt_attr_reader(self, :capture_erb_returns)
52
57
  end
53
58
 
54
59
  module InstanceMethods
@@ -57,21 +62,23 @@ class Roda
57
62
  # Return the value of the block, converted to a string.
58
63
  # Restore the previous ERB output buffer before returning.
59
64
  #
60
- # Options:
61
- # :returns :: If set to :buffer, returns the value of the
62
- # template output variable, instead of the return
63
- # value of the block converted to a string. This
64
- # is the default behavior if the template output
65
- # variable supports the +capture+ method and is not
66
- # a String instance.
67
- def capture_erb(opts=OPTS, &block)
65
+ # Keyword arguments:
66
+ # +returns+ :: If set to :buffer, returns the value of the
67
+ # template output variable, instead of the return
68
+ # value of the block converted to a string. This
69
+ # is the default behavior if the template output
70
+ # variable supports the +capture+ method and is not
71
+ # a String instance.
72
+ def capture_erb(opts=(no_positional_opts = true; OPTS), returns: (opts.has_key?(:returns) ? opts[:returns] : (returns_not_set = true; nil)), **kw, &block)
73
+ RodaPlugins.check_keywords(__callee__, no_positional_opts, opts, kw)
74
+
68
75
  outvar = render_opts[:template_opts][:outvar]
69
76
  buf_was = instance_variable_get(outvar)
70
77
 
71
78
  if buf_was.respond_to?(:capture) && !buf_was.instance_of?(String)
72
79
  buf_was.capture(&block)
73
80
  else
74
- returns = opts.fetch(:returns) { self.opts[:capture_erb_returns] }
81
+ returns = self.class.capture_erb_returns if returns_not_set
75
82
 
76
83
  begin
77
84
  instance_variable_set(outvar, String.new)
@@ -23,7 +23,7 @@ class Roda
23
23
  # handling is reduced, since if an error occurs while rendering a template,
24
24
  # a successful response code has already been sent.
25
25
  #
26
- # To use chunked encoding for a response, just call the chunked method
26
+ # To use chunked encoding for a response, call the +chunked+ method
27
27
  # instead of view:
28
28
  #
29
29
  # r.root do
@@ -32,7 +32,7 @@ class Roda
32
32
  #
33
33
  # If you want to execute code after flushing the top part of the layout
34
34
  # template, but before rendering the content template, pass a block to
35
- # chunked:
35
+ # +chunked+:
36
36
  #
37
37
  # r.root do
38
38
  # chunked(:index) do
@@ -40,7 +40,7 @@ class Roda
40
40
  # end
41
41
  # end
42
42
  #
43
- # You can also call delay manually with a block, and the execution of the
43
+ # You can also call +delay+ manually with a block, and the execution of the
44
44
  # block will be delayed until rendering the content template. This is
45
45
  # useful if you want to delay execution for all routes under a branch:
46
46
  #
@@ -56,12 +56,12 @@ class Roda
56
56
  # end
57
57
  # end
58
58
  #
59
- # If you want to chunk all responses, pass the :chunk_by_default option
59
+ # If you want to chunk all responses, pass the +chunk_by_default+ argument
60
60
  # when loading the plugin:
61
61
  #
62
62
  # plugin :chunked, chunk_by_default: true
63
63
  #
64
- # then you can just use the normal view method:
64
+ # then you can just use the normal +view+ method:
65
65
  #
66
66
  # r.root do
67
67
  # view(:index)
@@ -73,17 +73,17 @@ class Roda
73
73
  # enable chunking by default, you need to make sure that none of your
74
74
  # content templates make state changes that affect the layout template.
75
75
  # Additionally, make sure nowhere in your app are you doing any processing
76
- # after the call to view.
76
+ # after the call to +view+.
77
77
  #
78
- # If you use :chunk_by_default, but want to turn off chunking for a view,
79
- # call no_chunk!:
78
+ # If you use +chunk_by_default+, but want to turn off chunking for a view,
79
+ # call +no_chunk!+:
80
80
  #
81
81
  # r.root do
82
82
  # no_chunk!
83
83
  # view(:index)
84
84
  # end
85
85
  #
86
- # Inside your layout or content templates, you can call the flush method
86
+ # Inside your layout or content templates, you can call the +flush+ method
87
87
  # to flush the current result of the template to the user, useful for
88
88
  # streaming large datasets.
89
89
  #
@@ -103,7 +103,7 @@ class Roda
103
103
  # <% flush %><%= render(:subtemplate) %>
104
104
  #
105
105
  # If you want to use chunked encoding when rendering a template, but don't
106
- # want to use a layout, pass the <tt>layout: false</tt> option to chunked.
106
+ # want to use a layout, pass the <tt>layout: false</tt> option to +chunked+.
107
107
  #
108
108
  # r.root do
109
109
  # chunked(:index, layout: false)
@@ -116,8 +116,8 @@ class Roda
116
116
  # env['rack.logger'].error(e)
117
117
  # end
118
118
  #
119
- # It is possible to set @_out_buf to an error notification and call
120
- # flush to output the message to the client inside handle_chunk_error.
119
+ # It is possible to set +@_out_buf+ to an error notification and call
120
+ # +flush+ to output the message to the client inside handle_chunk_error.
121
121
  #
122
122
  # In order for chunking to work, you must make sure that no proxies between
123
123
  # the application and the client buffer responses.
@@ -125,22 +125,22 @@ class Roda
125
125
  # If you are using nginx and have it set to buffer proxy responses by
126
126
  # default, you can turn this off on a per response basis using the
127
127
  # X-Accel-Buffering header. To set this header or similar headers for
128
- # all chunked responses, pass a :headers option when loading the plugin:
128
+ # all chunked responses, pass a +headers+ argument when loading the plugin:
129
129
  #
130
130
  # plugin :chunked, headers: {'X-Accel-Buffering'=>'no'}
131
131
  #
132
- # By default, this plugin does not use Transfer-Encoding: chunked, it only
132
+ # By default, this plugin does not use <tt>Transfer-Encoding: chunked</tt>, it only
133
133
  # returns a body that will stream the response in chunks. If you would like
134
- # to force the use of Transfer-Encoding: chunked, you can use the
135
- # :force_chunked_encoding plugin option. If using the
136
- # :force_chunked_encoding plugin option, chunking will only be used for
137
- # HTTP/1.1 requests since Transfer-Encoding: chunked is only supported
134
+ # to force the use of <tt>Transfer-Encoding: chunked</tt>, you can use the
135
+ # +force_chunked_encoding+ plugin argument. If using the
136
+ # +force_chunked_encoding+ argument, chunking will only be used for
137
+ # HTTP/1.1 requests since <tt>Transfer-Encoding: chunked</tt> is only supported
138
138
  # in HTTP/1.1 (non-HTTP/1.1 requests will have behavior similar to
139
- # calling no_chunk!).
139
+ # calling +no_chunk!+).
140
140
  #
141
141
  # The chunked plugin requires the render plugin, and only works for
142
142
  # template engines that store their template output variable in
143
- # @_out_buf. Also, it only works if the content template is directly
143
+ # +@_out_buf+. Also, it only works if the content template is directly
144
144
  # injected into the layout template without modification.
145
145
  #
146
146
  # If using the chunked plugin with the flash plugin, make sure you
@@ -155,14 +155,16 @@ class Roda
155
155
  app.plugin :render
156
156
  end
157
157
 
158
- # Set plugin specific options. Options:
159
- # :chunk_by_default :: chunk all calls to view by default
160
- # :headers :: Set default additional headers to use when calling view
161
- def self.configure(app, opts=OPTS)
162
- app.opts[:chunk_by_default] = opts[:chunk_by_default]
163
- app.opts[:force_chunked_encoding] = opts[:force_chunked_encoding]
164
- if opts[:headers]
165
- app.opts[:chunk_headers] = (app.opts[:chunk_headers] || {}).merge(opts[:headers]).freeze
158
+ # Set plugin specific arguments. Keyword arguments:
159
+ # +chunk_by_default+ :: chunk all calls to view by default
160
+ # +force_chunked_encoding+ :: force the use of Transfer-Encoding: chunked for HTTP 1.1 requests
161
+ # +headers+ :: Set default additional headers to use when calling view
162
+ def self.configure(app, opts=(no_positional_opts = true; OPTS), chunk_by_default: opts[:chunk_by_default], force_chunked_encoding: opts[:force_chunked_encoding], headers: opts[:headers], **kw)
163
+ RodaPlugins.check_keywords("the chunked plugin", no_positional_opts, opts, kw)
164
+ app.opts[:chunk_by_default] = chunk_by_default
165
+ app.opts[:force_chunked_encoding] = force_chunked_encoding
166
+ if headers
167
+ app.opts[:chunk_headers] = (app.chunk_headers || {}).merge(headers).freeze
166
168
  end
167
169
  end
168
170
 
@@ -207,6 +209,12 @@ class Roda
207
209
  end
208
210
  end
209
211
 
212
+ module ClassMethods
213
+ RodaPlugins.opt_attr_reader(self, :chunk_by_default, name: :chunk_by_default?)
214
+ RodaPlugins.opt_attr_reader(self, :force_chunked_encoding, name: :force_chunked_encoding?)
215
+ RodaPlugins.opt_attr_reader(self, :chunk_headers)
216
+ end
217
+
210
218
  module InstanceMethods
211
219
  # Disable chunking for the current request. Mostly useful when
212
220
  # chunking is turned on by default.
@@ -217,7 +225,7 @@ class Roda
217
225
  # If chunking by default, call chunked if it hasn't yet been
218
226
  # called and chunking is not specifically disabled.
219
227
  def view(*a)
220
- if opts[:chunk_by_default] && @_chunked != false && !defined?(yield)
228
+ if self.class.chunk_by_default? && @_chunked != false && !defined?(yield)
221
229
  chunked(*a)
222
230
  else
223
231
  super
@@ -228,7 +236,7 @@ class Roda
228
236
  # an overview. If a block is given, it is passed to #delay.
229
237
  def chunked(template, opts=OPTS, &block)
230
238
  if @_chunked.nil?
231
- @_chunked = !self.opts[:force_chunked_encoding] || @_request.http_version == "HTTP/1.1"
239
+ @_chunked = !self.class.force_chunked_encoding? || @_request.http_version == "HTTP/1.1"
232
240
  end
233
241
 
234
242
  if block
@@ -245,7 +253,7 @@ class Roda
245
253
  if opts.empty?
246
254
  opts = template
247
255
  else
248
- opts = Hash[opts].merge!(template)
256
+ opts = opts.merge(template)
249
257
  end
250
258
  end
251
259
 
@@ -255,10 +263,10 @@ class Roda
255
263
 
256
264
  res = response
257
265
  headers = res.headers
258
- if chunk_headers = self.opts[:chunk_headers]
266
+ if chunk_headers = self.class.chunk_headers
259
267
  headers.merge!(chunk_headers)
260
268
  end
261
- if self.opts[:force_chunked_encoding]
269
+ if self.class.force_chunked_encoding?
262
270
  res[RodaResponseHeaders::TRANSFER_ENCODING] = 'chunked'
263
271
  body = Body.new(self)
264
272
  else
@@ -289,7 +297,7 @@ class Roda
289
297
  @_out_buf = String.new
290
298
  end
291
299
 
292
- if layout_opts = view_layout_opts(opts)
300
+ if layout_opts = view_layout_opts(opts)
293
301
  @_out_buf = render_template(layout_opts) do
294
302
  flush
295
303
  run_delayed_blocks
@@ -45,13 +45,16 @@ class Roda
45
45
  #
46
46
  # When using the class_level_routing plugin with nested routes, you may also want to use the
47
47
  # delegate plugin to delegate certain instance methods to the request object, so you don't have
48
- # to continually use +request.+ in your routing blocks.
48
+ # to continually use +request.+ in your routing blocks, or use the r plugin so you can use +r+
49
+ # instead of +request+.
49
50
  #
50
51
  # Note that class level routing is implemented via a simple array of routes, so routing performance
51
52
  # will degrade linearly as the number of routes increases. For best performance, you should use
52
53
  # the normal +route+ class method to define your routing tree. This plugin does make it simpler to
53
54
  # add additional routes after the routing tree has already been defined, though.
54
55
  module ClassLevelRouting
56
+ SCOPE_INSTANCE_VARIABLES = [:@_original_remaining_path].freeze
57
+
55
58
  # Initialize the class_routes array when the plugin is loaded. Also, if the application doesn't
56
59
  # currently have a routing block, setup an empty routing block so that things will still work if
57
60
  # a routing block isn't added.
@@ -60,17 +63,19 @@ class Roda
60
63
  end
61
64
 
62
65
  module ClassMethods
66
+ RodaPlugins.opt_attr_reader(self, :class_level_routes)
67
+
63
68
  # Define routing methods that will store class level routes.
64
- [:root, :on, :is, :get, :post, :delete, :head, :options, :link, :patch, :put, :trace, :unlink].each do |request_meth|
69
+ %i[root on is get post delete head options link patch put trace unlink query].each do |request_meth|
65
70
  define_method(request_meth) do |*args, &block|
66
71
  meth = define_roda_method("class_level_routing_#{request_meth}", :any, &block)
67
- opts[:class_level_routes] << [request_meth, args, meth].freeze
72
+ class_level_routes << [request_meth, args, meth].freeze
68
73
  end
69
74
  end
70
75
 
71
76
  # Freeze the class level routes so that there can be no thread safety issues at runtime.
72
77
  def freeze
73
- opts[:class_level_routes].freeze
78
+ class_level_routes.freeze
74
79
  super
75
80
  end
76
81
  end
@@ -93,7 +98,7 @@ class Roda
93
98
  @_response.status = nil
94
99
  result.replace(_roda_handle_route do
95
100
  r = @_request
96
- opts[:class_level_routes].each do |request_meth, args, meth|
101
+ self.class.class_level_routes.each do |request_meth, args, meth|
97
102
  r.instance_variable_set(:@remaining_path, @_original_remaining_path)
98
103
  r.public_send(request_meth, *args) do |*a|
99
104
  send(meth, *a)
@@ -67,8 +67,8 @@ class Roda
67
67
  # Blocks passed to the class_matchers plugin are evaluated in route
68
68
  # block context.
69
69
  #
70
- # When passing a regexp as a matcher, to class_matcher, you can provide a
71
- # <tt>segment: true</tt> option to speed up the matching on Ruby 2.4+:
70
+ # When passing a regexp as a matcher to class_matcher, you can provide a
71
+ # <tt>segment: true</tt> argument to speed up the matching on Ruby 2.4+:
72
72
  #
73
73
  # symbol_matcher(:employee_id, /E-(\d{6})/, segment: true) do |employee_id|
74
74
  # employee_id.to_i
@@ -93,6 +93,8 @@ class Roda
93
93
  end
94
94
 
95
95
  module ClassMethods
96
+ RodaPlugins.opt_attr_reader(self, :class_matchers)
97
+
96
98
  # Set the matcher and block to use for the given class.
97
99
  # The matcher can be a regexp, registered class matcher, or registered symbol
98
100
  # matcher (if using the symbol_matchers plugin).
@@ -101,10 +103,13 @@ class Roda
101
103
  # If providing a registered class or symbol, the block will be called with the
102
104
  # captures returned by the block for the registered class or symbol, or the regexp
103
105
  # captures if no block was registered with the class or symbol. In either case,
104
- # if a block is given, it should return an array with the captures to yield to
105
- # the match block.
106
- def class_matcher(klass, matcher, opts=OPTS, &block)
107
- _symbol_class_matcher(Class, klass, matcher, block, opts) do |meth, (_, regexp, convert_meth, consume_meth)|
106
+ # if a block is given, it should return an object to yield to the match block,
107
+ # or an array to yield multiple arguments to the match block. If the block returns
108
+ # nil or false, it will be treated as not matching.
109
+ def class_matcher(klass, matcher, opts=(no_positional_opts = true; OPTS), segment: opts[:segment], **kw, &block)
110
+ RodaPlugins.check_keywords(__callee__, no_positional_opts, opts, kw)
111
+
112
+ _symbol_class_matcher(Class, klass, matcher, block, segment: segment) do |meth, (_, regexp, convert_meth, consume_meth)|
108
113
  if regexp
109
114
  if consume_meth == :_consume_single_segment
110
115
  define_method(meth){_consume_single_segment(regexp, convert_meth)}
@@ -115,11 +120,13 @@ class Roda
115
120
  define_method(meth){_consume_segment(convert_meth)}
116
121
  end
117
122
  end
123
+
124
+ nil
118
125
  end
119
126
 
120
127
  # Freeze the class_matchers hash when freezing the app.
121
128
  def freeze
122
- opts[:class_matchers].freeze
129
+ class_matchers.freeze
123
130
  super
124
131
  end
125
132
  end