[~/local/src/ruby/sinatra/rack/rack-debugging(master)]$ cat middlefoo.rb
require 'rack'
class MiddleFoo
def initialize(app)
@app = app
end
def call env
# Podemos modificar el request (env) aqui
env['chuchu'] = 'SYTW'
status, headers, body = @app.call(env)
# Podemos modificar la respuesta aqui
newbody = body.map(&:upcase)
[status, headers, newbody]
end
end
[~/local/src/ruby/sinatra/rack/rack-debugging(master)]$ cat hello_middle.rb
require 'rack'
require './middlefoo'
class HelloWorld
def call env
[200, {"Content-Type" => "text/plain"}, ["Hello world\nchuchu=#{env['chuchu']}\n"]]
end
end
Rack::Handler::WEBrick::run MiddleFoo.new(HelloWorld.new)
Cuando ejecutamos el programa produce la salida:
HELLO WORLD CHUCHU=SYTW