class Rack::Router::Middleware
Attributes
logger[R]
Public Class Methods
new(app, logger: nil, &block)
click to toggle source
# File lib/rack/router/middleware.rb, line 8 def initialize(app, logger: nil, &block) @app = app @routes = RouteBuilder.new @current_path = nil @current_options = {} @not_found = nil @logger = logger || Logger.new(STDOUT) @callbacks = [] instance_exec(&block) end
Public Instance Methods
after_route(&block)
click to toggle source
@yield after route match executed @yieldparam env [Hash] @yieldparam route [Rack::Router::Route, NilClass]
# File lib/rack/router/middleware.rb, line 104 def after_route(&block) raise ArgumentError, 'block must be given' unless block_given? @callbacks.push(block) end
call(env)
click to toggle source
# File lib/rack/router/middleware.rb, line 19 def call(env) route = @routes.match(env) env[RACK_ROUTER_PATH] = route&.value env[RACK_ROUTER_PATH_HASH] = route&.path_hash env[Rack::PATH_INFO] run_after_route(env, route) if route.nil? && !@not_found.nil? logger.debug { "#{self.class}#call route was not found\n#{@routes.print_routes}" } return render_not_found(env) end @app.call(env) end
define_path(http_method, path, value, options = {})
click to toggle source
@param http_method [String,Symbol] @param path [String] @param value [String, Proc<Hash>] @param options [Hash]
:constraint [Hash<Symbol=>Proc<String>>] :content_type [Array<String>,String]
# File lib/rack/router/middleware.rb, line 89 def define_path(http_method, path, value, options = {}) options = @current_options.dup.merge(options) path = [@current_path, path].compact.join('/') @routes.add(http_method, path, value, options) end
match(path, value, options = {})
click to toggle source
# File lib/rack/router/middleware.rb, line 44 def match(path, value, options = {}) define_path(nil, path, value, options) end
namespace(path, &block)
click to toggle source
@param path [String]
# File lib/rack/router/middleware.rb, line 64 def namespace(path, &block) nested(path, {}, &block) end
nested(path, options = {}) { || ... }
click to toggle source
@param path [String] @param options [Hash<Symbol>,Hash]
:constraint [Hash<Symbol=>Proc<String>>] :content_type [String, Array<String>]
# File lib/rack/router/middleware.rb, line 52 def nested(path, options = {}) old_current_path = @current_path old_current_options = @current_options @current_path = [@current_path, path].compact.join('/') @current_options = @current_options.dup.merge(options) yield ensure @current_path = old_current_path @current_options = old_current_options end
not_found(response)
click to toggle source
# File lib/rack/router/middleware.rb, line 95 def not_found(response) response = default_not_found if response == :default response = nil unless response @not_found = response end
with_accept(accept, &block)
click to toggle source
@param accept [String, Array<String>]
# File lib/rack/router/middleware.rb, line 79 def with_accept(accept, &block) nested(nil, accept: accept, &block) end
with_constraint(constraint, &block)
click to toggle source
@param constraint [Hash<Symbol,Proc<String>>]
# File lib/rack/router/middleware.rb, line 69 def with_constraint(constraint, &block) nested(nil, constraint: constraint, &block) end
with_content_type(content_type, &block)
click to toggle source
@param content_type [String, Array<String>]
# File lib/rack/router/middleware.rb, line 74 def with_content_type(content_type, &block) nested(nil, content_type: content_type, &block) end
Private Instance Methods
default_not_found()
click to toggle source
# File lib/rack/router/middleware.rb, line 118 def default_not_found [404, { Rack::CONTENT_TYPE => 'text/plain' }, ['Route not found']] end
render_not_found(env)
click to toggle source
# File lib/rack/router/middleware.rb, line 122 def render_not_found(env) return @not_found.call(env) if @not_found.is_a?(Proc) @not_found end
run_after_route(env, route)
click to toggle source
# File lib/rack/router/middleware.rb, line 112 def run_after_route(env, route) @callbacks.each do |cb| instance_exec(env, route, &cb) end end