Sha256: 07f34c3ca539a29674cfc14802b72d0661861dbb059744f18aff35068aa2c8d4

Contents?: true

Size: 1.75 KB

Versions: 28

Compression:

Stored size: 1.75 KB

Contents

# Originally from:
# https://github.com/rkh/mustermann/blob/500a603fffe0594ab842d72addcc449eedd6d5be/mustermann/lib/mustermann/router/rack.rb
# Copied here after its removal from Mustermann proper

require 'praxis/router/simple'

module Mustermann
  module Router
    # 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
    class Rack < Simple
      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

      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

      def string_for(env)
        env['PATH_INFO']
      end

      private :invoke, :string_for
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
praxis-2.0.pre.18 lib/praxis/router/rack.rb
praxis-2.0.pre.17 lib/praxis/router/rack.rb
praxis-2.0.pre.16 lib/praxis/router/rack.rb
praxis-2.0.pre.15 lib/praxis/router/rack.rb
praxis-2.0.pre.14 lib/praxis/router/rack.rb
praxis-2.0.pre.13 lib/praxis/router/rack.rb
praxis-2.0.pre.12 lib/praxis/router/rack.rb
praxis-2.0.pre.11 lib/praxis/router/rack.rb
praxis-2.0.pre.10 lib/praxis/router/rack.rb
praxis-2.0.pre.9 lib/praxis/router/rack.rb
praxis-2.0.pre.8 lib/praxis/router/rack.rb
praxis-2.0.pre.7 lib/praxis/router/rack.rb
praxis-2.0.pre.6 lib/praxis/router/rack.rb
praxis-2.0.pre.5 lib/praxis/router/rack.rb
praxis-2.0.pre.4 lib/praxis/router/rack.rb
praxis-2.0.pre.3 lib/praxis/router/rack.rb
praxis-2.0.pre.2 lib/praxis/router/rack.rb
praxis-2.0.pre.1 lib/praxis/router/rack.rb
praxis-0.22.pre.2 lib/praxis/router/rack.rb
praxis-0.22.pre.1 lib/praxis/router/rack.rb