Attributes
request | [R] |
Public Class methods
new(request, session)
[show source]
# File lib/httpx/plugins/stream.rb 7 def initialize(request, session) 8 @request = request 9 @options = @request.options 10 @session = session 11 @response_enum = nil 12 @buffered_chunks = [] 13 end
Public Instance methods
each(&block)
[show source]
# File lib/httpx/plugins/stream.rb 15 def each(&block) 16 return enum_for(__method__) unless block 17 18 if (response_enum = @response_enum) 19 @response_enum = nil 20 # streaming already started, let's finish it 21 22 while (chunk = @buffered_chunks.shift) 23 block.call(chunk) 24 end 25 26 # consume enum til the end 27 begin 28 while (chunk = response_enum.next) 29 block.call(chunk) 30 end 31 rescue StopIteration 32 return 33 end 34 end 35 36 @request.stream = self 37 38 begin 39 @on_chunk = block 40 41 response = @session.request(@request) 42 43 response.raise_for_status 44 ensure 45 @on_chunk = nil 46 end 47 end
each_line()
[show source]
# File lib/httpx/plugins/stream.rb 49 def each_line 50 return enum_for(__method__) unless block_given? 51 52 line = "".b 53 54 each do |chunk| 55 line << chunk 56 57 while (idx = line.index("\n")) 58 yield line.byteslice(0..idx - 1) 59 60 line = line.byteslice(idx + 1..-1) 61 end 62 end 63 64 yield line unless line.empty? 65 end
inspect()
:nocov:
[show source]
# File lib/httpx/plugins/stream.rb 75 def inspect 76 "#<#{self.class}:#{object_id}>" 77 end
on_chunk(chunk)
This is a ghost method. It’s to be used ONLY internally, when processing streams
[show source]
# File lib/httpx/plugins/stream.rb 68 def on_chunk(chunk) 69 raise NoMethodError unless @on_chunk 70 71 @on_chunk.call(chunk) 72 end
to_s()
:nocov:
[show source]
# File lib/httpx/plugins/stream.rb 80 def to_s 81 if @request.response 82 @request.response.to_s 83 else 84 @buffered_chunks.join 85 end 86 end