aboutsummaryrefslogtreecommitdiffstats
path: root/lib/plum
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2015-11-03 17:24:59 +0900
committerKazuki Yamaguchi <k@rhe.jp>2015-11-03 17:24:59 +0900
commitbe694f1c01ce8d28b50b11ee703bc28f3a61b6a9 (patch)
treeae5b3abf451fe789b3876961913672f6f477b9c8 /lib/plum
parent3fb060945fb27a17024df8795d0712b3d3d714f2 (diff)
downloadplum-be694f1c01ce8d28b50b11ee703bc28f3a61b6a9.tar.gz
client/response: add test and docs
Diffstat (limited to 'lib/plum')
-rw-r--r--lib/plum/client/response.rb27
1 files changed, 24 insertions, 3 deletions
diff --git a/lib/plum/client/response.rb b/lib/plum/client/response.rb
index 0756546..56bff7e 100644
--- a/lib/plum/client/response.rb
+++ b/lib/plum/client/response.rb
@@ -1,23 +1,39 @@
# -*- frozen-string-literal: true -*-
module Plum
class Response
+ # The response headers
+ # @return [Hash<String, String>]
attr_reader :headers
+ # @api private
def initialize
@body = Queue.new
@finished = false
@body_read = false
end
+ # Return the HTTP status code.
+ # @return [String] the HTTP status code
def status
@headers && @headers[":status"]
end
+ # Returns the header value that correspond to the header name.
+ # @param key [String] the header name
+ # @return [String] the header value
+ def [](key)
+ @headers[key.to_s.downcase]
+ end
+
+ # Returns whether the response is complete or not.
+ # @return [Boolean]
def finished?
@finished
end
- def each_body(&block)
+ # Yields a chunk of the response body until EOF.
+ # @yield [chunk] A chunk of the response body.
+ def each_chunk(&block)
raise "Body already read" if @body_read
@body_read = true
while chunk = @body.pop
@@ -25,21 +41,26 @@ module Plum
end
end
+ # Returns the complete response body. Use #each_body instead if the body can be very large.
+ # @return [String] the whole response body
def body
body = String.new
- each_body { |chunk| body << chunk }
+ each_chunk { |chunk| body << chunk }
body
end
+ # @api private
def _headers(raw_headers)
# response headers should not have duplicates
- @headers = raw_headers.to_h
+ @headers = raw_headers.to_h.freeze
end
+ # @api private
def _chunk(chunk)
@body << chunk
end
+ # @api private
def _finish
@finished = true
@body << nil # @body.close is not implemented in Ruby 2.2