mirror of
https://github.com/lostisland/faraday.git
synced 2025-09-29 00:01:53 -04:00
add support for configurable Faraday::Connection#response_class
This commit is contained in:
parent
46dfd02f26
commit
d46bbceb07
@ -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'
|
||||
|
@ -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
|
||||
|
@ -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
29
lib/faraday/response.rb
Normal 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
|
Loading…
x
Reference in New Issue
Block a user