lib/httpx/session.rb in httpx-0.8.2 vs lib/httpx/session.rb in httpx-0.9.0
- old
+ new
@@ -3,11 +3,13 @@
module HTTPX
class Session
include Loggable
include Chainable
- def initialize(options = {}, &blk)
+ EMPTY_HASH = {}.freeze
+
+ def initialize(options = EMPTY_HASH, &blk)
@options = self.class.default_options.merge(options)
@responses = {}
@persistent = @options.persistent
wrap(&blk) if block_given?
end
@@ -27,17 +29,25 @@
def close(*args)
pool.close(*args)
end
def request(*args, **options)
- requests = build_requests(*args, options)
+ requests = args.first.is_a?(Request) ? args : build_requests(*args, options)
responses = send_requests(*requests, options)
return responses.first if responses.size == 1
responses
end
+ def build_request(verb, uri, options = EMPTY_HASH)
+ rklass = @options.request_class
+ request = rklass.new(verb, uri, @options.merge(options).merge(persistent: @persistent))
+ request.on(:response, &method(:on_response).curry[request])
+ request.on(:promise, &method(:on_promise))
+ request
+ end
+
private
def pool
Thread.current[:httpx_connection_pool] ||= Pool.new
end
@@ -120,12 +130,12 @@
request_options = @options.merge(options)
requests = case args.size
when 1
reqs = args.first
- reqs.map do |verb, uri|
- build_request(verb, uri, request_options)
+ reqs.map do |verb, uri, opts = EMPTY_HASH|
+ build_request(verb, uri, request_options.merge(opts))
end
when 2, 3
verb, uris = args
if uris.respond_to?(:each)
uris.map do |uri, **opts|
@@ -191,17 +201,9 @@
end
responses
ensure
close(connections) unless @persistent
end
- end
-
- def build_request(verb, uri, options)
- rklass = @options.request_class
- request = rklass.new(verb, uri, @options.merge(options).merge(persistent: @persistent))
- request.on(:response, &method(:on_response).curry[request])
- request.on(:promise, &method(:on_promise))
- request
end
@default_options = Options.new
@default_options.freeze
@plugins = []