class Rewriter
Rewrite a request path based on a set of defined rules.
Definitions
def initialize
Initialize an empty rewrite rule sequence.
Implementation
def initialize
@rules = []
end
def extract_prefix(**patterns, &block)
Add a rule that extracts a typed prefix from the request path.
Signature
-
parameter
patternsHash The path rewrite patterns.
-
yields
{|request, path, match| ...} The request, original path, and match data when the rule matches.
-
returns
ExtractPrefixRule The added rule.
Implementation
def extract_prefix(**patterns, &block)
@rules << ExtractPrefixRule.new(patterns, block)
end
def apply(context, request, path)
Apply every rewrite rule in order.
Signature
-
parameter
contextObject The context.
-
parameter
requestRack::Request The request.
-
parameter
pathUtopia::Path | String The path.
-
returns
Path The rewritten path.
Implementation
def apply(context, request, path)
@rules.each do |rule|
path = rule.apply(context, request, path)
end
return path
end
def call(context, request, path)
Rewrite a path's components in place.
Signature
-
parameter
contextObject The context.
-
parameter
requestRack::Request The request.
-
parameter
pathUtopia::Path | String The path.
-
returns
Array(String) The rewritten components.
Implementation
def call(context, request, path)
path.components = apply(context, request, path).components
end