Class: Faraday::Response
- Inherits:
-
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
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
#env ⇒ Object
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
|
#body ⇒ Object
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
36
37
38
|
# File 'lib/faraday/response.rb', line 36
def finished?
!!env
end
|
26
27
28
|
# File 'lib/faraday/response.rb', line 26
def
finished? ? env. : {}
end
|
#marshal_dump ⇒ Object
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_phrase ⇒ Object
22
23
24
|
# File 'lib/faraday/response.rb', line 22
def reason_phrase
finished? ? env.reason_phrase : nil
end
|
#status ⇒ Object
18
19
20
|
# File 'lib/faraday/response.rb', line 18
def status
finished? ? env.status : nil
end
|
#success? ⇒ Boolean
57
58
59
|
# File 'lib/faraday/response.rb', line 57
def success?
finished? && env.success?
end
|
#to_hash ⇒ Object
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.,
url: env.url
}
end
|