lib/ezclient.rb in ezclient-0.1.0 vs lib/ezclient.rb in ezclient-0.2.0

- old
+ new

@@ -4,14 +4,13 @@ require "ezclient/version" require "ezclient/request" require "ezclient/response" class EzClient - def initialize(on_complete: nil, on_error: nil) + def initialize(options = {}) + self.options = options self.clients = {} - self.on_complete = on_complete - self.on_error = on_error end def request(verb, url, **options) keep_alive_timeout = options.delete(:keep_alive) @@ -19,17 +18,25 @@ client = persistent_client_for(url, timeout: keep_alive_timeout) else client = HTTP::Client.new end - Request.new(verb, url, client: client, on_complete: on_complete, on_error: on_error, **options) + Request.new(verb, url, client: client, **default_options, **options) end private - attr_accessor :clients, :on_complete, :on_error + attr_accessor :options, :clients def persistent_client_for(url, timeout: 600) uri = HTTP::URI.parse(url) clients[uri.origin] ||= HTTP.persistent(uri.origin, timeout: timeout) + end + + def default_options() + { + on_complete: options[:on_complete], + on_error: options[:on_error], + timeout: options[:default_timeout], + } end end