Class: SimpleAcp::Client::SSEParser

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_acp/client/sse.rb

Overview

Server-Sent Events (SSE) parser for streaming responses.

Parses chunked SSE data into structured events following the SSE specification (event, data, id, retry fields).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ SSEParser

Create a new SSE parser.



14
15
16
17
18
19
# File 'lib/simple_acp/client/sse.rb', line 14

def initialize
  @buffer = ""
  @events = []
  @current_event = nil
  @current_data = []
end

Instance Attribute Details

#events ⇒ Array<Hash> (readonly)

Returns parsed events with :event and :data keys.

Returns:

  • (Array<Hash>) —

    parsed events with :event and :data keys



11
12
13
# File 'lib/simple_acp/client/sse.rb', line 11

def events
  @events
end

Instance Method Details

#each_event {|Hash| ... } ⇒ void

This method returns an undefined value.

Iterate over parsed events, clearing them after yield.

Yields:

  • (Hash) —

    event with :event (type) and :data (payload) keys



35
36
37
38
39
40
# File 'lib/simple_acp/client/sse.rb', line 35

def each_event
  flush_events
  while (event = @events.shift)
    yield event
  end
end

#feed(chunk) ⇒ void

This method returns an undefined value.

Feed a chunk of data to the parser.

Parameters:

  • chunk (String) —

    raw SSE data chunk



25
26
27
28
29
# File 'lib/simple_acp/client/sse.rb', line 25

def feed(chunk)
  @buffer += chunk
  parse_buffer
  flush_events
end