roda 3.55.0 → 3.58.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +34 -0
- data/doc/conventions.rdoc +14 -11
- data/doc/release_notes/3.56.0.txt +33 -0
- data/doc/release_notes/3.57.0.txt +34 -0
- data/doc/release_notes/3.58.0.txt +16 -0
- data/lib/roda/plugins/chunked.rb +1 -1
- data/lib/roda/plugins/common_logger.rb +12 -1
- data/lib/roda/plugins/cookies.rb +2 -0
- data/lib/roda/plugins/exception_page.rb +20 -4
- data/lib/roda/plugins/filter_common_logger.rb +46 -0
- data/lib/roda/plugins/hash_branch_view_subdir.rb +76 -0
- data/lib/roda/plugins/hash_branches.rb +145 -0
- data/lib/roda/plugins/hash_paths.rb +128 -0
- data/lib/roda/plugins/hash_routes.rb +13 -176
- data/lib/roda/plugins/heartbeat.rb +5 -10
- data/lib/roda/plugins/json_parser.rb +6 -2
- data/lib/roda/plugins/multi_public.rb +8 -0
- data/lib/roda/plugins/multi_route.rb +1 -1
- data/lib/roda/plugins/multi_view.rb +0 -4
- data/lib/roda/plugins/named_routes.rb +1 -2
- data/lib/roda/plugins/not_allowed.rb +13 -0
- data/lib/roda/plugins/public.rb +8 -0
- data/lib/roda/plugins/route_csrf.rb +1 -0
- data/lib/roda/plugins/run_append_slash.rb +1 -1
- data/lib/roda/plugins/run_require_slash.rb +46 -0
- data/lib/roda/plugins/sessions.rb +1 -0
- data/lib/roda/plugins/sinatra_helpers.rb +10 -0
- data/lib/roda/plugins/static.rb +2 -0
- data/lib/roda/plugins/static_routing.rb +1 -1
- data/lib/roda/plugins/status_303.rb +6 -3
- data/lib/roda/plugins/status_handler.rb +35 -9
- data/lib/roda/plugins/symbol_status.rb +2 -0
- data/lib/roda/plugins/unescape_path.rb +2 -0
- data/lib/roda/request.rb +35 -1
- data/lib/roda/response.rb +5 -0
- data/lib/roda/version.rb +1 -1
- metadata +30 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 01acdb87d6bf21ec4137dd2b6c7e3210c5923c43f188a510135c30a66945160a
|
4
|
+
data.tar.gz: e84ddddfbfe23a4be3951c2611f38916cb6dfb60b15c1a6bfd18155869ad4985
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1681a1b8b8eb950be113d1a533c545ed0a71c24dc6d027a41d2cddb1ae99e4a2c2d23fc0e4c84d1f2960fd141021684d37d75e8509b247d732203100eae6ab2f
|
7
|
+
data.tar.gz: a21456321d0ab6f3c32d4ba97dabd31635751caee7889a2b556dd17cc74fb019d974cc06500081a859402cbf8002a26144d4885dbd8c47589852395d1e398ed2
|
data/CHANGELOG
CHANGED
@@ -1,3 +1,37 @@
|
|
1
|
+
= 3.58.0 (2022-07-13)
|
2
|
+
|
3
|
+
* Add filter_common_logger plugin for skipping the logging of certain requests when using the common_logger plugin (jeremyevans)
|
4
|
+
|
5
|
+
* Make exception_page plugin use Exception#detailed_message on Ruby 3.2+ (jeremyevans)
|
6
|
+
|
7
|
+
* Make heartbeat plugin compatible with recent changes in the rack master branch (jeremyevans)
|
8
|
+
|
9
|
+
= 3.57.0 (2022-06-14)
|
10
|
+
|
11
|
+
* Make static_routing plugin depend on the hash_paths instead of the hash_routes plugin (jeremyevans)
|
12
|
+
|
13
|
+
* Split hash_branches and hash_paths plugins from hash_routes plugin (jeremyevans)
|
14
|
+
|
15
|
+
* Hex escape unprintable characters in common_logger plugin output (jeremyevans)
|
16
|
+
|
17
|
+
* Add hash_branch_view_subdir plugin for automatically appending a view subdirectory on a successful hash branch (jeremyevans)
|
18
|
+
|
19
|
+
= 3.56.0 (2022-05-13)
|
20
|
+
|
21
|
+
* Make status_303 plugin use 303 responses for HTTP/2 and higher versions (jeremyevans)
|
22
|
+
|
23
|
+
* Add RodaRequest#http_version for determining the HTTP version in use (jeremyevans)
|
24
|
+
|
25
|
+
* Do not set a body for 405 responses when using the verb methods in the not_allowed plugin (jeremyevans) (#267)
|
26
|
+
|
27
|
+
* Support status_handler method :keep_headers option in status_handler plugin (jeremyevans) (#267)
|
28
|
+
|
29
|
+
* Make not_allowed plugin have r.root return 405 responses for non-GET requests (jeremyevans) (#266)
|
30
|
+
|
31
|
+
* In Rack 3, only require the parts of rack used by Roda, instead of requiring rack itself and relying on autoload (jeremyevans)
|
32
|
+
|
33
|
+
* Add run_require_slash plugin, for skipping application dispatch for remaining paths that would violate Rack SPEC (jeremyevans)
|
34
|
+
|
1
35
|
= 3.55.0 (2022-04-12)
|
2
36
|
|
3
37
|
* Allow passing blocks to the view method in the render plugin (jeremyevans) (#262)
|
data/doc/conventions.rdoc
CHANGED
@@ -86,17 +86,17 @@ Large applications generally need more structure:
|
|
86
86
|
|
87
87
|
For larger apps, the +Rakefile+, +assets/+, +migrate+, +models.rb+, +models/+, +public/+, remain the same.
|
88
88
|
|
89
|
-
+app_name.rb+ should use the +
|
90
|
-
|
89
|
+
+app_name.rb+ should use the +hash_branch_view_subdir+ plugin (which builds on the +hash_branches+ and
|
90
|
+
+view_options+ plugin), or the +multi_run+ plugin.
|
91
|
+
The routes used by the +hash_branches+ or +multi_run+ should be stored in routing files in the +routes/+
|
91
92
|
directory, with one file per prefix.
|
92
93
|
|
93
94
|
For specs/tests, you should have +spec/models/+ and +spec/web/+, with one file per model in +spec/models/+
|
94
95
|
and one file per prefix in +spec/web/+. Substitute +spec+ with +test+ if that is what you are using as the
|
95
96
|
name of the directory.
|
96
97
|
|
97
|
-
You should have a separate view subdirectory per prefix.
|
98
|
-
|
99
|
-
specified on every call to view.
|
98
|
+
You should have a separate view subdirectory per prefix. With the +hash_branch_view_subdir+, the application
|
99
|
+
will automatically set a separate view subdirectory per routing tree branch.
|
100
100
|
|
101
101
|
+helpers/+ should be used to store helper methods for your application, that you call in your routing files
|
102
102
|
and views. In a small application, these methods should just be specified in +app_name.rb+
|
@@ -104,7 +104,7 @@ and views. In a small application, these methods should just be specified in +a
|
|
104
104
|
=== Really Large Applications
|
105
105
|
|
106
106
|
For very large applications, it's expected that there will be deviations from these conventions. However,
|
107
|
-
it is recommended to use the +
|
107
|
+
it is recommended to use the +hash_branch_view_subdir+ or +multi_run+ plugins to organize your application, and have
|
108
108
|
subdirectories in the +routes/+ directory, and nested subdirectories in the +views/+ directory.
|
109
109
|
|
110
110
|
== Roda Application File Layout
|
@@ -156,19 +156,22 @@ For larger applications, there are some slight changes to the Roda application f
|
|
156
156
|
|
157
157
|
plugin :render, escape: true, layout: './layout'
|
158
158
|
plugin :assets
|
159
|
-
plugin :
|
160
|
-
plugin :hash_routes
|
159
|
+
plugin :hash_branch_view_subdir
|
161
160
|
Dir['routes/*.rb'].each{|f| require_relative f}
|
162
161
|
|
163
162
|
route do |r|
|
164
|
-
r.
|
163
|
+
r.hash_branches('')
|
164
|
+
|
165
|
+
r.root do
|
166
|
+
# ...
|
167
|
+
end
|
165
168
|
end
|
166
169
|
|
167
170
|
Dir['helpers/*.rb'].each{|f| require_relative f}
|
168
171
|
end
|
169
172
|
|
170
|
-
After loading the +
|
173
|
+
After loading the +hash_branch_view_subdir+ plugin, you require all of your
|
171
174
|
routing files. Inside your route block, instead of defining your routes, you just call
|
172
|
-
+r.
|
175
|
+
+r.hash_branches+, which will dispatch to all of your routing files. After your route
|
173
176
|
block, you require all of your helper files containing the instance methods for your
|
174
177
|
route block or views, instead of defining the methods directly.
|
@@ -0,0 +1,33 @@
|
|
1
|
+
= New Features
|
2
|
+
|
3
|
+
* RodaRequest#http_version has been added for determining the HTTP
|
4
|
+
version the request was submitted with. This will be a string
|
5
|
+
such as "HTTP/1.0", "HTTP/1.1", "HTTP/2", etc. This will use the
|
6
|
+
SERVER_PROTOCOL and HTTP_VERSION entries from the environment to
|
7
|
+
determine which HTTP version is in use.
|
8
|
+
|
9
|
+
* The status_handler method in the status_handler plugin now supports
|
10
|
+
a :keep_headers option. The value for this option should be an
|
11
|
+
array of header names to keep. All other headers are removed. The
|
12
|
+
default behavior without the option is still to remove all headers.
|
13
|
+
|
14
|
+
* A run_require_slash plugin has been added, which will skip
|
15
|
+
dispatching to another rack application if the remaining path is not
|
16
|
+
empty and does not start with a slash.
|
17
|
+
|
18
|
+
= Other Improvements
|
19
|
+
|
20
|
+
* The status_303 plugin will use 303 as the default redirect status
|
21
|
+
for non-GET requests for HTTP/2 and higher HTTP versions. Previously,
|
22
|
+
it only used 303 for HTTP/1.1.
|
23
|
+
|
24
|
+
* The not_allowed plugin now overrides the r.root method to return
|
25
|
+
405 responses to non-GET requests to the root.
|
26
|
+
|
27
|
+
* The not_allowed plugin no longer sets the body when returning 405
|
28
|
+
responses using methods such as r.get and r.post. Previously, the
|
29
|
+
body was unintentionally set to the same value as the Allow header.
|
30
|
+
|
31
|
+
* When using the Rack master branch (what will become Rack 3), Roda
|
32
|
+
only requires the parts of rack that it uses, instead of requiring
|
33
|
+
rack and relying on autoload to load the parts of rack in use.
|
@@ -0,0 +1,34 @@
|
|
1
|
+
= New Features
|
2
|
+
|
3
|
+
* hash_branches and hash_paths plugins have been split off from the
|
4
|
+
hash_routes plugin, allowing you to use only those parts instead
|
5
|
+
of all of hash_routes.
|
6
|
+
|
7
|
+
The hash_branches plugin supports the hash_branch class method
|
8
|
+
and r.hash_branches routing method.
|
9
|
+
|
10
|
+
The hash_paths plugin supports the hash_path class method and
|
11
|
+
r.hash_paths routing method.
|
12
|
+
|
13
|
+
The hash_routes plugin functions as it did previously by
|
14
|
+
requiring the hash_branches and hash_paths plugins. It adds
|
15
|
+
the hash_routes DSL and r.hash_routes routing method.
|
16
|
+
|
17
|
+
* A hash_branch_view_subdir has been added. It builds on the
|
18
|
+
view_options plugin and new hash_branches plugin, automatically
|
19
|
+
appending a view subdirectory for each successful hash branch.
|
20
|
+
This can DRY up code that uses a separate view subdirectory for
|
21
|
+
each branch.
|
22
|
+
|
23
|
+
= Other Improvements
|
24
|
+
|
25
|
+
* Unprintable characters are now hex escaped in the output of the
|
26
|
+
common_logger plugin. This can protect users who use software
|
27
|
+
that respects shell escape sequences to view the logs.
|
28
|
+
|
29
|
+
= Backwards Compatibility
|
30
|
+
|
31
|
+
* The static_routing plugin now depends on the hash_paths plugin
|
32
|
+
instead of the hash_routes plugin, so you will need to update
|
33
|
+
your application to explicitly load the hash_routes plugin if
|
34
|
+
you were relying on static_routing to implicitly load it.
|
@@ -0,0 +1,16 @@
|
|
1
|
+
= New Features
|
2
|
+
|
3
|
+
* A filter_common_logger plugin has been added, allowing you to skip
|
4
|
+
logging of certain requests in the common_logger plugin. This
|
5
|
+
allows you to only log requests for certain paths, or only log
|
6
|
+
requests for certain types of responses.
|
7
|
+
|
8
|
+
= Other Improvements
|
9
|
+
|
10
|
+
* The heartbeat plugin is now compatible with recent changes in the
|
11
|
+
rack master branch (what will be rack 3).
|
12
|
+
|
13
|
+
* The exception_page plugin will now use Exception#detailed_message
|
14
|
+
on Ruby 3.2+, preserving the did_you_mean and error_highlight
|
15
|
+
information. Additionally, the display of exception messages
|
16
|
+
has been improved.
|
data/lib/roda/plugins/chunked.rb
CHANGED
@@ -226,7 +226,7 @@ class Roda
|
|
226
226
|
# an overview. If a block is given, it is passed to #delay.
|
227
227
|
def chunked(template, opts=OPTS, &block)
|
228
228
|
unless defined?(@_chunked)
|
229
|
-
@_chunked = !self.opts[:force_chunked_encoding] ||
|
229
|
+
@_chunked = !self.opts[:force_chunked_encoding] || @_request.http_version == "HTTP/1.1"
|
230
230
|
end
|
231
231
|
|
232
232
|
if block
|
@@ -20,6 +20,9 @@ class Roda
|
|
20
20
|
# plugin :common_logger, Logger.new('filename')
|
21
21
|
# plugin :common_logger, Logger.new('filename'), method: :debug
|
22
22
|
module CommonLogger
|
23
|
+
MUTATE_LINE = RUBY_VERSION < '2.3' || RUBY_VERSION >= '3'
|
24
|
+
private_constant :MUTATE_LINE
|
25
|
+
|
23
26
|
def self.configure(app, logger=nil, opts=OPTS)
|
24
27
|
app.opts[:common_logger] = logger || app.opts[:common_logger] || $stderr
|
25
28
|
app.opts[:common_logger_meth] = app.opts[:common_logger].method(opts.fetch(:method){logger.respond_to?(:write) ? :write : :<<})
|
@@ -53,7 +56,15 @@ class Roda
|
|
53
56
|
|
54
57
|
env = @_request.env
|
55
58
|
|
56
|
-
|
59
|
+
line = "#{env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-"} - #{env["REMOTE_USER"] || "-"} [#{Time.now.strftime("%d/%b/%Y:%H:%M:%S %z")}] \"#{env["REQUEST_METHOD"]} #{env["SCRIPT_NAME"]}#{env["PATH_INFO"]}#{"?#{env["QUERY_STRING"]}" if ((qs = env["QUERY_STRING"]) && !qs.empty?)} #{@_request.http_version}\" #{result[0]} #{((length = result[1]['Content-Length']) && (length unless length == '0')) || '-'} #{elapsed_time}\n"
|
60
|
+
if MUTATE_LINE
|
61
|
+
line.gsub!(/[^[:print:]\n]/){|c| sprintf("\\x%x", c.ord)}
|
62
|
+
# :nocov:
|
63
|
+
else
|
64
|
+
line = line.gsub(/[^[:print:]\n]/){|c| sprintf("\\x%x", c.ord)}
|
65
|
+
# :nocov:
|
66
|
+
end
|
67
|
+
opts[:common_logger_meth].call(line)
|
57
68
|
end
|
58
69
|
|
59
70
|
# Create timer instance used for timing
|
data/lib/roda/plugins/cookies.rb
CHANGED
@@ -127,7 +127,7 @@ div.context ol.context-line li span { float: right; }
|
|
127
127
|
div.commands { margin-left: 40px; }
|
128
128
|
div.commands a { color:black; text-decoration:none; }
|
129
129
|
#summary { background: #ffc; }
|
130
|
-
#summary h2 { font-weight: normal; color: #666; }
|
130
|
+
#summary h2 { font-weight: normal; color: #666; font-family: monospace; white-space: pre-wrap;}
|
131
131
|
#summary ul#quicklinks { list-style-type: none; margin-bottom: 2em; }
|
132
132
|
#summary ul#quicklinks li { float: left; padding: 0 1em; }
|
133
133
|
#summary ul#quicklinks>li+li { border-left: 1px #666 solid; }
|
@@ -196,12 +196,13 @@ END
|
|
196
196
|
# Designed to be used with the +json+ exception, which will
|
197
197
|
# automatically convert the hash to JSON format.
|
198
198
|
def exception_page(exception, opts=OPTS)
|
199
|
+
message = exception_page_exception_message(exception)
|
199
200
|
if opts[:json]
|
200
201
|
@_response['Content-Type'] = "application/json"
|
201
202
|
{
|
202
203
|
"exception"=>{
|
203
204
|
"class"=>exception.class.to_s,
|
204
|
-
"message"=>
|
205
|
+
"message"=>message,
|
205
206
|
"backtrace"=>exception.backtrace.map(&:to_s)
|
206
207
|
}
|
207
208
|
}
|
@@ -319,7 +320,7 @@ END
|
|
319
320
|
|
320
321
|
<div id="summary">
|
321
322
|
<h1>#{h exception.class} at #{h r.path}</h1>
|
322
|
-
<h2>#{h
|
323
|
+
<h2>#{h message}</h2>
|
323
324
|
<table><tr>
|
324
325
|
<th>Ruby</th>
|
325
326
|
<td>
|
@@ -394,7 +395,22 @@ END1
|
|
394
395
|
END
|
395
396
|
else
|
396
397
|
@_response['Content-Type'] = "text/plain"
|
397
|
-
"#{exception.class}: #{
|
398
|
+
"#{exception.class}: #{message}\n#{exception.backtrace.map{|l| "\t#{l}"}.join("\n")}"
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
private
|
403
|
+
|
404
|
+
# :nocov:
|
405
|
+
if RUBY_VERSION >= '3.2'
|
406
|
+
def exception_page_exception_message(exception)
|
407
|
+
exception.detailed_message(highlight: false).to_s
|
408
|
+
end
|
409
|
+
# :nocov:
|
410
|
+
else
|
411
|
+
# Return message to use for exception.
|
412
|
+
def exception_page_exception_message(exception)
|
413
|
+
exception.message.to_s
|
398
414
|
end
|
399
415
|
end
|
400
416
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
class Roda
|
5
|
+
module RodaPlugins
|
6
|
+
# The skip_common_logger plugin allows for skipping common_logger logging
|
7
|
+
# of some requests. You pass a block when loading the plugin, and the
|
8
|
+
# block will be called before logging each request. The block should return
|
9
|
+
# whether the request should be logged.
|
10
|
+
#
|
11
|
+
# Example:
|
12
|
+
#
|
13
|
+
# # Only log server errors
|
14
|
+
# plugin :filter_common_logger do |result|
|
15
|
+
# result[0] >= 500
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# # Don't log requests to certain paths
|
19
|
+
# plugin :filter_common_logger do |_|
|
20
|
+
# # Block is called in the same context as the route block
|
21
|
+
# !request.path.start_with?('/admin/')
|
22
|
+
# end
|
23
|
+
module FilterCommonLogger
|
24
|
+
def self.load_dependencies(app)
|
25
|
+
app.plugin :common_logger
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.configure(app, &block)
|
29
|
+
app.send(:define_method, :_common_log_request?, &block)
|
30
|
+
app.send(:private, :_common_log_request?)
|
31
|
+
app.send(:alias_method, :_common_log_request?, :_common_log_request?)
|
32
|
+
end
|
33
|
+
|
34
|
+
module InstanceMethods
|
35
|
+
private
|
36
|
+
|
37
|
+
# Log request/response information in common log format to logger.
|
38
|
+
def _roda_after_90__common_logger(result)
|
39
|
+
super if result && _common_log_request?(result)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
register_plugin(:filter_common_logger, FilterCommonLogger)
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
class Roda
|
5
|
+
module RodaPlugins
|
6
|
+
# The hash_branch_view_subdir plugin builds on the hash_branches and view_options
|
7
|
+
# plugins, automatically appending a view subdirectory for any matching hash branch
|
8
|
+
# taken. In cases where you are using a separate view subdirectory per hash branch,
|
9
|
+
# this can result in DRYer code. Example:
|
10
|
+
#
|
11
|
+
# plugin :hash_branch_view_subdir
|
12
|
+
#
|
13
|
+
# route do |r|
|
14
|
+
# r.hash_branches
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# hash_branch 'foo' do |r|
|
18
|
+
# # view subdirectory here is 'foo'
|
19
|
+
# r.hash_branches('foo')
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# hash_branch 'foo', 'bar' do |r|
|
23
|
+
# # view subdirectory here is 'foo/bar'
|
24
|
+
# end
|
25
|
+
module HashBranchViewSubdir
|
26
|
+
def self.load_dependencies(app)
|
27
|
+
app.plugin :hash_branches
|
28
|
+
app.plugin :view_options
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.configure(app)
|
32
|
+
app.opts[:hash_branch_view_subdir_methods] ||= {}
|
33
|
+
end
|
34
|
+
|
35
|
+
module ClassMethods
|
36
|
+
# Freeze the hash_branch_view_subdir metadata when freezing the app.
|
37
|
+
def freeze
|
38
|
+
opts[:hash_branch_view_subdir_methods].freeze.each_value(&:freeze)
|
39
|
+
super
|
40
|
+
end
|
41
|
+
|
42
|
+
# Duplicate hash_branch_view_subdir metadata in subclass.
|
43
|
+
def inherited(subclass)
|
44
|
+
super
|
45
|
+
|
46
|
+
h = subclass.opts[:hash_branch_view_subdir_methods]
|
47
|
+
opts[:hash_branch_view_subdir_methods].each do |namespace, routes|
|
48
|
+
h[namespace] = routes.dup
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Automatically append a view subdirectory for a successful hash_branch route,
|
53
|
+
# by modifying the generated method to append the view subdirectory before
|
54
|
+
# dispatching to the original block.
|
55
|
+
def hash_branch(namespace='', segment, &block)
|
56
|
+
meths = opts[:hash_branch_view_subdir_methods][namespace] ||= {}
|
57
|
+
|
58
|
+
if block
|
59
|
+
meth = meths[segment] = define_roda_method(meths[segment] || "_hash_branch_view_subdir_#{namespace}_#{segment}", 1, &convert_route_block(block))
|
60
|
+
super do |*_|
|
61
|
+
append_view_subdir(segment)
|
62
|
+
send(meth, @_request)
|
63
|
+
end
|
64
|
+
else
|
65
|
+
if meth = meths.delete(segment)
|
66
|
+
remove_method(meth)
|
67
|
+
end
|
68
|
+
super
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
register_plugin(:hash_branch_view_subdir, HashBranchViewSubdir)
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
class Roda
|
5
|
+
module RodaPlugins
|
6
|
+
# The hash_branches plugin allows for O(1) dispatch to multiple route tree branches,
|
7
|
+
# based on the next segment in the remaining path:
|
8
|
+
#
|
9
|
+
# class App < Roda
|
10
|
+
# plugin :hash_branches
|
11
|
+
#
|
12
|
+
# hash_branch("a") do |r|
|
13
|
+
# # /a branch
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# hash_branch("b") do |r|
|
17
|
+
# # /b branch
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# route do |r|
|
21
|
+
# r.hash_branches
|
22
|
+
# end
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# With the above routing tree, the +r.hash_branches+ call in the main routing tree
|
26
|
+
# will dispatch requests for the +/a+ and +/b+ branches of the tree to the appropriate
|
27
|
+
# routing blocks.
|
28
|
+
#
|
29
|
+
# In this example, the hash branches for +/a+ and +/b+ are in the same file, but in larger
|
30
|
+
# applications, they are usually stored in separate files. This allows for easily splitting
|
31
|
+
# up the routing tree into a separate file per branch.
|
32
|
+
#
|
33
|
+
# The +hash_branch+ method supports namespaces, which allow for dispatching to sub-branches
|
34
|
+
# any level of the routing tree, fully supporting the needs of applications with large and
|
35
|
+
# deep routing branches:
|
36
|
+
#
|
37
|
+
# class App < Roda
|
38
|
+
# plugin :hash_branches
|
39
|
+
#
|
40
|
+
# # Only one argument used, so the namespace defaults to '', and the argument
|
41
|
+
# # specifies the route name
|
42
|
+
# hash_branch("a") do |r|
|
43
|
+
# # No argument given, so uses the already matched path as the namespace,
|
44
|
+
# # which is '/a' in this case.
|
45
|
+
# r.hash_branches
|
46
|
+
# end
|
47
|
+
#
|
48
|
+
# hash_branch("b") do |r|
|
49
|
+
# # uses :b as the namespace when looking up routes, as that was explicitly specified
|
50
|
+
# r.hash_branches(:b)
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# # Two arguments used, so first specifies the namespace and the second specifies
|
54
|
+
# # the branch name
|
55
|
+
# hash_branch("/a", "b") do |r|
|
56
|
+
# # /a/b path
|
57
|
+
# end
|
58
|
+
#
|
59
|
+
# hash_branch("/a", "c") do |r|
|
60
|
+
# # /a/c path
|
61
|
+
# end
|
62
|
+
#
|
63
|
+
# hash_branch(:b, "b") do |r|
|
64
|
+
# # /b/b path
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# hash_branch(:b, "c") do |r|
|
68
|
+
# # /b/c path
|
69
|
+
# end
|
70
|
+
#
|
71
|
+
# route do |r|
|
72
|
+
# # No argument given, so uses '' as the namespace, as no part of the path has
|
73
|
+
# # been matched yet
|
74
|
+
# r.hash_branches
|
75
|
+
# end
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# With the above routing tree, requests for the +/a+ and +/b+ branches will be
|
79
|
+
# dispatched to the appropriate +hash_branch+ block. Those blocks will the dispatch
|
80
|
+
# to the remaining +hash_branch+ blocks, with the +/a+ branch using the implicit namespace of
|
81
|
+
# +/a+, and the +/b+ branch using the explicit namespace of +:b+.
|
82
|
+
#
|
83
|
+
# It is best for performance to explicitly specify the namespace when calling
|
84
|
+
# +r.hash_branches+.
|
85
|
+
module HashBranches
|
86
|
+
def self.configure(app)
|
87
|
+
app.opts[:hash_branches] ||= {}
|
88
|
+
end
|
89
|
+
|
90
|
+
module ClassMethods
|
91
|
+
# Freeze the hash_branches metadata when freezing the app.
|
92
|
+
def freeze
|
93
|
+
opts[:hash_branches].freeze.each_value(&:freeze)
|
94
|
+
super
|
95
|
+
end
|
96
|
+
|
97
|
+
# Duplicate hash_branches metadata in subclass.
|
98
|
+
def inherited(subclass)
|
99
|
+
super
|
100
|
+
|
101
|
+
h = subclass.opts[:hash_branches]
|
102
|
+
opts[:hash_branches].each do |namespace, routes|
|
103
|
+
h[namespace] = routes.dup
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# Add branch handler for the given namespace and segment. If called without
|
108
|
+
# a block, removes the existing branch handler if it exists.
|
109
|
+
def hash_branch(namespace='', segment, &block)
|
110
|
+
segment = "/#{segment}"
|
111
|
+
routes = opts[:hash_branches][namespace] ||= {}
|
112
|
+
if block
|
113
|
+
routes[segment] = define_roda_method(routes[segment] || "hash_branch_#{namespace}_#{segment}", 1, &convert_route_block(block))
|
114
|
+
elsif meth = routes.delete(segment)
|
115
|
+
remove_method(meth)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
module RequestMethods
|
121
|
+
# Checks the matching hash_branch namespace for a branch matching the next
|
122
|
+
# segment in the remaining path, and dispatch to that block if there is one.
|
123
|
+
def hash_branches(namespace=matched_path)
|
124
|
+
rp = @remaining_path
|
125
|
+
|
126
|
+
return unless rp.getbyte(0) == 47 # "/"
|
127
|
+
|
128
|
+
if routes = roda_class.opts[:hash_branches][namespace]
|
129
|
+
if segment_end = rp.index('/', 1)
|
130
|
+
if meth = routes[rp[0, segment_end]]
|
131
|
+
@remaining_path = rp[segment_end, 100000000]
|
132
|
+
always{scope.send(meth, self)}
|
133
|
+
end
|
134
|
+
elsif meth = routes[rp]
|
135
|
+
@remaining_path = ''
|
136
|
+
always{scope.send(meth, self)}
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
register_plugin(:hash_branches, HashBranches)
|
144
|
+
end
|
145
|
+
end
|