lib/httpx/options.rb in httpx-0.6.7 vs lib/httpx/options.rb in httpx-0.7.0

- old
+ new

@@ -1,10 +1,9 @@ # frozen_string_literal: true module HTTPX class Options - MAX_CONCURRENT_REQUESTS = 100 WINDOW_SIZE = 1 << 14 # 16K MAX_BODY_THRESHOLD_SIZE = (1 << 10) * 112 # 112K class << self def inherited(klass) @@ -26,11 +25,18 @@ def def_option(name, &interpreter) defined_options << name.to_sym interpreter ||= ->(v) { v } - attr_accessor name + attr_reader name + + define_method(:"#{name}=") do |value| + return if value.nil? + + instance_variable_set(:"@#{name}", instance_exec(value, &interpreter)) + end + protected :"#{name}=" define_method(:"with_#{name}") do |value| other = dup other.send(:"#{name}=", other.instance_exec(value, &interpreter)) @@ -46,11 +52,10 @@ :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), @@ -64,41 +69,52 @@ :resolver_options => { cache: true }, } defaults.merge!(options) defaults[:headers] = Headers.new(defaults[:headers]) - defaults.each { |(k, v)| self[k] = v } + defaults.each do |(k, v)| + __send__(:"#{k}=", v) + end end def_option(:headers) do |headers| - self.headers.merge(headers) + if self.headers + self.headers.merge(headers) + else + headers + end end def_option(:timeout) do |opts| - self.timeout = Timeout.new(opts) + Timeout.new(opts) end def_option(:max_concurrent_requests) do |num| - max = Integer(num) - raise Error, ":max_concurrent_requests must be positive" unless max.positive? + raise Error, ":max_concurrent_requests must be positive" unless num.positive? - self.max_concurrent_requests = max + num end + def_option(:max_requests) do |num| + raise Error, ":max_requests must be positive" unless num.positive? + + num + end + def_option(:window_size) do |num| - self.window_size = Integer(num) + Integer(num) end def_option(:body_threshold_size) do |num| - self.body_threshold_size = Integer(num) + Integer(num) 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 + transport end %w[ params form json body follow ssl http2_settings @@ -169,14 +185,8 @@ response_class.freeze headers_class.freeze request_body_class.freeze response_body_class.freeze connection_class.freeze - end - - protected - - def []=(option, val) - send(:"#{option}=", val) end end end