Sha256: 2944ae338e55ea659e81e8d94ae711bb6e4848a251ffe98e36aaa4a641f46ce6

Contents?: true

Size: 1.27 KB

Versions: 62

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module Rack
  # Proxy for response bodies allowing calling a block when
  # the response body is closed (after the response has been fully
  # sent to the client).
  class BodyProxy
    # Set the response body to wrap, and the block to call when the
    # response has been fully sent.
    def initialize(body, &block)
      @body = body
      @block = block
      @closed = false
    end

    # Return whether the wrapped body responds to the method.
    def respond_to_missing?(method_name, include_all = false)
      super or @body.respond_to?(method_name, include_all)
    end

    # If not already closed, close the wrapped body and
    # then call the block the proxy was initialized with.
    def close
      return if @closed
      @closed = true
      begin
        @body.close if @body.respond_to? :close
      ensure
        @block.call
      end
    end

    # Whether the proxy is closed.  The proxy starts as not closed,
    # and becomes closed on the first call to close.
    def closed?
      @closed
    end

    # Delegate missing methods to the wrapped body.
    def method_missing(method_name, *args, &block)
      @body.__send__(method_name, *args, &block)
    end
    ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
  end
end

Version data entries

62 entries across 47 versions & 14 rubygems

Version Path
rack-2.2.10 lib/rack/body_proxy.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rack-2.2.9/lib/rack/body_proxy.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rack-2.2.7/lib/rack/body_proxy.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rack-2.2.7/lib/rack/body_proxy.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rack-2.2.7/lib/rack/body_proxy.rb
rack-2.2.9 lib/rack/body_proxy.rb
rack-2.2.8.1 lib/rack/body_proxy.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rack-2.2.4/lib/rack/body_proxy.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rack-2.2.7/lib/rack/body_proxy.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rack-2.2.3/lib/rack/body_proxy.rb
rack-2.2.8 lib/rack/body_proxy.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rack-2.2.7/lib/rack/body_proxy.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rack-2.2.4/lib/rack/body_proxy.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rack-2.2.3/lib/rack/body_proxy.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/body_proxy.rb
rack-2.2.7 lib/rack/body_proxy.rb
rack-2.2.6.4 lib/rack/body_proxy.rb
rack-2.2.6.3 lib/rack/body_proxy.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/body_proxy.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/body_proxy.rb