Module: Roda::RodaPlugins::PathRewriter
- Defined in:
- lib/roda/plugins/path_rewriter.rb
Overview
The path_rewriter plugin allows you to rewrite the remaining path or the path info for requests. This is useful if you want to transparently treat some paths the same as other paths.
By default, rewrite_path
will rewrite just the remaining path. So only routing in the current Roda app will be affected. This is useful if you have other code in your app that uses PATH_INFO and needs to see the original PATH_INFO (for example, when using relative links).
rewrite_path '/a', '/b'
# PATH_INFO '/a' => remaining_path '/b'
# PATH_INFO '/a/c' => remaining_path '/b/c'
In some cases, you may want to override PATH_INFO for the rewritten paths, such as when you are passing the request to another Rack app. For those cases, you can use the path_info: true
option to rewrite_path
.
rewrite_path '/a', '/b', path_info: true
# PATH_INFO '/a' => PATH_INFO '/b'
# PATH_INFO '/a/c' => PATH_INFO '/b/c'
If you pass a string to rewrite_path
, it will rewrite all paths starting with that string. You can provide a regexp if you want more complete control, such as only matching exact paths.
rewrite_path /\A\/a\z/, '/b'
# PATH_INFO '/a' => remaining_path '/b'
# PATH_INFO '/a/c' => remaining_path '/a/c', no change
Patterns can be rewritten dynamically by providing a block accepting a MatchData object and evaluating to the replacement.
rewrite_path(/\A\/a\/(\w+)/){|match| "/a/#{match[1].capitalize}"}
# PATH_INFO '/a/moo' => remaining_path '/a/Moo'
rewrite_path(/\A\/a\/(\w+)/, path_info: true){|match| "/a/#{match[1].capitalize}"}
# PATH_INFO '/a/moo' => PATH_INFO '/a/Moo'
All path rewrites are applied in order, so if a path is rewritten by one rewrite, it can be rewritten again by a later rewrite. Note that PATH_INFO rewrites are processed before remaining_path rewrites.
Defined Under Namespace
Modules: ClassMethods, RequestMethods
Class Method Summary collapse
Class Method Details
.configure(app) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/roda/plugins/path_rewriter.rb', line 48 def self.configure(app) app.instance_exec do app.opts[:remaining_path_rewrites] ||= [] app.opts[:path_info_rewrites] ||= [] end end |