Class: Mustermann::Router::Rack

Inherits:
Simple show all
Defined in:
lib/mustermann/router/rack.rb

Overview

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

Examples:

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

Constant Summary

Constants inherited from Pattern

Pattern::PATTERN_METHODS

Constants included from Mustermann

DEFAULT_TYPE, VERSION

Instance Attribute Summary

Attributes inherited from Mustermann::RegexpBased

#regexp

Instance Method Summary collapse

Methods inherited from Mustermann::RegexpBased

#peek_match, #peek_size

Methods inherited from Pattern

#&, #===, #=~, #^, #expand, #match, #named_captures, #names, new, #params, #peek, #peek_match, #peek_params, #peek_size, supported?, supported_options, #to_proc, #to_s, #to_templates, #|

Methods included from Mustermann

[], new, register

Constructor Details

#initialize(options = {}, &block) ⇒ Rack


24
25
26
27
28
29
30
31
# File 'lib/mustermann/router/rack.rb', line 24

def initialize(options = {}, &block)
  env_prefix  = options.delete(:env_prefix) || "mustermann"
  params_key  = options.delete(:params_key) || "#{env_prefix}.params"
  pattern_key = options.delete(:pattern_key) || "#{env_prefix}.pattern"
  @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