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 30
def initialize(env_prefix: 'mustermann', params_key: "#{env_prefix}.params", pattern_key: "#{env_prefix}.pattern", **options, &block)
  @params_key = params_key
  @pattern_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 37
def invoke(callback, env, params, pattern)
  params_was = env[@params_key]
  pattern_was = env[@pattern_key]
  env[@params_key] = params
  env[@pattern_key] = pattern
  response = callback.call(env)
  response[1].each { |k, v| throw :pass if (k.downcase == 'x-cascade') && (v == 'pass') }
  response
ensure
  env[@params_key] = params_was
  env[@pattern_key] = pattern_was
end
string_for(env) click to toggle source
# File lib/praxis/router/rack.rb, line 50
def string_for(env)
  env['PATH_INFO']
end