Class: Mustermann::Router::Rack

Inherits:
Simple
  • Object
show all
Defined in:
lib/praxis/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

Instance Attribute Summary

Attributes inherited from Simple

#default

Instance Method Summary collapse

Methods inherited from Simple

#[], #[]=, #call, #on

Constructor Details

#initialize(env_prefix: 'mustermann', params_key: "#{env_prefix}.params", pattern_key: "#{env_prefix}.pattern", **options, &block) ⇒ Rack

Returns a new instance of Rack.



30
31
32
33
34
35
# 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