diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2015-11-03 17:48:40 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2015-11-03 17:48:40 +0900 |
commit | b29910e76398a5fb542cdafdf134ab886b37c809 (patch) | |
tree | a01d95489282f0f4130486d77802d4d524d49b39 | |
parent | be694f1c01ce8d28b50b11ee703bc28f3a61b6a9 (diff) | |
download | plum-b29910e76398a5fb542cdafdf134ab886b37c809.tar.gz |
add docs for Plum::Client
-rw-r--r-- | lib/plum/client.rb | 35 | ||||
-rw-r--r-- | lib/plum/client/connection.rb | 4 | ||||
-rw-r--r-- | test/plum/server/test_http_connection.rb (renamed from test/plum/test_http_connection.rb) | 0 | ||||
-rw-r--r-- | test/plum/server/test_https_connection.rb (renamed from test/plum/test_https_connection.rb) | 4 |
4 files changed, 30 insertions, 13 deletions
diff --git a/lib/plum/client.rb b/lib/plum/client.rb index 853b049..cd1b40e 100644 --- a/lib/plum/client.rb +++ b/lib/plum/client.rb @@ -8,11 +8,16 @@ module Plum attr_reader :host, :port, :config attr_reader :socket + # Creates a new HTTP client and starts communication. + # A shorthand for `Plum::Client.new(args).start(&block)` def self.start(host, port, config = {}, &block) client = self.new(host, port, config) client.start(&block) end + # @param host [String] the host to connect + # @param port [Integer] the port number to connect + # @param config [Hash<Symbol, Object>] the client configuration def initialize(host, port, config = {}) @host = host @port = port @@ -21,7 +26,9 @@ module Plum @responses = {} end - def start + # Starts communication. + # If block passed, waits for asynchronous requests and closes the connection after calling the block. + def start(&block) raise IOError, "Session already started" if @started _start if block_given? @@ -36,12 +43,17 @@ module Plum self end - def wait - while !@responses.empty? - _succ + # Waits for the asynchronous response(s) to finish. + # @param response [Response] if specified, waits only for the response + def wait(response = nil) + if response + _succ while !response.finished? + else + _succ while !@responses.empty? end end + # Closes the connection. def close begin @plum.close if @plum @@ -50,6 +62,10 @@ module Plum end end + # Creates a new HTTP request. + # @param headers [Hash<String, String>] the request headers + # @param body [String] the request body + # @param block [Proc] if specified, calls the block when finished def request_async(headers, body = nil, &block) stream = @plum.open_stream response = Response.new @@ -69,9 +85,12 @@ module Plum response end + # Creates a new HTTP request and waits for the response + # @param headers [Hash<String, String>] the request headers + # @param body [String] the request body def request(headers, body = nil) response = request_async(headers, body) - _succ while !response.finished? + wait(response) response end @@ -85,15 +104,11 @@ module Plum end } - def https? - !!@config[:https] - end - private def _start @started = true sock = TCPSocket.open(host, port) - if https? + if config[:https] ctx = @config[:ssl_context] || new_ssl_ctx sock = OpenSSL::SSL::SSLSocket.new(sock, ctx) sock.sync_close = true diff --git a/lib/plum/client/connection.rb b/lib/plum/client/connection.rb index fc96a9a..bab2dc0 100644 --- a/lib/plum/client/connection.rb +++ b/lib/plum/client/connection.rb @@ -10,9 +10,11 @@ module Plum @state = :waiting_settings end + # Create a new stream for HTTP request. + # @param args [Hash] the argument for Stream.new def open_stream(**args) next_id = @max_odd_stream_id > 0 ? @max_odd_stream_id + 2 : 1 - stream = new_stream(next_id, state: :open, **args) + stream = new_stream(next_id, **args) stream end end diff --git a/test/plum/test_http_connection.rb b/test/plum/server/test_http_connection.rb index bd586a5..bd586a5 100644 --- a/test/plum/test_http_connection.rb +++ b/test/plum/server/test_http_connection.rb diff --git a/test/plum/test_https_connection.rb b/test/plum/server/test_https_connection.rb index 52a1cf5..5251e20 100644 --- a/test/plum/test_https_connection.rb +++ b/test/plum/server/test_https_connection.rb @@ -40,8 +40,8 @@ class HTTPSConnectionNegotiationTest < Minitest::Test ctx = OpenSSL::SSL::SSLContext.new ctx.alpn_select_cb = -> protocols { "h2" } - ctx.cert = OpenSSL::X509::Certificate.new File.read(File.expand_path("../../server.crt", __FILE__)) - ctx.key = OpenSSL::PKey::RSA.new File.read(File.expand_path("../../server.key", __FILE__)) + ctx.cert = OpenSSL::X509::Certificate.new File.read(File.expand_path("../../../server.crt", __FILE__)) + ctx.key = OpenSSL::PKey::RSA.new File.read(File.expand_path("../../../server.key", __FILE__)) tcp_server = TCPServer.new("127.0.0.1", LISTEN_PORT) ssl_server = OpenSSL::SSL::SSLServer.new(tcp_server, ctx) |