Custom Options Classes ↑

Faraday’s own options classes (ConnectionOptions, RequestOptions, SSLOptions, ProxyOptions and Env) inherit from Faraday::BaseOptions. The legacy Faraday::Options, a Struct, still exists for code that subclasses it, but new options classes should not use it. Two building blocks are available for code that defines its own options classes, such as adapters and middleware:

Defining a class ↑

A subclass lists its attributes in MEMBERS and defines a public reader and writer for each of them. COERCIONS is optional and maps an attribute to the coercion applied to every non-nil value assigned to it:

  • a Class, coerced with Class.from(value) (for example another BaseOptions subclass);

  • a Proc, coerced with proc.call(value).

class CacheOptions < Faraday::BaseOptions
  MEMBERS = %i[ttl store].freeze

  attr_accessor :ttl, :store
end

class MyOptions < Faraday::BaseOptions
  MEMBERS = %i[timeout cache headers].freeze
  COERCIONS = { cache: CacheOptions }.freeze

  attr_accessor :timeout, :cache, :headers
end

options = MyOptions.new(timeout: 5, cache: { ttl: 60 })
options.cache.class # => CacheOptions

Behavior ↑

  • MyOptions.from(value) returns a deep copy of value if it is already a MyOptions (the caller’s object is never shared), a new empty instance for nil, and a new instance built from a Hash, another OptionsLike or anything that responds to to_hash. Other values raise ArgumentError.

  • new accepts a single optional hash, so MyOptions.new(timeout: 5) works too.

  • Attributes are only read and written through their public readers and writers, so a subclass can override them.

  • Keys can be symbols or strings, and are normalized to symbols. A key that is not in MEMBERS raises NoMethodError, like it does for Faraday::Options.

  • nil values are skipped by new, update and merge!, so they never overwrite an existing value. false does.

  • merge! deep merges a hash into the existing hash or options value of the same attribute, recursively. Other values are replaced. Nested options objects are replaced by a merged copy rather than mutated.

  • merge and deep_dup copy hashes, arrays and nested options recursively, so the result shares none of them with the original. Any other object (strings, procs, IO objects and so on) is kept as is.

  • to_hash skips nil attributes and converts nested options to hashes. Because deep_dup and merge go through to_hash, an attribute holding an options object needs a matching entry in COERCIONS to stay an options object.

options.merge(cache: { store: :memory }).to_hash
# => { timeout: 5, cache: { ttl: 60, store: :memory } }
options.merge!(timeout: nil, 'cache' => { 'ttl' => 30 })
options.to_hash # => { timeout: 5, cache: { ttl: 30 } }

Faraday::Utils.deep_merge!(target, hash) accepts a BaseOptions as target and calls target.merge!(hash).

Struct-style access ↑

BaseOptions has no Struct API: no [], []=, each, keys, dig or value-based ==. Use the readers and writers instead. RequestOptions, SSLOptions and ProxyOptions still answer [], []=, fetch and key? through Faraday::StructCompat, because adapters and other gems call them (for example env[:ssl].fetch(:verify, true) or conn.options[:timeout] = 5). StructCompat is deprecated and will be removed in Faraday 3.0, so new classes must not include it.

For the same reason, every BaseOptions class keeps to_h, empty? and the class method members. They are deprecated too: use to_hash, to_hash.empty? and MEMBERS in new code.

Faraday::Env keeps its own hash-like API, because middleware relies on it. See The Env Object.