diff --git a/lib/faraday.rb b/lib/faraday.rb index ab457795..c8120ddd 100644 --- a/lib/faraday.rb +++ b/lib/faraday.rb @@ -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 diff --git a/lib/faraday/adapter/net_http.rb b/lib/faraday/adapter/net_http.rb index 75c25ade..36d7266f 100644 --- a/lib/faraday/adapter/net_http.rb +++ b/lib/faraday/adapter/net_http.rb @@ -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 diff --git a/lib/faraday/connection.rb b/lib/faraday/connection.rb index 2544409b..3b362dd4 100644 --- a/lib/faraday/connection.rb +++ b/lib/faraday/connection.rb @@ -15,7 +15,6 @@ module Faraday end end - # Override in a subclass, or include an adapter # # def _get(uri, headers)