add support for configurable Faraday::Connection#response_class

This commit is contained in:
rick 2009-12-10 07:14:06 -08:00
parent 46dfd02f26
commit d46bbceb07
4 changed files with 39 additions and 24 deletions

View File

@ -8,27 +8,7 @@ module Faraday
end
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 = []
end
body << chunk
end
def processed!
self.body = body.join if body.respond_to?(:join)
end
end
autoload :Response, 'faraday/response'
module Adapter
autoload :NetHttp, 'faraday/adapter/net_http'

View File

@ -4,7 +4,7 @@ module Faraday
module NetHttp
def _get(uri, request_headers)
http = Net::HTTP.new(uri.host, uri.port)
Response.new do |resp|
response_class.new do |resp|
http_resp = http.get(uri.path, request_headers) do |chunk|
resp.process(chunk)
end

View File

@ -4,9 +4,11 @@ module Faraday
include Addressable
attr_accessor :host, :port
attr_reader :path_prefix
attr_reader :path_prefix
attr_writer :response_class
def initialize(url = nil)
@response_class = nil
if url
uri = URI.parse(url)
self.host = uri.host
@ -21,7 +23,11 @@ module Faraday
# end
#
def get(url, params = {}, headers = {})
_get(build_uri(url, params), headers)
_get(build_uri(url, params), headers).content
end
def response_class
@response_class || Response
end
def path_prefix=(value)

29
lib/faraday/response.rb Normal file
View File

@ -0,0 +1,29 @@
module Faraday
class Response < Struct.new(:headers, :body)
autoload :StringResponse, 'faraday/response/string_response'
def initialize(headers = nil, body = nil)
super(headers || {}, body)
if block_given?
yield self
processed!
end
end
def process(chunk)
if !body
self.body = []
end
body << chunk
end
def processed!
self.body = body.join if body.respond_to?(:join)
end
# determines what Faraday::Connection#get returns
def content
self
end
end
end