class HTTPX::Options
def ==(other)
def ==(other) ivars = instance_variables | other.instance_variables ivars.all? do |ivar| case ivar when :@headers headers = instance_variable_get(ivar) headers.same_headers?(other.instance_variable_get(ivar)) when *REQUEST_IVARS true else instance_variable_get(ivar) == other.instance_variable_get(ivar) end end end
def def_option(name, layout = nil, &interpreter)
def def_option(name, layout = nil, &interpreter) attr_reader name if layout class_eval(<<-OUT, __FILE__, __LINE__ + 1) def #{name}=(value) return if value.nil? value = begin #{layout} end @#{name} = value end OUT elsif interpreter define_method(:"#{name}=") do |value| return if value.nil? instance_variable_set(:"@#{name}", instance_exec(value, &interpreter)) end else attr_writer name end protected :"#{name}=" end
def freeze
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
def initialize(options = {})
def initialize(options = {}) defaults = DEFAULT_OPTIONS.merge(options) defaults.each do |(k, v)| next if v.nil? begin __send__(:"#{k}=", v) rescue NoMethodError raise Error, "unknown option: #{k}" end end end
def initialize_dup(other)
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 merge(other)
def merge(other) raise ArgumentError, "#{other.inspect} is not a valid set of options" unless other.respond_to?(:to_hash) h2 = other.to_hash return self if h2.empty? h1 = to_hash return self if h1 == h2 merged = h1.merge(h2) do |k, v1, v2| case k when :headers, :ssl, :http2_settings, :timeout v1.merge(v2) else v2 end end self.class.new(merged) end
def new(options = {})
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 to_hash
def to_hash hash_pairs = instance_variables.map do |ivar| [ivar[1..-1].to_sym, instance_variable_get(ivar)] end Hash[hash_pairs] end