Class: HermesAgent::Client::Stream
- Inherits:
-
Object
- Object
- HermesAgent::Client::Stream
- Includes:
- Enumerable
- Defined in:
- lib/hermes_agent/client/stream.rb
Overview
A consumable Server-Sent Events stream.
Stream parses the SSE frames emitted by a streaming endpoint, wrapping
each frame's data payload (parsed as JSON) in an event wrapper object,
and implements the block-or-enumerator contract:
- Iterated with a block (via #each), it yields each event as it arrives, giving natural backpressure over the network read.
- Without a block it is an
Enumerablethe caller drives itself.
After the stream is fully consumed, #result returns the aggregated final object (built by the aggregator block given at construction). It is a single-pass stream over a live network read: it can be iterated once.
The class is HTTP-agnostic — it consumes anything that yields String
byte chunks via #each (the http gem's response body, or an array of
chunks in tests) — so it never owns or manages the network connection
itself.
Instance Method Summary collapse
-
#each {|event| ... } ⇒ self, Enumerator
Iterate the events.
-
#result ⇒ Object?
The aggregated final object, available after the stream is consumed.
Instance Method Details
#each {|event| ... } ⇒ self, Enumerator
Iterate the events. With a block, yields each event as it is parsed and
returns self. Without a block, returns an Enumerator.
75 76 77 78 79 80 81 82 |
# File 'lib/hermes_agent/client/stream.rb', line 75 def each(&block) return enum_for(:each) unless block raise Error, "Stream has already been consumed" if @consumed consume(&block) self end |
#result ⇒ Object?
The aggregated final object, available after the stream is consumed. Consuming the stream first if it has not been iterated.
91 92 93 94 |
# File 'lib/hermes_agent/client/stream.rb', line 91 def result consume unless @consumed @result end |