Class: Faraday::Response

Inherits:
Object show all
Extended by:
AutoloadHelper, MiddlewareRegistry, Forwardable
Defined in:
lib/faraday/response.rb,
lib/faraday/autoload.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.



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

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

#bodyObject



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

def body
  finished? ? env.body : nil
end

#finish(env) ⇒ Object



62
63
64
65
66
67
# File 'lib/faraday/response.rb', line 62

def finish(env)
  raise "response already finished" if finished?
  @on_complete_callbacks.each { |callback| callback.call(env) }
  @env = Env.from(env)
  return self
end

#finished?Boolean

Returns:

  • (Boolean)


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

def finished?
  !!env
end

#headersObject



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

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

#marshal_dumpObject

because @on_complete_callbacks cannot be marshalled



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

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

#marshal_load(env) ⇒ Object



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

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

#on_completeObject



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

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

#statusObject



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

def status
  finished? ? env.status : nil
end

#success?Boolean

Returns:

  • (Boolean)


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

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