diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2016-05-08 15:13:49 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2016-05-08 15:24:17 +0900 |
commit | db7f141a7ea0d486599e02c839f12ddc72cdd152 (patch) | |
tree | 9c4e827a534a769c3b2311c82a67bc267b2d5afd /lib/plum | |
parent | cd269bf5693bcb9a7b5b301bbd3a0eb65222b9fc (diff) | |
download | plum-db7f141a7ea0d486599e02c839f12ddc72cdd152.tar.gz |
client: add Response#join method
This method waits until the response completes.
Diffstat (limited to 'lib/plum')
-rw-r--r-- | lib/plum/client.rb | 2 | ||||
-rw-r--r-- | lib/plum/client/client_session.rb | 2 | ||||
-rw-r--r-- | lib/plum/client/legacy_client_session.rb | 2 | ||||
-rw-r--r-- | lib/plum/client/response.rb | 12 |
4 files changed, 13 insertions, 5 deletions
diff --git a/lib/plum/client.rb b/lib/plum/client.rb index bdbfd72..cb968b4 100644 --- a/lib/plum/client.rb +++ b/lib/plum/client.rb @@ -33,7 +33,7 @@ module Plum @host = host @port = port || (config[:https] ? 443 : 80) end - @config = DEFAULT_CONFIG.merge(hostname: host).merge(config) + @config = DEFAULT_CONFIG.merge(hostname: host).merge!(config) @started = false end diff --git a/lib/plum/client/client_session.rb b/lib/plum/client/client_session.rb index 0c7e366..f21d2a6 100644 --- a/lib/plum/client/client_session.rb +++ b/lib/plum/client/client_session.rb @@ -42,7 +42,7 @@ module Plum ":scheme" => @config[:https] ? "https" : "http", }.merge(headers) - response = Response.new(**options, &headers_cb) + response = Response.new(self, **options, &headers_cb) @responses << response stream = @plum.open_stream stream.send_headers(headers, end_stream: !body) diff --git a/lib/plum/client/legacy_client_session.rb b/lib/plum/client/legacy_client_session.rb index 07ae6a9..3766ad3 100644 --- a/lib/plum/client/legacy_client_session.rb +++ b/lib/plum/client/legacy_client_session.rb @@ -39,7 +39,7 @@ module Plum end end - response = Response.new(**options, &headers_cb) + response = Response.new(self, **options, &headers_cb) @requests << [response, headers, body, chunked] consume_queue response diff --git a/lib/plum/client/response.rb b/lib/plum/client/response.rb index a091c7a..32a3d30 100644 --- a/lib/plum/client/response.rb +++ b/lib/plum/client/response.rb @@ -6,9 +6,9 @@ module Plum attr_reader :headers # @api private - def initialize(auto_decode: true, **options, &on_headers) + def initialize(session, auto_decode: true, **options, &on_headers) + @session = session @headers = nil - @body = Queue.new @finished = false @failed = false @body = [] @@ -48,6 +48,7 @@ module Plum raise ArgumentError, "block must be given" unless block_given? @on_headers = block yield self if @headers + self end # Set callback that will be called when received a chunk of response body. @@ -60,6 +61,7 @@ module Plum @body.each(&block) @body.clear end + self end # Set callback that will be called when the response finished. @@ -70,6 +72,7 @@ module Plum else @on_finish = block end + self end # Returns the complete response body. Use #each_body instead if the body can be very large. @@ -80,6 +83,11 @@ module Plum @body.join end + def join + @session.succ until (@finished || @failed) + self + end + private # internal: set headers and setup decoder def set_headers(headers) |