Class: MoondreamClient::Client::SSEDecoder

Inherits:
Object
  • Object
show all
Defined in:
lib/moondream-client/client.rb

Overview

Minimal SSE decoder for parsing standard server-sent event lines.

Instance Method Summary collapse

Constructor Details

#initializeSSEDecoder

Returns a new instance of SSEDecoder.



110
111
112
113
114
115
# File 'lib/moondream-client/client.rb', line 110

def initialize
  @event = ""
  @data = ""
  @id = nil
  @retry = nil
end

Instance Method Details

#decode(line) ⇒ Hash?

Parameters:

  • line (String)

Returns:

  • (Hash, nil)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/moondream-client/client.rb', line 119

def decode(line)
  return flush_event if line.empty?
  return if line.start_with?(":")

  field, _, value = line.partition(":")
  value = value.lstrip

  case field
  when "event"
    @event = value
  when "data"
    @data += "#{value}\n"
  when "id"
    @id = value
  when "retry"
    @retry = value.to_i
  end

  nil
end