Protocol::RackSourceProtocolRackBodyEnumerable

class Enumerable

Wraps the rack response body.

The rack body must respond to each and must only yield String values. If the body responds to close, it will be called after iteration.

Definitions

def self.wrap(body, length = nil)

Wraps an array into a buffered body.

Signature

parameter body Object

The rack response body.

Implementation

def self.wrap(body, length = nil)
	if body.is_a?(Array)
		length ||= body.sum(&:bytesize)
		return self.new(body, length)
	else
		return self.new(body, length)
	end
end

def initialize(body, length)

Initialize the output wrapper.

Signature

parameter body Object

The rack response body.

parameter length Integer

The rack response length.

Implementation

def initialize(body, length)
	@length = length
	@body = body
	
	@chunks = nil
end

attr :body

The rack response body.

attr :length

The content length of the rack response body.

def empty?

Whether the body is empty.

Implementation

def empty?
	@length == 0 or (@body.respond_to?(:empty?) and @body.empty?)
end

def ready?

Whether the body can be read immediately.

Implementation

def ready?
	body.is_a?(Array) or body.respond_to?(:to_ary)
end

def close(error = nil)

Close the response body.

Implementation

def close(error = nil)
	if @body and @body.respond_to?(:close)
		@body.close
	end
	
	@body = nil
	@chunks = nil
	
	super
end

def each(&block)

Enumerate the response body.

Signature

yields {|chunk| ...}
parameter chunk String

Implementation

def each(&block)
	@body.each(&block)
ensure
	self.close($!)
end

def read

Read the next chunk from the response body.

Signature

returns String | Nil

Implementation

def read
	@chunks ||= @body.to_enum(:each)
	
	return @chunks.next
rescue StopIteration
	return nil
end