class Faraday::Env

Env holds the state of one request and its response while it goes through the middleware stack.

Middleware reads and writes it with the accessors or with {#[]} and {#[]=}. Keys that are not members are kept as custom members, see {#custom_members}.

The body key means the request body until the response has a status, and the response body after that, see {#body}. {#[]}, {#[]=} and {#delete} accept it as the Symbol :body only, like the legacy Struct: the String +‘body’+ is a custom member there. {.new}, {.from}, {#update}, {#merge} and {#merge!} accept both.

{.new}, {.from}, {#update}, {#merge} and {#merge!} coerce request to {RequestOptions}, ssl to {SSLOptions}, and request_headers and response_headers to {Utils::Headers}. false means “not set” for these members: they skip it and keep the current value. They store a copy of other Hash values, and {#merge} and {#merge!} replace them instead of merging them. The writers and {#[]=} keep the assigned value as it is.

Constants

COERCIONS
ContentLength

The names are public API, so they keep the CamelCase of the legacy Struct.

MEMBERS
MethodsWithBodies

A Set of HTTP verbs that typically send a body. If no body is set for these requests, the Content-Length header is set to 0.

StatusesWithoutBody
SuccessfulStatuses

Attributes

@return [Symbol] HTTP method (:get, :post)

@return [Object] set if the connection is in parallel mode

@return [Hash] the request params

@return [String] the reason phrase of the response status

@return [RequestOptions] options for configuring the request

@return [String, Hash] the request body, which middleware eventually

converts to a String

@return [Utils::Headers] HTTP headers to be sent to the server

@return [Response] the response, once the adapter has made one

@return [Object] the response body

@return [Utils::Headers] HTTP headers from the server

@return [SSLOptions] options for configuring SSL requests

@return [Integer] HTTP response status code

@return [URI] URI instance for the current request

Public Class Methods

Builds the Env of a request. The request options, the request headers and the SSL options of the connection are used as they are, not copied.

@param request [Request] @param url [URI] the full URL of the request @param connection [Connection] @return [Env] @api private

# File lib/faraday/options/env.rb, line 113
def self.for_request(request, url, connection)
  env = new
  env.method = request.http_method
  env.request_body = request.body
  env.url = url
  env.request = request.options
  env.request_headers = request.headers
  env.ssl = connection.ssl
  env.parallel_manager = connection.parallel_manager
  env
end

Builds a new Env from a Hash or from another Env.

A copy of an Env gets new {RequestOptions}, {SSLOptions}, headers and Hash values, and the same custom members. Other values, such as the bodies and the URL, are shared with the original.

@param value [nil, Hash, Env] the values of the new Env @return [Env]

Calls superclass method Faraday::BaseOptions::from
# File lib/faraday/options/env.rb, line 97
def self.from(value)
  return super unless value.is_a?(Env)

  env = new(value.to_hash)
  env.custom_members.update(value.custom_members)
  env
end

Public Instance Methods

@param key [Object] a member, :body or a custom member @return [Object] the value, or nil if a custom member is not set

# File lib/faraday/options/env.rb, line 127
def [](key)
  member = member_for(key)
  member ? public_send(member) : custom_members[key]
end

@param key [Object] a member, :body or a custom member @param value [Object] the value, which is not coerced

# File lib/faraday/options/env.rb, line 134
def []=(key, value)
  member = member_for(key)
  if member
    public_send(:"#{member}=", value)
  else
    custom_members[key] = value
  end
end

@return [Object] the request body, or the response body once the

response has a status
# File lib/faraday/options/env.rb, line 173
def body
  self[:body]
end

Sets the request body, or the response body once the response has a status.

# File lib/faraday/options/env.rb, line 179
def body=(value)
  self[:body] = value
end

Sets content length to zero and the body to the empty string.

# File lib/faraday/options/env.rb, line 195
def clear_body
  request_headers[ContentLength] = '0'
  self.body = +''
end

@return [Symbol] :response_body if the response has a status,

+:request_body+ otherwise
# File lib/faraday/options/env.rb, line 167
def current_body
  status ? :response_body : :request_body
end

The values of the keys that are not members, set with {#[]=}.

@return [Hash]

# File lib/faraday/options/env.rb, line 238
def custom_members
  @custom_members ||= {}
end

@return [Env] a deep copy, with a deep copy of the custom members

Calls superclass method Faraday::BaseOptions#deep_dup
# File lib/faraday/options/env.rb, line 266
def deep_dup
  copy = super
  copy.custom_members.update(Utils.deep_dup(custom_members))
  copy
end

Unsets a member or removes a custom member.

@param key [Object] a member, :body or a custom member @return [Object] the value before the deletion

# File lib/faraday/options/env.rb, line 147
def delete(key)
  member = member_for(key)
  return custom_members.delete(key) unless member

  value = public_send(member)
  public_send(:"#{member}=", nil)
  value
end

Reads key, then calls dig on the value with the other keys.

@example

env.dig(:response_headers, 'Content-Type')
# File lib/faraday/options/env.rb, line 160
def dig(key, *keys)
  value = self[key]
  keys.empty? || value.nil? ? value : value.dig(*keys)
end

@return [String] the set members and the custom members, like the

legacy Struct
# File lib/faraday/options/env.rb, line 274
def inspect
  attrs = [nil]
  MEMBERS.each do |member|
    if (value = public_send(member))
      attrs << "@#{member}=#{value.inspect}"
    end
  end
  attrs << "@custom=#{custom_members.inspect}" unless custom_members.empty?
  %(#<#{self.class}#{attrs.join(' ')}>)
end

Like {#merge}, but changes the receiver. Only the coerced members are deep-merged, other values are replaced, like the legacy Struct did.

@param obj [Hash, to_hash] the values to merge @return [Env] self

# File lib/faraday/options/env.rb, line 258
def merge!(obj)
  each_option(obj) do |key, value|
    write(key, COERCIONS.key?(key) ? merge_value(public_send(key), value) : value)
  end
  self
end

@return [Boolean] true if there’s no body yet, and the method is in the

set of {MethodsWithBodies}.
# File lib/faraday/options/env.rb, line 190
def needs_body?
  !body && MethodsWithBodies.include?(method)
end

@return [Boolean] true if there is a parallel_manager

# File lib/faraday/options/env.rb, line 207
def parallel?
  !!parallel_manager
end

@return [Boolean] true if the status isn’t in the set of

{StatusesWithoutBody}.
# File lib/faraday/options/env.rb, line 202
def parse_body?
  !StatusesWithoutBody.include?(status)
end

Calls the on_data callback of the request options with each chunk that the block yields, and once with an empty chunk if there is none.

@yield [chunk_handler] the block that reads the response @return [Object] the value of the block

# File lib/faraday/options/env.rb, line 221
def stream_response(&block)
  size = 0
  yielded = false
  block_result = block.call do |chunk|
    if chunk.bytesize.positive? || size.positive?
      yielded = true
      size += chunk.bytesize
      request.on_data.call(chunk, size, self)
    end
  end
  request.on_data.call(+'', 0, self) unless yielded
  block_result
end

@return [Boolean] true if the request options have an on_data callback

# File lib/faraday/options/env.rb, line 212
def stream_response?
  request.stream_response?
end

@return [Boolean] true if status is in the set of {SuccessfulStatuses}.

# File lib/faraday/options/env.rb, line 184
def success?
  SuccessfulStatuses.include?(status)
end

Members only, nested options objects stay objects, like the legacy Struct. Custom members are not included.

@return [Hash]

# File lib/faraday/options/env.rb, line 246
def to_hash
  MEMBERS.each_with_object({}) do |key, hash|
    value = public_send(key)
    hash[key] = value unless value.nil?
  end
end