diff options
-rw-r--r-- | README.md | 15 | ||||
-rw-r--r-- | lib/plum/client.rb | 36 | ||||
-rw-r--r-- | test/plum/client/test_client.rb | 11 |
3 files changed, 32 insertions, 30 deletions
@@ -1,4 +1,4 @@ -# Plum: An HTTP/2 Library for Ruby + Plum: An HTTP/2 Library for Ruby A pure Ruby HTTP/2 server and client implementation. WARNING: Plum is currently under heavy development. You *will* encounter bugs when using it. @@ -54,9 +54,9 @@ If the server does't support HTTP/2, `Plum::Client` tries to use HTTP/1.x instea ##### Sequential request ```ruby client = Plum::Client.start("http2.rhe.jp", 443, user_agent: "nyaan") -res1 = client.get("/", headers: { "accept" => "*/*" }) +res1 = client.get!("/", headers: { "accept" => "*/*" }) puts res1.body # => "..." -res2 = client.post("/post", "data") +res2 = client.post!("/post", "data") puts res2.body # => "..." client.close @@ -66,8 +66,9 @@ client.close ```ruby res1 = res2 = nil Plum::Client.start("rhe.jp", http2_settings: { max_frame_size: 32768 }) { |client| - res1 = client.get_async("/") - res2 = client.post_async("/post", "data") + res1 = client.get("/") + res2 = client.post("/post", "data") + # res1.status == nil ; because it's async request } # wait for response(s) and close p res1.status # => "200" @@ -76,10 +77,10 @@ p res1.status # => "200" ##### Download a large file ```ruby Plum::Client.start("http2.rhe.jp", hostname: "assets.rhe.jp") { |client| - client.get_async("/large") do |res| # called when received response headers + client.get("/large") do |res| # called when received response headers p res.status # => "200" File.open("/tmp/large.file", "wb") { |file| - res.on_chunk do |chunk| + res.on_chunk do |chunk| # called when each chunk of response body arrived file << chunk end } diff --git a/lib/plum/client.rb b/lib/plum/client.rb index 233ad4b..cc255f4 100644 --- a/lib/plum/client.rb +++ b/lib/plum/client.rb @@ -83,49 +83,49 @@ module Plum @session.request(headers, body, options, &block) end - # @!method get - # @!method head - # @!method delete + # @!method get! + # @!method head! + # @!method delete! # @param path [String] the absolute path to request (translated into :path header) # @param options [Hash<Symbol, Object>] the request options # @param block [Proc] if specified, calls the block when finished - # Shorthand method for `#request` + # Shorthand method for `Client#resume(Client#request(*args))` - # @!method get_async - # @!method head_async - # @!method delete_async + # @!method get + # @!method head + # @!method delete # @param path [String] the absolute path to request (translated into :path header) # @param options [Hash<Symbol, Object>] the request options # @param block [Proc] if specified, calls the block when finished - # Shorthand method for `#request_async` + # Shorthand method for `#request` %w(GET HEAD DELETE).each { |method| - define_method(:"#{method.downcase}") do |path, options = {}, &block| + define_method(:"#{method.downcase}!") do |path, options = {}, &block| resume _request_helper(method, path, nil, options, &block) end - define_method(:"#{method.downcase}_async") do |path, options = {}, &block| + define_method(:"#{method.downcase}") do |path, options = {}, &block| _request_helper(method, path, nil, options, &block) end } - # @!method post - # @!method put + # @!method post! + # @!method put! # @param path [String] the absolute path to request (translated into :path header) # @param body [String] the request body # @param options [Hash<Symbol, Object>] the request options # @param block [Proc] if specified, calls the block when finished - # Shorthand method for `#request` + # Shorthand method for `Client#resume(Client#request(*args))` - # @!method post_async - # @!method put_async + # @!method post + # @!method put # @param path [String] the absolute path to request (translated into :path header) # @param body [String] the request body # @param options [Hash<Symbol, Object>] the request options # @param block [Proc] if specified, calls the block when finished - # Shorthand method for `#request_async` + # Shorthand method for `#request` %w(POST PUT).each { |method| - define_method(:"#{method.downcase}") do |path, body, options = {}, &block| + define_method(:"#{method.downcase}!") do |path, body, options = {}, &block| resume _request_helper(method, path, body, options, &block) end - define_method(:"#{method.downcase}_async") do |path, body, options = {}, &block| + define_method(:"#{method.downcase}") do |path, body, options = {}, &block| _request_helper(method, path, body, options, &block) end } diff --git a/test/plum/client/test_client.rb b/test/plum/client/test_client.rb index 8806052..6d6586a 100644 --- a/test/plum/client/test_client.rb +++ b/test/plum/client/test_client.rb @@ -5,7 +5,7 @@ class ClientTest < Minitest::Test def test_request_sync server_thread = start_tls_server client = Client.start("127.0.0.1", LISTEN_PORT, https: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) - res1 = client.put("/", "aaa", headers: { "header" => "ccc" }) + res1 = client.put!("/", "aaa", headers: { "header" => "ccc" }) assert_equal("PUTcccaaa", res1.body) client.close ensure @@ -23,7 +23,8 @@ class ClientTest < Minitest::Test } assert_nil(res1.headers) - res2 = client.get_async("/", headers: { "header" => "ccc" }) + res2 = client.get("/", headers: { "header" => "ccc" }) + assert_nil(res2.headers) } assert(res2.headers) assert_equal("GETccc", res2.body) @@ -47,7 +48,7 @@ class ClientTest < Minitest::Test Client.start("127.0.0.1", LISTEN_PORT, https: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) { |c| client = c assert_raises(LocalConnectionError) { - client.get("/connection_error") + client.get!("/connection_error") } } ensure @@ -57,7 +58,7 @@ class ClientTest < Minitest::Test def test_raise_error_async_seq_resume server_thread = start_tls_server client = Client.start("127.0.0.1", LISTEN_PORT, https: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) - res = client.get_async("/error_in_data") + res = client.get("/error_in_data") assert_raises(LocalConnectionError) { client.resume(res) } @@ -72,7 +73,7 @@ class ClientTest < Minitest::Test assert_raises(LocalConnectionError) { Client.start("127.0.0.1", LISTEN_PORT, https: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) { |c| client = c - client.get_async("/connection_error") { |res| flunk "success??" } + client.get("/connection_error") { |res| flunk "success??" } } # resume } ensure |