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.



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

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



53
54
55
56
57
58
59
# File 'lib/faraday/response.rb', line 53

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)


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

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



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

def marshal_dump
  finished? ? to_hash : nil
end

#marshal_load(env) ⇒ Object



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

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

#on_complete(&block) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/faraday/response.rb', line 44

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)


61
62
63
# File 'lib/faraday/response.rb', line 61

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

#to_hashObject



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

def to_hash
  {
    status: status, body: body,
    response_headers: headers,
    url: url
  }
end

#urlObject



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

def url
  finished? ? env.url : nil
end