Module: Async::HTTP::Body::Reader

Included in:
Request, Response
Defined in:
lib/async/http/body/reader.rb

Overview

General operations for interacting with a request or response body.

Instance Method Summary collapse

Instance Method Details

#body?Boolean

Whether there is a body?

Returns:

  • (Boolean)


77
78
79
# File 'lib/async/http/body/reader.rb', line 77

def body?
	@body and !@body.empty?
end

#close(error = nil) ⇒ Object

Close the connection as quickly as possible. Discards body. May close the underlying connection if necessary to terminate the stream.



69
70
71
72
73
74
# File 'lib/async/http/body/reader.rb', line 69

def close(error = nil)
	if @body
		@body.close(error)
		@body = nil
	end
end

#each {|String| ... } ⇒ Object

Read chunks from the body.

Yields:

  • (String)

    read chunks from the body.



28
29
30
31
32
33
# File 'lib/async/http/body/reader.rb', line 28

def each(&block)
	if @body
		@body.each(&block)
		@body = nil
	end
end

#finishBuffered

Gracefully finish reading the body. This will buffer the remainder of the body.

Returns:

  • (Buffered)

    buffers the entire body.



48
49
50
51
52
53
54
55
# File 'lib/async/http/body/reader.rb', line 48

def finish
	if @body
		body = @body.finish
		@body = nil
		
		return body
	end
end

#readString

Reads the entire request/response body.

Returns:

  • (String)

    the entire body as a string.



37
38
39
40
41
42
43
44
# File 'lib/async/http/body/reader.rb', line 37

def read
	if @body
		buffer = @body.join
		@body = nil
		
		return buffer
	end
end

#save(path, mode = ::File::WRONLY|::File::CREAT, *args) ⇒ Object

Write the body of the response to the given file path.



58
59
60
61
62
63
64
65
66
# File 'lib/async/http/body/reader.rb', line 58

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