class Mustermann::Router::Rack

Simple pattern based router that allows matching paths to a given Rack application.

@example config.ru

router = Mustermann::Rack.new do
  on '/' do |env|
    [200, {'Content-Type' => 'text/plain'}, ['Hello World!']]
  end

  on '/:name' do |env|
    name = env['mustermann.params']['name']
    [200, {'Content-Type' => 'text/plain'}, ["Hello #{name}!"]]
  end

  on '/something/*', call: SomeApp
end

# in a config.ru
run router

Public Class Methods

new(env_prefix: "mustermann", params_key: " click to toggle source
Calls superclass method
# File lib/praxis/router/rack.rb, line 28
def initialize(env_prefix: "mustermann", params_key: "#{env_prefix}.params", pattern_key: "#{env_prefix}.pattern", **options, &block)
  @params_key, @pattern_key = params_key, pattern_key
  options[:default] = [404, {"Content-Type" => "text/plain", "X-Cascade" => "pass"}, ["Not Found"]] unless options.include? :default
  super(**options, &block)
end

Private Instance Methods

invoke(callback, env, params, pattern) click to toggle source
# File lib/praxis/router/rack.rb, line 34
def invoke(callback, env, params, pattern)
  params_was, pattern_was             = env[@params_key], env[@pattern_key]
  env[@params_key], env[@pattern_key] = params, pattern
  response = callback.call(env)
  response[1].each { |k,v| throw :pass if k.downcase == 'x-cascade' and v == 'pass' }
  response
ensure
  env[@params_key], env[@pattern_key] = params_was, pattern_was
end
string_for(env) click to toggle source
# File lib/praxis/router/rack.rb, line 44
def string_for(env)
  env['PATH_INFO']
end