我显然有一个Rack :: Builder的误解。在我的config.ru文件里面我有:
require 'rack'
require 'rack/lobster'
class Shrimp
SHRIMP_STRING = 'teste'
def initialize(app)
@app = app
end
def call(env)
status, headers, response = @app.call(env)
response_body = ""
response.each { |part| response_body += part }
response_body += "<pre>#{SHRIMP_STRING}</pre>"
headers["Content-Length"] = response_body.length.to_s
[status, headers, response_body]
end
end
app = Rack::Builder.new do
use Rack::Lobster
run Shrimp.new
end
Rack::Handler::WEBrick.run app
当我做的时候 rackup config.ru
我得到了
/home/vagrant/config.ru:7:in `initialize': wrong number of arguments (0 for 1) (ArgumentError)
from /home/vagrant/config.ru:26:in `new'
我错过了什么吗?根据这个tutorial Rack :: Builder.new只接收一个块作为参数。
编辑: 改变这一行
run Shrimp.new
为:
run Shrimp
我仍然得到错误数量的参数,但这次是Rack :: Builder
ERROR ArgumentError: wrong number of arguments (1 for 0)
/home/vagrant/.rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:86:in `initialize'
答案 0 :(得分:1)
对于Rack中间件,您不需要执行Shrimp.new
,您只需执行use Shrimp
即可。
您可以找到它的一个示例here.
与此链接相同,您只需执行以下操作:
# config.ru
require 'rack'
require 'rack/lobster'
require 'shrimp'
use Shrimp
run Rack::Lobster.new