Created
June 13, 2013 15:54
-
-
Save gbuesing/5774849 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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