Class: Faraday::Response

Inherits:
Object show all
Extended by:
AutoloadHelper, 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, lookup_module, register_lookup_modules

Constructor Details

#initialize(env = nil) ⇒ Response

Returns a new instance of Response.



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

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.



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

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.



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

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

#bodyObject



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

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

#finish(env) ⇒ Object



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

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)


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

def finished?
  !!env
end

#headersObject



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

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

#marshal_dumpObject

because @on_complete_callbacks cannot be marshalled



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

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

#marshal_load(env) ⇒ Object



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

def marshal_load(env)
  @env = env
end

#on_completeObject



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

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

#statusObject



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

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

#success?Boolean

Returns:

  • (Boolean)


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

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