class Faraday::Response

Response represents an HTTP response from making an HTTP request.

Attributes

Public Class Methods

# File lib/faraday/response.rb, line 11
def initialize(env = nil)
  @env = Env.from(env) if env
  @on_complete_callbacks = []
end

Public Instance Methods

Expand the env with more properties, without overriding existing ones. Useful for applying request params after restoring a marshalled Response.

# File lib/faraday/response.rb, line 80
def apply_request(request_env)
  raise "response didn't finish yet" unless finished?

  @env = Env.from(request_env).update(@env)
  self
end
# File lib/faraday/response.rb, line 32
def body
  finished? ? env.body : nil
end
# File lib/faraday/response.rb, line 49
def finish(env)
  raise 'response already finished' if finished?

  @env = env.is_a?(Env) ? env : Env.from(env)
  @on_complete_callbacks.each { |callback| callback.call(@env) }
  self
end
# File lib/faraday/response.rb, line 36
def finished?
  !!env
end
# File lib/faraday/response.rb, line 26
def headers
  finished? ? env.response_headers : {}
end

because @on_complete_callbacks cannot be marshalled

# File lib/faraday/response.rb, line 70
def marshal_dump
  finished? ? to_hash : nil
end
# File lib/faraday/response.rb, line 74
def marshal_load(env)
  @env = Env.from(env)
end
# File lib/faraday/response.rb, line 40
def on_complete(&block)
  if finished?
    yield(env)
  else
    @on_complete_callbacks << block
  end
  self
end
# File lib/faraday/response.rb, line 22
def reason_phrase
  finished? ? env.reason_phrase : nil
end
# File lib/faraday/response.rb, line 18
def status
  finished? ? env.status : nil
end
# File lib/faraday/response.rb, line 57
def success?
  finished? && env.success?
end
# File lib/faraday/response.rb, line 61
def to_hash
  {
    status: env.status, body: env.body,
    response_headers: env.response_headers,
    url: env.url
  }
end