class HTTP::Options

Configuration options for HTTP requests and clients

def argument_error!(message)

Returns:
  • (void) -

Other tags:
    Api: - private
def argument_error!(message)
  error = Error.new(message)
  error.set_backtrace(caller(1) || [])
  raise error
end

def assign_options(env)

Returns:
  • (void) -

Other tags:
    Api: - private

Parameters:
  • env (Binding) -- binding from initialize with keyword argument values
def assign_options(env)
  self.class.defined_options.each do |name|
    value = env.local_variable_get(name)
    value = Headers.coerce(value) if name.eql?(:headers)
    __send__(:"#{name}=", value)
  end
end

def base_uri=(value)

Returns:
  • (HTTP::URI, nil) -

Other tags:
    Api: - private

Parameters:
  • value (String, HTTP::URI, nil) --
def base_uri=(value)
  @base_uri = value ? parse_base_uri(value) : nil
  validate_base_uri_and_persistent!
end

def base_uri?

Returns:
  • (Boolean) -

Other tags:
    Api: - public
def base_uri?
  !base_uri.nil?
end

def def_option(name, reader_only: false, &interpreter)

Returns:
  • (void) -

Other tags:
    Api: - private

Parameters:
  • reader_only (Boolean) --
  • name (Symbol) --
def def_option(name, reader_only: false, &interpreter)
  defined_options << name.to_sym
  interpreter ||= ->(v) { v }
  def_option_accessor(name, reader_only: reader_only)
  define_method(:"with_#{name}") do |value|
    dup { |opts| opts.send(:"#{name}=", instance_exec(value, &interpreter)) } # steep:ignore
  end
end

def def_option_accessor(name, reader_only:)

Other tags:
    Api: - private

Returns:
  • (void) -
def def_option_accessor(name, reader_only:)
  if reader_only
    attr_reader name
  else
    attr_accessor name
    protected :"#{name}="
  end
end

def defined_options

Returns:
  • (Array) -

Other tags:
    Api: - semipublic
def defined_options
  @defined_options ||= []
end

def dup

Returns:
  • (HTTP::Options) -

Other tags:
    Api: - public
def dup
  dupped = super
  yield(dupped) if block_given?
  dupped
end

def feature(name)

Returns:
  • (Feature, nil) -

Other tags:
    Api: - public

Parameters:
  • name (Symbol) --
def feature(name)
  features[name]
end

def features=(features)

Returns:
  • (Hash) -

Other tags:
    Api: - private

Parameters:
  • features (Hash) --
def features=(features)
  result = {} #: Hash[Symbol, Feature]
  @features = features.each_with_object(result) do |(name, opts_or_feature), h|
    h[name] = if opts_or_feature.is_a?(Feature)
                opts_or_feature
              else
                unless (feature = self.class.available_features[name])
                  argument_error! "Unsupported feature: #{name}"
                end
                feature.new(**opts_or_feature) # steep:ignore
              end
  end
end

def follow=(value)

Returns:
  • (Hash, nil) -

Other tags:
    Api: - private

Parameters:
  • value (Boolean, Hash, nil) --
def follow=(value)
  @follow =
    if    !value                    then nil
    elsif true == value             then {} #: Hash[untyped, untyped]
    elsif value.respond_to?(:fetch) then value
    else argument_error! "Unsupported follow options: #{value}"
    end
end

def initialize(

Returns:
  • (HTTP::Options) -

Other tags:
    Api: - public
def initialize(
  response: :auto,
  encoding: nil,
  nodelay: false,
  keep_alive_timeout: 5,
  proxy: {},
  ssl: {},
  headers: {},
  features: {},
  timeout_class: self.class.default_timeout_class,
  timeout_options: {},
  socket_class: self.class.default_socket_class,
  ssl_socket_class: self.class.default_ssl_socket_class,
  params: nil,
  form: nil,
  json: nil,
  body: nil,
  follow: nil,
  retriable: nil,
  base_uri: nil,
  persistent: nil,
  ssl_context: nil
)
  assign_options(binding)
end

def merge(other)

Returns:
  • (HTTP::Options) -

Other tags:
    Api: - public

Parameters:
  • other (HTTP::Options, Hash) --
def merge(other)
  merged = to_hash.merge(other.to_hash) do |k, v1, v2|
    k == :headers ? v1.merge(v2) : v2
  end
  self.class.new(**merged)
end

def new(options = nil, **kwargs)

Returns:
  • (HTTP::Options) -

Other tags:
    Api: - public

Parameters:
  • options (HTTP::Options, Hash, nil) -- existing Options or Hash to convert
def new(options = nil, **kwargs)
  return options if options.is_a?(self)
  super(**(options || kwargs)) # steep:ignore
end

def parse_base_uri(value)

Returns:
  • (HTTP::URI) -

Other tags:
    Api: - private

Parameters:
  • value (String, HTTP::URI) -- the base URI to parse
def parse_base_uri(value)
  uri = URI.parse(value)
  base = @base_uri
  return resolve_base_uri(base, uri) if base
  argument_error!(format("Invalid base URI: %s", value)) unless uri.scheme
  uri
end

def persistent=(value)

Returns:
  • (String, nil) -

Other tags:
    Api: - private

Parameters:
  • value (String, nil) --
def persistent=(value)
  @persistent = value ? URI.parse(value).origin : nil
  validate_base_uri_and_persistent!
end

def persistent?

Returns:
  • (Boolean) -

Other tags:
    Api: - public
def persistent?
  !persistent.nil?
end

def register_feature(name, impl)

Returns:
  • (Class) -

Other tags:
    Api: - public

Parameters:
  • impl (Class) --
  • name (Symbol) --
def register_feature(name, impl)
  @available_features[name] = impl
end

def resolve_base_uri(base, relative)

Returns:
  • (HTTP::URI) -

Other tags:
    Api: - private

Parameters:
  • relative (HTTP::URI) -- the URI to join
  • base (HTTP::URI) -- the existing base URI
def resolve_base_uri(base, relative)
  unless base.path.end_with?("/")
    base = base.dup
    base.path = "#{base.path}/"
  end
  URI.parse(base.join(relative))
end

def retriable=(value)

Returns:
  • (Hash, nil) -

Other tags:
    Api: - private

Parameters:
  • value (Boolean, Hash, nil) --
def retriable=(value)
  @retriable =
    if    !value                    then nil
    elsif true == value             then {} #: Hash[untyped, untyped]
    elsif value.respond_to?(:fetch) then value
    else argument_error! "Unsupported retriable options: #{value}"
    end
end

def to_hash

Returns:
  • (Hash) -

Other tags:
    Api: - public
def to_hash
  self.class.defined_options.to_h { |opt_name| [opt_name, public_send(opt_name)] }
end

def validate_base_uri_and_persistent!

Returns:
  • (void) -

Other tags:
    Api: - private
def validate_base_uri_and_persistent!
  base = @base_uri
  persistent = @persistent
  return unless base && persistent
  return if base.origin.eql?(persistent)
  argument_error!(
    format("Persistence origin (%s) conflicts with base URI origin (%s)",
           persistent, base.origin)
  )
end