Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow call #persistent with block #201

Merged
merged 2 commits into from
Apr 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## master (unreleased)

* _the future is unwritten_
* Add block-form `#persistent` calls. See #200, #201. (@ixti)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL Joe Strummer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:D



## 0.8.0 (2015-04-01)
Expand Down
38 changes: 34 additions & 4 deletions lib/http/chainable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,41 @@ def request(verb, uri, options = {})
branch(options).request verb, uri
end

# Flag as persistent
# @param [String] host
# @raise [Request::Error] if Host is invalid
# @overload persistent(host)
# Flags as persistent
# @param [String] host
# @raise [Request::Error] if Host is invalid
# @return [HTTP::Client] Persistent client
# @overload persistent(host, &block)
# Executes given block with persistent client and automatically closes
# connection at the end of execution.
#
# @example
#
# def keys(users)
# HTTP.persistent("https://github.com") do |http|
# users.map { |u| http.get("/#{u}.keys").to_s }
# end
# end
#
# # same as
#
# def keys(users)
# http = HTTP.persistent "https://github.com"
# users.map { |u| http.get("/#{u}.keys").to_s }
# ensure
# http.close if http
# end
#
#
# @yieldparam [HTTP::Client] client Persistent client
# @return [Object] result of last expression in the block
def persistent(host)
branch default_options.with_persistent host
p_client = branch default_options.with_persistent host
return p_client unless block_given?
yield p_client
ensure
p_client.close
end

# Make a request through an HTTP proxy
Expand Down
16 changes: 11 additions & 5 deletions lib/http/client.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "forwardable"

require "cgi"
require "uri"

Expand All @@ -10,6 +12,7 @@
module HTTP
# Clients make requests and receive responses
class Client
extend Forwardable
include Chainable

CONNECTION = "Connection".freeze
Expand Down Expand Up @@ -58,6 +61,11 @@ def perform(req, options)
end
end

# @!method persistent?
# @see Options#persistent?
# @return [Boolean] whenever client is persistent
def_delegator :default_options, :persistent?

def make_request(req, options)
verify_connection!(req.uri)

Expand All @@ -79,12 +87,10 @@ def make_request(req, options)
@state = :clean

res

# On any exception we reset the conn. This is a safety measure, to ensure
# we don't have conns in a bad state resulting in mixed requests/responses
rescue
close if default_options.persistent?

# On any exception we reset the conn. This is a safety measure, to ensure
# we don't have conns in a bad state resulting in mixed requests/responses
close if persistent?
raise
end

Expand Down
23 changes: 23 additions & 0 deletions spec/lib/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,27 @@
expect(client.default_options[:cache]).to eq cache
end
end

describe ".persistent" do
let(:host) { "https://api.github.com" }

context "with host only given" do
subject { HTTP.persistent host }
it { is_expected.to be_an HTTP::Client }
it { is_expected.to be_persistent }
end

context "with host and block given" do
it "returns last evaluation of last expression" do
expect(HTTP.persistent(host) { :http }).to be :http
end

it "auto-closes connection" do
HTTP.persistent host do |client|
expect(client).to receive(:close).and_call_original
client.get("/repos/httprb/http.rb")
end
end
end
end
end