class Rutter::Route
Represents a single route.
@!attribute [r] path
@return [String] Raw path template.
@!attribute [r] endpoint
@return [Hash] Route endpoint.
@private
Attributes
endpoint[R]
path[R]
Public Class Methods
new(path, endpoint, constraints = nil)
click to toggle source
Initializes the route.
@param path [String]
Path template to match.
@param endpoint [#call]
Rack endpoint.
@param constraints [Hash]
Route segment constraints.
@return [void]
@private
# File lib/rutter/route.rb, line 30 def initialize(path, endpoint, constraints = nil) @path = Naming.cleanpath(path) @endpoint = endpoint_to_hash(endpoint) @pattern = ::Mustermann.new(@path, capture: constraints) freeze end
Public Instance Methods
call(env)
click to toggle source
Calls the endpoint.
@param env [Hash]
Rack's environment hash.
@return [Array]
Rack response array.
@private
# File lib/rutter/route.rb, line 86 def call(env) env["router.params"] ||= {} env["router.params"].merge!(params(env["PATH_INFO"])) env["rutter.action"] = @endpoint[:action] ctrl = @endpoint[:controller] ctrl = ::Object.const_get(ctrl) if ctrl.is_a?(String) ctrl.call(env) end
expand(**args)
click to toggle source
Generates a path from the given arguments.
@overload expand(key: value)
@param key [String, Integer, Array] Key value.
@overload expand(key: value, key2: value2)
@param key2 [String, Integer, Array] Key value.
@return [String]
Generated path.
@raise [ArguemntError]
If the path cannot be generated. Mostly due to missing key(s).
# File lib/rutter/route.rb, line 61 def expand(**args) @pattern.expand(:append, **args) rescue ::Mustermann::ExpandError => e raise ArgumentError, e.message end
match?(env)
click to toggle source
Matches the route pattern against environment.
@param env [Hash]
Rack environment hash.
@return [Boolean]
# File lib/rutter/route.rb, line 43 def match?(env) @pattern === env["PATH_INFO"] # rubocop:disable Style/CaseEquality end
params(path)
click to toggle source
Extract params from the given path.
@param path [String]
Path used to extract params from.
@return [Hash]
# File lib/rutter/route.rb, line 73 def params(path) @pattern.params(path) || {} end
Private Instance Methods
endpoint_to_hash(endpoint)
click to toggle source
@private
# File lib/rutter/route.rb, line 99 def endpoint_to_hash(endpoint) ctrl, action = if endpoint.is_a?(String) ctrl, action = endpoint.split("#") ctrl = Naming.classify(ctrl) [ctrl, action] else [endpoint, nil] end { controller: ctrl, action: action } end