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 MiddlewareRegistry

lookup_middleware, register_middleware

Methods included from AutoloadHelper

all_loaded_constants, autoload_all, load_autoloaded_constants

Constructor Details

- (Response) initialize(env = nil)



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

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

Instance Attribute Details

- (Object) env (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

- (Object) apply_request(request_env)

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

- (Object) body



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

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

- (Object) finish(env)



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

- (Boolean) finished?



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

def finished?
  !!env
end

- (Object) headers



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

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

- (Object) marshal_dump

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

- (Object) marshal_load(env)



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

def marshal_load(env)
  @env = env
end

- (Object) on_complete



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

- (Object) status



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

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

- (Boolean) success?



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

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