From 3c72a20d46f1ea39421cd55247eb732c380efa8e Mon Sep 17 00:00:00 2001 From: HoneyryderChuck Date: Mon, 18 Dec 2017 18:08:16 +0200 Subject: [PATCH] removing usage of ** params, as this forces typecast to hash, and one loses the options silently keeping the object passed in init, which is a big part of the plugins --- lib/httpx/chainable.rb | 3 ++- lib/httpx/client.rb | 14 +++++++++----- lib/httpx/options.rb | 2 -- lib/httpx/request.rb | 2 +- lib/httpx/response.rb | 2 +- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/httpx/chainable.rb b/lib/httpx/chainable.rb index 235c9493..8a3caa18 100644 --- a/lib/httpx/chainable.rb +++ b/lib/httpx/chainable.rb @@ -59,7 +59,7 @@ module HTTPX end def plugin(*plugins) - Class.new(Client).plugins(plugins).new(default_options) + Class.new(Client).plugins(plugins).new end alias :plugins :plugin @@ -71,6 +71,7 @@ module HTTPX # :nodoc: def branch(options) + return self.class.new(options) if self.is_a?(Client) Client.new(options) end end diff --git a/lib/httpx/client.rb b/lib/httpx/client.rb index c469ae73..a744acbc 100644 --- a/lib/httpx/client.rb +++ b/lib/httpx/client.rb @@ -4,7 +4,7 @@ module HTTPX class Client include Chainable - def initialize(**options) + def initialize(options = {}) @default_options = self.class.default_options.merge(options) @connection = Connection.new(@default_options) if block_given? @@ -34,22 +34,21 @@ module HTTPX private def __build_reqs(*args, **options) - rklass = @default_options.request_class case args.size when 1 reqs = args.first requests = reqs.map do |verb, uri, opts = {}| - rklass.new(verb, uri, **@default_options.merge(options.merge(opts))) + __build_req(verb, uri, options.merge(opts)) end when 2, 3 verb, uris, opts = args opts ||= {} if uris.respond_to?(:each) requests = uris.map do |uri| - rklass.new(verb, uri, **@default_options.merge(options.merge(opts))) + __build_req(verb, uri, options.merge(opts)) end else - [rklass.new(verb, uris, **@default_options.merge(options.merge(opts)))] + [ __build_req(verb, uris, options.merge(opts)) ] end else raise ArgumentError, "unsupported number of arguments" @@ -72,6 +71,11 @@ module HTTPX requests.size == 1 ? responses.first : responses end + def __build_req(verb, uri, options = {}) + rklass = @default_options.request_class + rklass.new(verb, uri, @default_options.merge(options)) + end + @default_options = Options.new @plugins = [] diff --git a/lib/httpx/options.rb b/lib/httpx/options.rb index 465b602c..67824cbb 100644 --- a/lib/httpx/options.rb +++ b/lib/httpx/options.rb @@ -24,8 +24,6 @@ module HTTPX @defined_options ||= [] end - protected - def def_option(name, &interpreter) defined_options << name.to_sym interpreter ||= lambda { |v| v } diff --git a/lib/httpx/request.rb b/lib/httpx/request.rb index b022e718..0417e4b5 100644 --- a/lib/httpx/request.rb +++ b/lib/httpx/request.rb @@ -39,7 +39,7 @@ module HTTPX def_delegator :@body, :empty? - def initialize(verb, uri, **options) + def initialize(verb, uri, options = {}) @verb = verb.to_s.downcase.to_sym @uri = URI(uri) @options = Options.new(options) diff --git a/lib/httpx/response.rb b/lib/httpx/response.rb index 31ad8853..5383c453 100644 --- a/lib/httpx/response.rb +++ b/lib/httpx/response.rb @@ -19,7 +19,7 @@ module HTTPX def_delegator :@body, :close - def initialize(request, status, headers, **options) + def initialize(request, status, headers, options = {}) @options = Options.new(options) @request = request @status = Integer(status)