aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2015-11-06 11:15:54 +0900
committerKazuki Yamaguchi <k@rhe.jp>2015-11-06 11:15:54 +0900
commit7d4afee71eee8f1c242d400affb278e3bd4fd276 (patch)
tree5805c94e11b63667465bbe7bcaeb3848eacf01ca /lib
parent4c26fd8e0c8bde6bac5a649629c79a6db82cd06f (diff)
downloadplum-7d4afee71eee8f1c242d400affb278e3bd4fd276.tar.gz
client: rename each_chunk to on_chunk
Diffstat (limited to 'lib')
-rw-r--r--lib/plum/client/response.rb36
1 files changed, 19 insertions, 17 deletions
diff --git a/lib/plum/client/response.rb b/lib/plum/client/response.rb
index ee23971..8270e95 100644
--- a/lib/plum/client/response.rb
+++ b/lib/plum/client/response.rb
@@ -10,7 +10,7 @@ module Plum
@body = Queue.new
@finished = false
@failed = false
- @body_read = false
+ @body = []
end
# Return the HTTP status code.
@@ -38,26 +38,26 @@ module Plum
@failed
end
- # Yields a chunk of the response body until EOF.
+ # Set callback tha called when received a chunk of response body.
# @yield [chunk] A chunk of the response body.
- def each_chunk(&block)
- raise "Body already read" if @body_read
- @body_read = true
- while !(finished? && @body.empty?) && chunk = @body.pop
- if Exception === chunk
- raise chunk
- else
- yield chunk
- end
+ def on_chunk(&block)
+ raise "Body already read" if @on_chunk
+ @on_chunk = block
+ unless @body.empty?
+ @body.each(&block)
+ @body.clear
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_chunk { |chunk| body << chunk }
- body
+ raise "Body already read" if @on_chunk
+ if finished?
+ @body.join
+ else
+ raise "Response body is not complete"
+ end
end
# @api private
@@ -68,19 +68,21 @@ module Plum
# @api private
def _chunk(chunk)
- @body << chunk
+ if @on_chunk
+ @on_chunk.call(chunk)
+ else
+ @body << chunk
+ end
end
# @api private
def _finish
@finished = true
- @body << nil # @body.close is not implemented in Ruby 2.2
end
# @api private
def _fail(ex)
@failed = true
- @body << ex
end
end
end