改变Rack Middleware中的response.body

我正在尝试为Rails 4.2应用程序编写一些Rack Middleware,它使用gsub方法改变响应体。 我发现使用这样的模式的旧示例:

 class MyMiddleware def initialize(app) @app = app end def call(env) status, headers, response = @app.call(env) # do some stuff [status, headers, response] end end 

我发现的是没有response.body setter方法response.body 。 还有另一种模式我可以开始修改身体吗?

问题是它需要一个Array作为call方法中的第三个参数。 这种模式让我重新开始工作。

 # not real code, just a pattern to follow class MyMiddleware def initialize(app) @app = app end def call(env) status, headers, response = @app.call(env) new_response = make_new_response(response.body) # also must reset the Content-Length header if changing body headers['Content-Length'] = new_response.length.to_s [status, headers, [new_response]] end end