module Faraday::StructCompat
The part of the Struct-style API of the legacy {Options} that adapters and middleware still use on the {BaseOptions} subclasses converted from it ({ProxyOptions}, {RequestOptions}, {SSLOptions}), e.g. env[:request][:proxy][:uri], +env.fetch(:verify, true)+, +ssl.key?(:verify_hostname)+ or +conn.options = 5+.
Official adapters support every Faraday 2.x, so it is kept for the whole 2.x series. Reads and writes go through the public accessors of the members.
@deprecated Slated for removal in Faraday 3.0, use the accessors of the
options instead. There is no runtime warning yet.
It must not be used by new options classes.
@api private
Public Instance Methods
@param key [Symbol, String] a member @return [Object] the value of the member @raise [NameError] if key is not a member
# File lib/faraday/struct_compat.rb, line 23 def [](key) public_send(member_for(key)) end
Sets a member, like +conn.options = 5+ or +conn.ssl = false+. The value is coerced like {BaseOptions#update} does (e.g. a proxy Hash or String becomes {ProxyOptions}), but nil is assigned instead of skipped, like the legacy Struct.
@param key [Symbol, String] a member @param value [Object] the new value @raise [NameError] if key is not a member
# File lib/faraday/struct_compat.rb, line 35 def []=(key, value) member = member_for(key) public_send(:"#{member}=", value.nil? ? nil : coerce(member, value)) end
Returns the value of a member. A member that is not set (nil) is set to the default, or to the block value, and that value is returned.
@param key [Symbol, String] a member @param args [Array] the optional default value @return [Object] the value of the member @raise [KeyError] if the member is not set and there is no default
# File lib/faraday/struct_compat.rb, line 47 def fetch(key, *args) unless self.class::MEMBERS.include?(key.to_sym) && !public_send(key).nil? key_setter = :"#{key}=" if !args.empty? public_send(key_setter, args.first) elsif block_given? public_send(key_setter, yield(key)) else raise KeyError, "key not found: #{key.inspect}" end end public_send(key) end
@param key [Symbol] any key, e.g. +ssl.key?(:client_cert_passwd)+ @return [Boolean] true if key is a member and it is set
# File lib/faraday/struct_compat.rb, line 63 def key?(key) self.class::MEMBERS.include?(key) && !public_send(key).nil? end