Class: HTTPX::StreamResponse
- Inherits:
-
Object
- Object
- HTTPX::StreamResponse
- Defined in:
- lib/httpx/plugins/stream.rb
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #each_line ⇒ Object
-
#initialize(request, session, connections) ⇒ 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, connections) ⇒ StreamResponse
Returns a new instance of StreamResponse.
5 6 7 8 9 |
# File 'lib/httpx/plugins/stream.rb', line 5 def initialize(request, session, connections) @request = request @session = session @connections = connections end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object (private)
83 84 85 86 87 |
# File 'lib/httpx/plugins/stream.rb', line 83 def method_missing(meth, *args, &block) return super unless response.respond_to?(meth) response.__send__(meth, *args, &block) end |
Instance Method Details
#each(&block) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/httpx/plugins/stream.rb', line 11 def each(&block) return enum_for(__method__) unless block raise Error, "response already streamed" if @response @request.stream = self begin @on_chunk = block if @request.response # if we've already started collecting the payload, yield it first # before proceeding body = @request.response.body body.each do |chunk| on_chunk(chunk) end end response.raise_for_status response.close ensure @on_chunk = nil end end |
#each_line ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/httpx/plugins/stream.rb', line 38 def each_line return enum_for(__method__) unless block_given? line = +"" each do |chunk| line << chunk while (idx = line.index("\n")) yield line.byteslice(0..idx - 1) line = line.byteslice(idx + 1..-1) end end end |
#inspect ⇒ Object
:nocov:
62 63 64 |
# File 'lib/httpx/plugins/stream.rb', line 62 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
55 56 57 58 59 |
# File 'lib/httpx/plugins/stream.rb', line 55 def on_chunk(chunk) raise NoMethodError unless @on_chunk @on_chunk.call(chunk) end |
#to_s ⇒ Object
:nocov:
67 68 69 |
# File 'lib/httpx/plugins/stream.rb', line 67 def to_s response.to_s end |