class Plum::Response
Attributes
headers[R]
The response headers @return [Hash<String, String>]
Public Class Methods
new(auto_decode: true, **options)
click to toggle source
@api private
# File lib/plum/client/response.rb, line 8 def initialize(auto_decode: true, **options) @body = Queue.new @finished = false @failed = false @body = [] @auto_decode = auto_decode end
Public Instance Methods
[](key)
click to toggle source
Returns the header value that correspond to the header name. @param key [String] the header name @return [String] the header value
# File lib/plum/client/response.rb, line 25 def [](key) @headers[key.to_s.downcase] end
_chunk(encoded)
click to toggle source
@api private
# File lib/plum/client/response.rb, line 79 def _chunk(encoded) chunk = @decoder.decode(encoded) if @on_chunk @on_chunk.call(chunk) else @body << chunk end end
_fail()
click to toggle source
@api private
# File lib/plum/client/response.rb, line 96 def _fail @failed = true end
_finish()
click to toggle source
@api private
# File lib/plum/client/response.rb, line 89 def _finish @finished = true @decoder.finish @on_finish.call if @on_finish end
_headers(raw_headers)
click to toggle source
@api private
# File lib/plum/client/response.rb, line 72 def _headers(raw_headers) # response headers should not have duplicates @headers = raw_headers.to_h.freeze @decoder = setup_decoder end
body()
click to toggle source
Returns the complete response body. Use each_body instead if the body can be very large. @return [String] the whole response body
# File lib/plum/client/response.rb, line 65 def body raise "Body already read" if @on_chunk raise "Response body is not complete" unless finished? @body.join end
failed?()
click to toggle source
Returns whether the request has failed or not. @return [Boolean]
# File lib/plum/client/response.rb, line 37 def failed? @failed end
finished?()
click to toggle source
Returns whether the response is complete or not. @return [Boolean]
# File lib/plum/client/response.rb, line 31 def finished? @finished end
on_chunk(&block)
click to toggle source
Set callback tha called when received a chunk of response body. @yield [chunk] A chunk of the response body.
# File lib/plum/client/response.rb, line 43 def on_chunk(&block) raise "Body already read" if @on_chunk raise ArgumentError, "block must be given" unless block_given? @on_chunk = block unless @body.empty? @body.each(&block) @body.clear end end
on_finish() { || ... }
click to toggle source
Set callback that will be called when the response finished.
# File lib/plum/client/response.rb, line 54 def on_finish(&block) raise ArgumentError, "block must be given" unless block_given? if finished? yield else @on_finish = block end end
status()
click to toggle source
Returns the HTTP status code. @return [String] the HTTP status code
# File lib/plum/client/response.rb, line 18 def status @headers && @headers[":status"] end
Private Instance Methods
setup_decoder()
click to toggle source
# File lib/plum/client/response.rb, line 101 def setup_decoder if @auto_decode klass = Decoders::DECODERS[@headers["content-encoding"]] end klass ||= Decoders::Base klass.new end