lib/httpx/options.rb in httpx-0.3.1 vs lib/httpx/options.rb in httpx-0.4.0

- old
+ new

@@ -14,10 +14,11 @@ def new(options = {}) # let enhanced options go through return options if self == Options && options.class > self return options if options.is_a?(self) + super end def defined_options @defined_options ||= [] @@ -29,35 +30,39 @@ attr_accessor name protected :"#{name}=" define_method(:"with_#{name}") do |value| - dup { |opts| opts.send(:"#{name}=", instance_exec(value, &interpreter)) } + other = dup + other.send(:"#{name}=", other.instance_exec(value, &interpreter)) + other end end end def initialize(options = {}) defaults = { - :debug => ENV.key?("HTTPX_DEBUG") ? $stderr : nil, - :debug_level => (ENV["HTTPX_DEBUG"] || 1).to_i, - :ssl => {}, - :http2_settings => { settings_enable_push: 0 }, - :fallback_protocol => "http/1.1", - :timeout => Timeout.new, - :headers => {}, - :max_concurrent_requests => MAX_CONCURRENT_REQUESTS, - :window_size => WINDOW_SIZE, - :body_threshold_size => MAX_BODY_THRESHOLD_SIZE, - :request_class => Class.new(Request), - :response_class => Class.new(Response), - :headers_class => Class.new(Headers), - :request_body_class => Class.new(Request::Body), - :response_body_class => Class.new(Response::Body), - :transport => nil, - :transport_options => nil, - :resolver_class => (ENV["HTTPX_RESOLVER"] || :native).to_sym, + :debug => ENV.key?("HTTPX_DEBUG") ? $stderr : nil, + :debug_level => (ENV["HTTPX_DEBUG"] || 1).to_i, + :ssl => {}, + :http2_settings => { settings_enable_push: 0 }, + :fallback_protocol => "http/1.1", + :timeout => Timeout.new, + :headers => {}, + :max_concurrent_requests => MAX_CONCURRENT_REQUESTS, + :window_size => WINDOW_SIZE, + :body_threshold_size => MAX_BODY_THRESHOLD_SIZE, + :request_class => Class.new(Request), + :response_class => Class.new(Response), + :headers_class => Class.new(Headers), + :request_body_class => Class.new(Request::Body), + :response_body_class => Class.new(Response::Body), + :connection_class => Class.new(Connection), + :transport => nil, + :transport_options => nil, + :persistent => false, + :resolver_class => (ENV["HTTPX_RESOLVER"] || :native).to_sym, } defaults.merge!(options) defaults[:headers] = Headers.new(defaults[:headers]) defaults.each { |(k, v)| self[k] = v } @@ -72,10 +77,11 @@ end def_option(:max_concurrent_requests) do |num| max = Integer(num) raise Error, ":max_concurrent_requests must be positive" unless max.positive? + self.max_concurrent_requests = max end def_option(:window_size) do |num| self.window_size = Integer(num) @@ -86,22 +92,31 @@ end def_option(:transport) do |tr| transport = tr.to_s raise Error, "#{transport} is an unsupported transport type" unless IO.registry.key?(transport) + self.transport = transport end %w[ params form json body follow ssl http2_settings - request_class response_class headers_class request_body_class response_body_class + request_class response_class headers_class request_body_class response_body_class connection_class io fallback_protocol debug debug_level transport_options resolver_class resolver_options + persistent ].each do |method_name| def_option(method_name) end + def ==(other) + ivars = instance_variables | other.instance_variables + ivars.all? do |ivar| + instance_variable_get(ivar) == other.instance_variable_get(ivar) + end + end + def merge(other) h1 = to_hash h2 = other.to_hash merged = h1.merge(h2) do |k, v1, v2| @@ -121,20 +136,31 @@ .defined_options .flat_map { |opt_name| [opt_name, send(opt_name)] } Hash[*hash_pairs] end - def dup - dupped = super - dupped.headers = headers.dup - dupped.ssl = ssl.dup - dupped.request_class = request_class.dup - dupped.response_class = response_class.dup - dupped.headers_class = headers_class.dup - dupped.request_body_class = request_body_class.dup - dupped.response_body_class = response_body_class.dup - yield(dupped) if block_given? - dupped + def initialize_dup(other) + self.headers = other.headers.dup + self.ssl = other.ssl.dup + self.request_class = other.request_class.dup + self.response_class = other.response_class.dup + self.headers_class = other.headers_class.dup + self.request_body_class = other.request_body_class.dup + self.response_body_class = other.response_body_class.dup + self.connection_class = other.connection_class.dup + end + + def freeze + super + + headers.freeze + ssl.freeze + request_class.freeze + response_class.freeze + headers_class.freeze + request_body_class.freeze + response_body_class.freeze + connection_class.freeze end protected def []=(option, val)