Class: Protocol::HTTP::Body::Streamable::RequestBody

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

Overview

A request body is used on the client side to generate the request body using a block.

As the response body isn’t available until the request is sent, the response body must be #streamed into the request body.

Instance Method Summary collapse

Methods inherited from Body

#call, #close_input, #close_output, #read, #stream?

Methods inherited from Readable

#as_json, #buffered, #call, #discard, #each, #empty?, #finish, #join, #length, #read, #ready?, #rewind, #rewindable?, #stream?, #to_json

Constructor Details

#initialize(block) ⇒ RequestBody

Initialize the request body with the given block.



169
170
171
# File 'lib/protocol/http/body/streamable.rb', line 169

def initialize(block)
	super(block, Writable.new)
end

Instance Method Details

#close(error = nil) ⇒ Object

Close will be invoked when all the input is read.



174
175
176
# File 'lib/protocol/http/body/streamable.rb', line 174

def close(error = nil)
	self.close_input(error)
end

#stream(body) ⇒ Object

Stream the response body into the block’s input.



179
180
181
182
183
184
185
186
# File 'lib/protocol/http/body/streamable.rb', line 179

def stream(body)
	body&.each do |chunk|
		@input.write(chunk)
	end
rescue => error
ensure
	@input.close_write(error)
end