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.



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

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

Instance Attribute Details

#bodyObject (readonly)

The rack response body.



57
58
59
# File 'lib/protocol/rack/body/enumerable.rb', line 57

def body
  @body
end

#lengthObject (readonly)

The content length of the rack response body.



60
61
62
# File 'lib/protocol/rack/body/enumerable.rb', line 60

def length
  @length
end

Class Method Details

.wrap(body, length = nil) ⇒ Object

Wraps an array into a buffered body.



37
38
39
40
41
42
43
44
# File 'lib/protocol/rack/body/enumerable.rb', line 37

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



97
98
99
100
101
# File 'lib/protocol/rack/body/enumerable.rb', line 97

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

#close(error = nil) ⇒ Object

Close the response body.



73
74
75
76
77
78
79
80
81
82
# File 'lib/protocol/rack/body/enumerable.rb', line 73

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.



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

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

#empty?Boolean

Whether the body is empty.

Returns:

  • (Boolean)


63
64
65
# File 'lib/protocol/rack/body/enumerable.rb', line 63

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

#inspectObject



113
114
115
# File 'lib/protocol/rack/body/enumerable.rb', line 113

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

#readObject

Read the next chunk from the response body.



105
106
107
108
109
110
111
# File 'lib/protocol/rack/body/enumerable.rb', line 105

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)


68
69
70
# File 'lib/protocol/rack/body/enumerable.rb', line 68

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

#stream?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/protocol/rack/body/enumerable.rb', line 93

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