refactor how adapters save response params to env

Ensures :response_headers always get initialized

Closes #50
This commit is contained in:
Mislav Marohnić 2011-04-04 02:02:00 +02:00
parent 74c8201857
commit b85e0a18df
8 changed files with 23 additions and 18 deletions

View File

@ -31,8 +31,13 @@ module Faraday
end
end
def response_headers(env)
env[:response_headers] ||= Utils::Headers.new
def save_response(env, status, body, headers = nil)
env[:status] = status
env[:body] = body
env[:response_headers] = Utils::Headers.new.tap do |response_headers|
response_headers.update headers unless headers.nil?
yield response_headers if block_given?
end
end
end
end

View File

@ -21,8 +21,7 @@ module Faraday
super
@session.__send__(env[:method], env[:url].request_uri, env[:body], env[:request_headers])
resp = @session.response
env.update :status => resp.status, :body => resp.body
response_headers(env).update resp.headers
save_response(env, resp.status, resp.body, resp.headers)
@app.call env
end
end

View File

@ -51,10 +51,11 @@ module Faraday
client = block.call
end
client.response_header.each do |name, value|
response_headers(env)[name.to_sym] = value
save_response(env, client.response_header.status, client.response) do |response_headers|
client.response_header.each do |name, value|
response_headers[name.to_sym] = value
end
end
env.update :status => client.response_header.status, :body => client.response
@app.call env
rescue Errno::ECONNREFUSED

View File

@ -17,8 +17,7 @@ module Faraday
:headers => env[:request_headers],
:body => env[:body]
env.update :status => resp.status.to_i, :body => resp.body
response_headers(env).update resp.headers
save_response(env, resp.status.to_i, resp.body, resp.headers)
@app.call env
rescue ::Excon::Errors::SocketError

View File

@ -53,10 +53,11 @@ module Faraday
raise Error::ConnectionFailed, $!
end
http_response.each_header do |key, value|
response_headers(env)[key] = value
save_response(env, http_response.code.to_i, http_response.body) do |response_headers|
http_response.each_header do |key, value|
response_headers[key] = value
end
end
env.update :status => http_response.code.to_i, :body => http_response.body
@app.call env
end

View File

@ -21,8 +21,7 @@ module Faraday
raise Error::ConnectionFailed, $!
end
env.update :status => response.status, :body => response.body
response_headers(env).update response.headers
save_response(env, response.status, response.body, response.headers)
@app.call env
end

View File

@ -110,8 +110,7 @@ module Faraday
if stub = stubs.match(env[:method], normalized_path, env[:body])
status, headers, body = stub.block.call(env)
env.update :status => status, :body => body
response_headers(env).update headers
save_response(env, status, body, headers)
else
raise "no stubbed request for #{env[:method]} #{normalized_path} #{env[:body]}"
end

View File

@ -27,8 +27,10 @@ module Faraday
is_parallel = !!env[:parallel_manager]
req.on_complete do |resp|
env.update :status => resp.code, :body => resp.body
response_headers(env).parse resp.headers
save_response(env, resp.code, resp.body) do |response_headers|
response_headers.parse resp.headers
end
# in async mode, :response is initialized at this point
env[:response].finish(env) if is_parallel
end