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 AutoloadHelper

all_loaded_constants, autoload_all, load_autoloaded_constants

Methods included from MiddlewareRegistry

fetch_middleware, load_middleware, lookup_middleware, middleware_mutex, register_middleware, unregister_middleware

Constructor Details

#initialize(env = nil) ⇒ Response

Returns a new instance of Response.



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

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.



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

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.



97
98
99
100
101
102
# File 'lib/faraday/response.rb', line 97

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

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

#bodyObject



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

def body
  finished? ? env.body : nil
end

#finish(env) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/faraday/response.rb', line 67

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)


54
55
56
# File 'lib/faraday/response.rb', line 54

def finished?
  !!env
end

#headersObject



45
46
47
# File 'lib/faraday/response.rb', line 45

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

#marshal_dumpObject

because @on_complete_callbacks cannot be marshalled



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

def marshal_dump
  finished? ? to_hash : nil
end

#marshal_load(env) ⇒ Object



91
92
93
# File 'lib/faraday/response.rb', line 91

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

#on_complete(&block) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/faraday/response.rb', line 58

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

#reason_phraseObject



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

def reason_phrase
  finished? ? env.reason_phrase : nil
end

#statusObject



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

def status
  finished? ? env.status : nil
end

#success?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/faraday/response.rb', line 75

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

#to_hashObject



79
80
81
82
83
84
# File 'lib/faraday/response.rb', line 79

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