Skip to content

Instantly share code, notes, and snippets.

@gbuesing
Created June 13, 2013 15: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 gbuesing/5774849 to your computer and use it in GitHub Desktop.
Save gbuesing/5774849 to your computer and use it in GitHub Desktop.
# Middleware to tap into request so that you can provide an alternate response depending upon request conditions.
#
# Supplied block should return a Rack-compatible response, or nil/false to pass on handling of the request.
#
# Example:
#
# use Rack::Tap do |env|
# if Rack::Request.new(env).host == 'admin.myapp.com'
# [301, {'Location' => 'http://www.myapp.com/admin'}, []]
# end
# end
#
# run MyApp
#
module Rack
class Tap
def initialize(app, &block)
@app = app
@block = block
end
def call(env)
@block.call(env) || @app.call(env)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment