Class: Protocol::HTTP::Body::Readable

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/http/body/readable.rb

Overview

def finish -> buffer the stream and close it. def close(error = nil) -> close the stream immediately. end

Direct Known Subclasses

Buffered, File, Wrapper

Instance Method Summary collapse

Instance Method Details

#call(stream) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/protocol/http/body/readable.rb', line 70

def call(stream)
	# Flushing after every chunk is inefficient, but it's also a safe default.
	self.each do |chunk|
		stream.write(chunk)
		stream.flush
	end
end

#close(error = nil) ⇒ Object

The consumer can call stop to signal that the stream output has terminated.



37
38
# File 'lib/protocol/http/body/readable.rb', line 37

def close(error = nil)
end

#eachObject

Enumerate all chunks until finished, then invoke ‘#close`.



61
62
63
64
65
66
67
68
# File 'lib/protocol/http/body/readable.rb', line 61

def each
	while chunk = self.read
		yield chunk
		# chunk.clear
	end
ensure
	self.close($!)
end

#empty?Boolean

Will read return any data?

Returns:

  • (Boolean)


41
42
43
# File 'lib/protocol/http/body/readable.rb', line 41

def empty?
	false
end

#finishObject

Read all remaining chunks into a buffered body and close the underlying input.



55
56
57
58
# File 'lib/protocol/http/body/readable.rb', line 55

def finish
	# Internally, this invokes `self.each` which then invokes `self.close`.
	Buffered.for(self)
end

#joinObject

Read all remaining chunks into a single binary string using ‘#each`.



79
80
81
82
83
84
85
86
87
88
# File 'lib/protocol/http/body/readable.rb', line 79

def join
	buffer = String.new.force_encoding(Encoding::BINARY)
	
	self.each do |chunk|
		buffer << chunk
		chunk.clear
	end
	
	return buffer
end

#lengthObject



45
46
47
# File 'lib/protocol/http/body/readable.rb', line 45

def length
	nil
end

#readObject

Read the next available chunk.



50
51
52
# File 'lib/protocol/http/body/readable.rb', line 50

def read
	nil
end