module Protocol::HTTP::Body::Reader
General operations for interacting with a request or response body.
Public Instance Methods
body?()
click to toggle source
Whether there is a body?
# File lib/protocol/http/body/reader.rb, line 79 def body? @body and !@body.empty? end
close(error = nil)
click to toggle source
Close the connection as quickly as possible. Discards body. May close the underlying connection if necessary to terminate the stream.
# File lib/protocol/http/body/reader.rb, line 71 def close(error = nil) if @body @body.close(error) @body = nil end end
each(&block)
click to toggle source
Read chunks from the body. @yield [String] read chunks from the body.
# File lib/protocol/http/body/reader.rb, line 30 def each(&block) if @body @body.each(&block) @body = nil end end
finish()
click to toggle source
Gracefully finish reading the body. This will buffer the remainder of the body. @return [Buffered] buffers the entire body.
# File lib/protocol/http/body/reader.rb, line 50 def finish if @body body = @body.finish @body = nil return body end end
read()
click to toggle source
Reads the entire request/response body. @return [String] the entire body as a string.
# File lib/protocol/http/body/reader.rb, line 39 def read if @body buffer = @body.join @body = nil return buffer end end
save(path, mode = ::File::WRONLY|::File::CREAT, *args)
click to toggle source
Write the body of the response to the given file path.
# File lib/protocol/http/body/reader.rb, line 60 def save(path, mode = ::File::WRONLY|::File::CREAT, *args) if @body ::File.open(path, mode, *args) do |file| self.each do |chunk| file.write(chunk) end end end end