Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example of baking our own middleware stack to better understand how Rack middleware works.
class Talk
def call(env)
"Can I talk to #{env[:name]}!"
end
end
class Shout
def initialize(app)
@app = app
end
def call(env)
@app.call(env).upcase
end
end
class Zuul
def initialize(app)
@app = app
end
def call(env)
"There is no #{env[:name]}. Only Zuul!"
end
end
def run_the_stack(middleware, app, env)
prev_app = app
middleware.reverse.each do |part|
part = part.new(prev_app)
prev_app = part
end
prev_app.call(env)
end
puts run_the_stack [Shout, Zuul], Talk.new, name: 'Dana'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment