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, #inspect, #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.



185
186
187
# File 'lib/protocol/http/body/streamable.rb', line 185

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.



190
191
192
# File 'lib/protocol/http/body/streamable.rb', line 190

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

#stream(body) ⇒ Object

Stream the response body into the block’s input.



195
196
197
198
199
200
201
202
# File 'lib/protocol/http/body/streamable.rb', line 195

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