Class: Protocol::Rack::Body::Enumerable

Inherits:
HTTP::Body::Readable
  • Object
show all
Defined in:
lib/protocol/rack/body/enumerable.rb

Overview

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.

Constant Summary collapse

CONTENT_LENGTH =
'content-length'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, length) ⇒ Enumerable

Initialize the output wrapper.



32
33
34
35
36
37
# File 'lib/protocol/rack/body/enumerable.rb', line 32

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

Instance Attribute Details

#bodyObject (readonly)

The rack response body.



40
41
42
# File 'lib/protocol/rack/body/enumerable.rb', line 40

def body
  @body
end

#lengthObject (readonly)

The content length of the rack response body.



43
44
45
# File 'lib/protocol/rack/body/enumerable.rb', line 43

def length
  @length
end

Class Method Details

.wrap(body, length = nil) ⇒ Object

Wraps an array into a buffered body.



20
21
22
23
24
25
26
27
# File 'lib/protocol/rack/body/enumerable.rb', line 20

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

Instance Method Details

#call(stream) ⇒ Object



80
81
82
83
84
# File 'lib/protocol/rack/body/enumerable.rb', line 80

def call(stream)
	@body.call(stream)
ensure
	self.close($!)
end

#close(error = nil) ⇒ Object

Close the response body.



56
57
58
59
60
61
62
63
64
65
# File 'lib/protocol/rack/body/enumerable.rb', line 56

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

#each(&block) ⇒ Object

Enumerate the response body.



70
71
72
73
74
# File 'lib/protocol/rack/body/enumerable.rb', line 70

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

#empty?Boolean

Whether the body is empty.

Returns:

  • (Boolean)


46
47
48
# File 'lib/protocol/rack/body/enumerable.rb', line 46

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

#inspectObject



96
97
98
# File 'lib/protocol/rack/body/enumerable.rb', line 96

def inspect
	"\#<#{self.class} length=#{@length.inspect} body=#{@body.class}>"
end

#readObject

Read the next chunk from the response body.



88
89
90
91
92
93
94
# File 'lib/protocol/rack/body/enumerable.rb', line 88

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

#ready?Boolean

Whether the body can be read immediately.

Returns:

  • (Boolean)


51
52
53
# File 'lib/protocol/rack/body/enumerable.rb', line 51

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

#stream?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/protocol/rack/body/enumerable.rb', line 76

def stream?
	!@body.respond_to?(:each)
end