Class: Plum::Response
- Inherits:
-
Object
- Object
- Plum::Response
- Defined in:
- lib/plum/client/response.rb
Instance Attribute Summary collapse
-
#headers ⇒ Hash<String, String>
readonly
The response headers.
Instance Method Summary collapse
-
#[](key) ⇒ String
Returns the header value that correspond to the header name.
- #_chunk(encoded) ⇒ Object private
- #_fail ⇒ Object private
- #_finish ⇒ Object private
- #_headers(raw_headers) ⇒ Object private
-
#body ⇒ String
Returns the complete response body.
-
#failed? ⇒ Boolean
Returns whether the request has failed or not.
-
#finished? ⇒ Boolean
Returns whether the response is complete or not.
-
#initialize(auto_decode: true, **options) ⇒ Response
constructor
private
A new instance of Response.
-
#on_chunk {|chunk| ... } ⇒ Object
Set callback tha called when received a chunk of response body.
-
#on_finish(&block) ⇒ Object
Set callback that will be called when the response finished.
-
#status ⇒ String
Returns the HTTP status code.
Constructor Details
#initialize(auto_decode: true, **options) ⇒ Response
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Response.
9 10 11 12 13 14 15 |
# File 'lib/plum/client/response.rb', line 9 def initialize(auto_decode: true, **) @body = Queue.new @finished = false @failed = false @body = [] @auto_decode = auto_decode end |
Instance Attribute Details
#headers ⇒ Hash<String, String> (readonly)
The response headers
6 7 8 |
# File 'lib/plum/client/response.rb', line 6 def headers @headers end |
Instance Method Details
#[](key) ⇒ String
Returns the header value that correspond to the header name.
26 27 28 |
# File 'lib/plum/client/response.rb', line 26 def [](key) @headers[key.to_s.downcase] end |
#_chunk(encoded) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
80 81 82 83 84 85 86 87 |
# File 'lib/plum/client/response.rb', line 80 def _chunk(encoded) chunk = @decoder.decode(encoded) if @on_chunk @on_chunk.call(chunk) else @body << chunk end end |
#_fail ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
97 98 99 |
# File 'lib/plum/client/response.rb', line 97 def _fail @failed = true end |
#_finish ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
90 91 92 93 94 |
# File 'lib/plum/client/response.rb', line 90 def _finish @finished = true @decoder.finish @on_finish.call if @on_finish end |
#_headers(raw_headers) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
73 74 75 76 77 |
# File 'lib/plum/client/response.rb', line 73 def _headers(raw_headers) # response headers should not have duplicates @headers = raw_headers.to_h.freeze @decoder = setup_decoder end |
#body ⇒ String
Returns the complete response body. Use #each_body instead if the body can be very large.
66 67 68 69 70 |
# File 'lib/plum/client/response.rb', line 66 def body raise "Body already read" if @on_chunk raise "Response body is not complete" unless finished? @body.join end |
#failed? ⇒ Boolean
Returns whether the request has failed or not.
38 39 40 |
# File 'lib/plum/client/response.rb', line 38 def failed? @failed end |
#finished? ⇒ Boolean
Returns whether the response is complete or not.
32 33 34 |
# File 'lib/plum/client/response.rb', line 32 def finished? @finished end |
#on_chunk {|chunk| ... } ⇒ Object
Set callback tha called when received a chunk of response body.
44 45 46 47 48 49 50 51 52 |
# File 'lib/plum/client/response.rb', line 44 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(&block) ⇒ Object
Set callback that will be called when the response finished.
55 56 57 58 59 60 61 62 |
# File 'lib/plum/client/response.rb', line 55 def on_finish(&block) raise ArgumentError, "block must be given" unless block_given? if finished? yield else @on_finish = block end end |
#status ⇒ String
Returns the HTTP status code.
19 20 21 |
# File 'lib/plum/client/response.rb', line 19 def status @headers && @headers[":status"] end |