lib/httpx/plugins/stream.rb in httpx-1.1.4 vs lib/httpx/plugins/stream.rb in httpx-1.1.5
- old
+ new
@@ -3,10 +3,11 @@
module HTTPX
class StreamResponse
def initialize(request, session)
@request = request
@session = session
+ @response = nil
end
def each(&block)
return enum_for(__method__) unless block
@@ -23,21 +24,23 @@
end
def each_line
return enum_for(__method__) unless block_given?
- line = +""
+ line = "".b
each do |chunk|
line << chunk
while (idx = line.index("\n"))
yield line.byteslice(0..idx - 1)
line = line.byteslice(idx + 1..-1)
end
end
+
+ yield line unless line.empty?
end
# This is a ghost method. It's to be used ONLY internally, when processing streams
def on_chunk(chunk)
raise NoMethodError unless @on_chunk
@@ -56,11 +59,13 @@
end
private
def response
- @response ||= begin
- @request.response || @session.request(@request)
+ return @response if @response
+
+ @request.response || begin
+ @response = @session.request(@request)
end
end
def respond_to_missing?(meth, *args)
response.respond_to?(meth, *args) || super