mirror of
https://github.com/lostisland/faraday.git
synced 2025-09-29 00:01:53 -04:00
use a cleaner block syntax for building the Faraday::Response
This commit is contained in:
parent
c5e155f178
commit
46dfd02f26
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -15,7 +15,6 @@ module Faraday
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# Override in a subclass, or include an adapter
|
||||
#
|
||||
# def _get(uri, headers)
|
||||
|
Loading…
x
Reference in New Issue
Block a user