Class: Faraday::Response

Inherits:
Object
  • Object
show all
Extended by:
MiddlewareRegistry, Forwardable
Defined in:
lib/faraday/response.rb,
lib/faraday/response/json.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: Json, Logger, RaiseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MiddlewareRegistry

lookup_middleware, register_middleware, registered_middleware, unregister_middleware

Constructor Details

#initialize(env = nil) ⇒ Response

Returns a new instance of Response.



11
12
13
14
# File 'lib/faraday/response.rb', line 11

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.



16
17
18
# File 'lib/faraday/response.rb', line 16

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.



80
81
82
83
84
85
# 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

#bodyObject



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

def body
  finished? ? env.body : nil
end

#finish(env) ⇒ Object



49
50
51
52
53
54
55
# 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

#finished?Boolean

Returns:

  • (Boolean)


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

def finished?
  !!env
end

#headersObject



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

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

#marshal_dumpObject

because @on_complete_callbacks cannot be marshalled



70
71
72
# File 'lib/faraday/response.rb', line 70

def marshal_dump
  finished? ? to_hash : nil
end

#marshal_load(env) ⇒ Object



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

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

#on_complete(&block) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/faraday/response.rb', line 40

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

#reason_phraseObject



22
23
24
# File 'lib/faraday/response.rb', line 22

def reason_phrase
  finished? ? env.reason_phrase : nil
end

#statusObject



18
19
20
# File 'lib/faraday/response.rb', line 18

def status
  finished? ? env.status : nil
end

#success?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/faraday/response.rb', line 57

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

#to_hashObject



61
62
63
64
65
66
67
# 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