Class: Faraday::Response

Inherits:
Object
  • Object
show all
Extended by:
AutoloadHelper, MiddlewareRegistry, Forwardable
Defined in:
lib/faraday/autoload.rb,
lib/faraday/response.rb,
lib/faraday/response/logger.rb,
lib/faraday/response/raise_error.rb

Overview

Response represents an HTTP response from making an HTTP request.

Defined Under Namespace

Classes: Logger, Middleware, RaiseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MiddlewareRegistry

fetch_middleware, load_middleware, lookup_middleware, middleware_mutex, register_middleware, unregister_middleware

Methods included from AutoloadHelper

all_loaded_constants, autoload_all, load_autoloaded_constants

Constructor Details

#initialize(env = nil) ⇒ Response

Returns a new instance of Response.


27
28
29
30
# File 'lib/faraday/response.rb', line 27

def initialize(env = nil)
  @env = Env.from(env) if env
  @on_complete_callbacks = []
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env


32
33
34
# File 'lib/faraday/response.rb', line 32

def env
  @env
end

Instance Method Details

#apply_request(request_env) ⇒ Object

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


94
95
96
97
98
99
# File 'lib/faraday/response.rb', line 94

def apply_request(request_env)
  raise "response didn't finish yet" unless finished?

  @env = Env.from(request_env).update(@env)
  self
end

#bodyObject


47
48
49
# File 'lib/faraday/response.rb', line 47

def body
  finished? ? env.body : nil
end

#finish(env) ⇒ Object


64
65
66
67
68
69
70
# File 'lib/faraday/response.rb', line 64

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

#finished?Boolean

Returns:

  • (Boolean)

51
52
53
# File 'lib/faraday/response.rb', line 51

def finished?
  !!env
end

#headersObject


42
43
44
# File 'lib/faraday/response.rb', line 42

def headers
  finished? ? env.response_headers : {}
end

#marshal_dumpObject

because @on_complete_callbacks cannot be marshalled


84
85
86
# File 'lib/faraday/response.rb', line 84

def marshal_dump
  finished? ? to_hash : nil
end

#marshal_load(env) ⇒ Object


88
89
90
# File 'lib/faraday/response.rb', line 88

def marshal_load(env)
  @env = Env.from(env)
end

#on_complete(&block) ⇒ Object


55
56
57
58
59
60
61
62
# File 'lib/faraday/response.rb', line 55

def on_complete(&block)
  if !finished?
    @on_complete_callbacks << block
  else
    yield(env)
  end
  self
end

#reason_phraseObject


38
39
40
# File 'lib/faraday/response.rb', line 38

def reason_phrase
  finished? ? env.reason_phrase : nil
end

#statusObject


34
35
36
# File 'lib/faraday/response.rb', line 34

def status
  finished? ? env.status : nil
end

#success?Boolean

Returns:

  • (Boolean)

72
73
74
# File 'lib/faraday/response.rb', line 72

def success?
  finished? && env.success?
end

#to_hashObject


76
77
78
79
80
81
# File 'lib/faraday/response.rb', line 76

def to_hash
  {
    status: env.status, body: env.body,
    response_headers: env.response_headers
  }
end