Skip to content

Instantly share code, notes, and snippets.

@ddollar
Forked from anonymous/snippet.rb
Created November 12, 2009 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddollar/233443 to your computer and use it in GitHub Desktop.
Save ddollar/233443 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'rack'
require 'ruby-debug'
class Rack::Nest
attr_reader :app
def initialize(app, options={}, &block)
@path = options[:path]
@method = options[:method]
@app = app
@middleware = []
instance_eval(&block)
end
def call(env)
if @path == env['PATH_INFO']
stack.call(env)
else
app.call(env)
end
end
def stack
@middleware.reverse.inject(app) { |app, mid| mid.call(app) }
end
def use(middleware, *args, &block)
@middleware << lambda { |app| middleware.new(app, *args, &block) }
end
end
use Rack::Nest, :path => '/no' do
use Rack::Auth::Basic, "Lobster 2.0" do |username, password|
'secret' == password
end
end
run lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'Hi'] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment