Module: Protocol::HTTP::Body::Reader

Included in:
Request, Response
Defined in:
lib/protocol/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)

79
80
81
# File 'lib/protocol/http/body/reader.rb', line 79

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.


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

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.


30
31
32
33
34
35
# File 'lib/protocol/http/body/reader.rb', line 30

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.


50
51
52
53
54
55
56
57
# File 'lib/protocol/http/body/reader.rb', line 50

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.


39
40
41
42
43
44
45
46
# 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) ⇒ Object

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


60
61
62
63
64
65
66
67
68
# 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