Class: Rack::BodyProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/body_proxy.rb

Overview

Proxy for response bodies allowing calling a block when the response body is closed (after the response has been fully sent to the client).

Direct Known Subclasses

Events::EventedBodyProxy

Instance Method Summary collapse

Constructor Details

#initialize(body, &block) ⇒ BodyProxy

Set the response body to wrap, and the block to call when the response has been fully sent.



10
11
12
13
14
# File 'lib/rack/body_proxy.rb', line 10

def initialize(body, &block)
  @body = body
  @block = block
  @closed = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Delegate missing methods to the wrapped body.



40
41
42
# File 'lib/rack/body_proxy.rb', line 40

def method_missing(method_name, *args, &block)
  @body.__send__(method_name, *args, &block)
end

Instance Method Details

#closeObject

If not already closed, close the wrapped body and then call the block the proxy was initialized with.



23
24
25
26
27
28
29
30
31
# File 'lib/rack/body_proxy.rb', line 23

def close
  return if @closed
  @closed = true
  begin
    @body.close if @body.respond_to?(:close)
  ensure
    @block.call
  end
end

#closed?Boolean

Whether the proxy is closed. The proxy starts as not closed, and becomes closed on the first call to close.

Returns:

  • (Boolean)


35
36
37
# File 'lib/rack/body_proxy.rb', line 35

def closed?
  @closed
end

#respond_to_missing?(method_name, include_all = false) ⇒ Boolean

Return whether the wrapped body responds to the method.

Returns:

  • (Boolean)


17
18
19
# File 'lib/rack/body_proxy.rb', line 17

def respond_to_missing?(method_name, include_all = false)
  super or @body.respond_to?(method_name, include_all)
end