Class: Faraday::Response

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

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

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.



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

def apply_request(request_env)
  raise "response didn't finish yet" unless finished?
  @env = Env.from(request_env).update(@env)
  return self
end

#bodyObject



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

def body
  finished? ? env.body : nil
end

#finish(env) ⇒ Object



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

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) }
  return self
end

#finished?Boolean

Returns:

  • (Boolean)


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

def finished?
  !!env
end

#headersObject



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

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

#marshal_dumpObject

because @on_complete_callbacks cannot be marshalled



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

def marshal_dump
  !finished? ? nil : {
    :status => @env.status, :body => @env.body,
    :response_headers => @env.response_headers
  }
end

#marshal_load(env) ⇒ Object



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

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

#on_completeObject



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

def on_complete
  if not finished?
    @on_complete_callbacks << Proc.new
  else
    yield(env)
  end
  return self
end

#reason_phraseObject



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

def reason_phrase
  finished? ? env.reason_phrase : nil
end

#statusObject



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

def status
  finished? ? env.status : nil
end

#success?Boolean

Returns:

  • (Boolean)


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

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