Class: Faraday::Response

Inherits:
Object show all
Extended by:
AutoloadHelper, MiddlewareRegistry, Forwardable
Defined in:
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

lookup_middleware, register_middleware

Constructor Details

#initialize(env = nil) ⇒ Response

Returns a new instance of Response.



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

def initialize(env = nil)
  @env = env
  @on_complete_callbacks = []
end

Instance Attribute Details

#envObject (readonly) Also known as: to_hash

Returns the value of attribute env.



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

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.



93
94
95
96
97
# File 'lib/faraday/response.rb', line 93

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

#bodyObject



51
52
53
# File 'lib/faraday/response.rb', line 51

def body
  finished? ? env[:body] : nil
end

#finish(env) ⇒ Object



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

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

#finished?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/faraday/response.rb', line 55

def finished?
  !!env
end

#headersObject



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

def headers
  finished? ? env[:response_headers] : {}
end

#marshal_dumpObject

because @on_complete_callbacks cannot be marshalled



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

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

#marshal_load(env) ⇒ Object



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

def marshal_load(env)
  @env = env
end

#on_completeObject



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

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

#statusObject



42
43
44
# File 'lib/faraday/response.rb', line 42

def status
  finished? ? env[:status] : nil
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  (200..299).include?(status)
end