use a cleaner block syntax for building the Faraday::Response

This commit is contained in:
rick 2009-12-10 06:59:47 -08:00
parent c5e155f178
commit 46dfd02f26
3 changed files with 16 additions and 10 deletions

View File

@ -10,6 +10,14 @@ module Faraday
autoload :Connection, 'faraday/connection'
class Response < Struct.new(:headers, :body)
def initialize(headers = nil, body = nil)
super(headers || {}, body)
if block_given?
yield self
processed!
end
end
def process(chunk)
if !body
self.body = []
@ -18,7 +26,7 @@ module Faraday
end
def processed!
self.body = body.join
self.body = body.join if body.respond_to?(:join)
end
end

View File

@ -4,15 +4,14 @@ module Faraday
module NetHttp
def _get(uri, request_headers)
http = Net::HTTP.new(uri.host, uri.port)
resp = Response.new({})
http_resp = http.get(uri.path, request_headers) do |chunk|
resp.process(chunk)
Response.new do |resp|
http_resp = http.get(uri.path, request_headers) do |chunk|
resp.process(chunk)
end
http_resp.each_header do |key, value|
resp.headers[key] = value
end
end
resp.processed!
http_resp.each_header do |key, value|
resp.headers[key] = value
end
resp
end
end
end

View File

@ -15,7 +15,6 @@ module Faraday
end
end
# Override in a subclass, or include an adapter
#
# def _get(uri, headers)