Skip to content
Browse files

After callbacks should be able to alter the response. They now take r…

…esponse as the argument for 'call'. Example :

class Gzip
  def call(response)
    status, headers, body = response
    ....
    [200, headers, compressed_body]
  end
end

application = Rack::Callbacks.new do
  run RackApp.new
  after Gzip
end
  • Loading branch information...
1 parent ec0fb58 commit a3a5ad3e13fda22f145ece02846f1384baedd465 @lifo lifo committed Jan 14, 2009
Showing with 24 additions and 6 deletions.
  1. +3 −2 lib/rack/contrib/callbacks.rb
  2. +21 −4 test/spec_rack_callbacks.rb
View
5 lib/rack/contrib/callbacks.rb
@@ -28,9 +28,10 @@ def run(app)
def call(env)
@before.each {|c| c.call(env) }
+
response = @app.call(env)
- @after.each {|c| c.call(env) }
- response
+
+ @after.inject(response) {|r, c| c.call(r) }
end
end
end
View
25 test/spec_rack_callbacks.rb
@@ -17,8 +17,22 @@ def call(env)
end
class Finale
- def call(env)
- $hax_logger = 'lol'
+ def call(response)
+ status, headers, body = response
+
+ headers['last'] = 'Finale'
+ $old_status = status
+
+ [201, headers, body]
+ end
+end
+
+class TheEnd
+ def call(response)
+ status, headers, body = response
+
+ headers['last'] = 'TheEnd'
+ [201, headers, body]
end
end
@@ -31,14 +45,17 @@ def call(env)
run lambda {|env| [200, {}, env['flame'] + env['peace']] }
after Finale
+ after TheEnd
end
app = Rack::Builder.new do
run callback_app
end.to_app
response = Rack::MockRequest.new(app).get("/")
- response.body.to_s.should.equal 'F Lifo..with love'
- $hax_logger.should.equal 'lol'
+
+ response.body.should.equal 'F Lifo..with love'
+ $old_status.should.equal 200
+ response.headers['last'].should.equal 'TheEnd'
end
end

0 comments on commit a3a5ad3

Please sign in to comment.
Something went wrong with that request. Please try again.