mirror of
https://github.com/lostisland/faraday.git
synced 2025-12-08 00:02:54 -05: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'
|
autoload :Connection, 'faraday/connection'
|
||||||
|
|
||||||
class Response < Struct.new(:headers, :body)
|
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)
|
def process(chunk)
|
||||||
if !body
|
if !body
|
||||||
self.body = []
|
self.body = []
|
||||||
@ -18,7 +26,7 @@ module Faraday
|
|||||||
end
|
end
|
||||||
|
|
||||||
def processed!
|
def processed!
|
||||||
self.body = body.join
|
self.body = body.join if body.respond_to?(:join)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -4,15 +4,14 @@ module Faraday
|
|||||||
module NetHttp
|
module NetHttp
|
||||||
def _get(uri, request_headers)
|
def _get(uri, request_headers)
|
||||||
http = Net::HTTP.new(uri.host, uri.port)
|
http = Net::HTTP.new(uri.host, uri.port)
|
||||||
resp = Response.new({})
|
Response.new do |resp|
|
||||||
http_resp = http.get(uri.path, request_headers) do |chunk|
|
http_resp = http.get(uri.path, request_headers) do |chunk|
|
||||||
resp.process(chunk)
|
resp.process(chunk)
|
||||||
|
end
|
||||||
|
http_resp.each_header do |key, value|
|
||||||
|
resp.headers[key] = value
|
||||||
|
end
|
||||||
end
|
end
|
||||||
resp.processed!
|
|
||||||
http_resp.each_header do |key, value|
|
|
||||||
resp.headers[key] = value
|
|
||||||
end
|
|
||||||
resp
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -15,7 +15,6 @@ module Faraday
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
# Override in a subclass, or include an adapter
|
# Override in a subclass, or include an adapter
|
||||||
#
|
#
|
||||||
# def _get(uri, headers)
|
# def _get(uri, headers)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user