Class: HTTPX::StreamResponse
- Inherits:
-
Object
- Object
- HTTPX::StreamResponse
- Defined in:
- lib/httpx/plugins/stream.rb
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #each_line {|line| ... } ⇒ Object
-
#initialize(request, session) ⇒ StreamResponse
constructor
A new instance of StreamResponse.
-
#inspect ⇒ Object
:nocov:.
-
#on_chunk(chunk) ⇒ Object
This is a ghost method.
-
#to_s ⇒ Object
:nocov:.
Constructor Details
#initialize(request, session) ⇒ StreamResponse
Returns a new instance of StreamResponse.
5 6 7 8 9 10 11 |
# File 'lib/httpx/plugins/stream.rb', line 5 def initialize(request, session) @request = request = @request. @session = session @response_enum = nil @buffered_chunks = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, **kwargs, &block) ⇒ Object (private)
108 109 110 111 112 |
# File 'lib/httpx/plugins/stream.rb', line 108 def method_missing(meth, *args, **kwargs, &block) return super unless response.respond_to?(meth) response.__send__(meth, *args, **kwargs, &block) end |
Instance Method Details
#each(&block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/httpx/plugins/stream.rb', line 13 def each(&block) return enum_for(__method__) unless block if (response_enum = @response_enum) @response_enum = nil # streaming already started, let's finish it while (chunk = @buffered_chunks.shift) block.call(chunk) end # consume enum til the end begin while (chunk = response_enum.next) block.call(chunk) end rescue StopIteration return end end @request.stream = self begin @on_chunk = block response = @session.request(@request) response.raise_for_status ensure @on_chunk = nil end end |
#each_line {|line| ... } ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/httpx/plugins/stream.rb', line 47 def each_line return enum_for(__method__) unless block_given? 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 |
#inspect ⇒ Object
:nocov:
73 74 75 |
# File 'lib/httpx/plugins/stream.rb', line 73 def inspect "#<StreamResponse:#{object_id}>" end |
#on_chunk(chunk) ⇒ Object
This is a ghost method. It’s to be used ONLY internally, when processing streams
66 67 68 69 70 |
# File 'lib/httpx/plugins/stream.rb', line 66 def on_chunk(chunk) raise NoMethodError unless @on_chunk @on_chunk.call(chunk) end |
#to_s ⇒ Object
:nocov:
78 79 80 81 82 83 84 |
# File 'lib/httpx/plugins/stream.rb', line 78 def to_s if @request.response @request.response.to_s else @buffered_chunks.join end end |