Class: A2A::Transport::SSEEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/a2a/transport/sse.rb

Overview

SSE Event representation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, data: nil, id: nil, retry_interval: nil) ⇒ SSEEvent

Initialize SSE event

Parameters:

  • type (String)

    Event type

  • data (Object) (defaults to: nil)

    Event data

  • id (String, nil) (defaults to: nil)

    Event ID

  • retry_interval (Integer, nil) (defaults to: nil)

    Retry interval



436
437
438
439
440
441
442
# File 'lib/a2a/transport/sse.rb', line 436

def initialize(type:, data: nil, id: nil, retry_interval: nil)
  @type = type.to_s
  @data = data
  @id = id
  @retry = retry_interval
  @timestamp = Time.now
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



426
427
428
# File 'lib/a2a/transport/sse.rb', line 426

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



426
427
428
# File 'lib/a2a/transport/sse.rb', line 426

def id
  @id
end

#retryObject (readonly)

Returns the value of attribute retry.



426
427
428
# File 'lib/a2a/transport/sse.rb', line 426

def retry
  @retry
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



426
427
428
# File 'lib/a2a/transport/sse.rb', line 426

def timestamp
  @timestamp
end

#typeObject (readonly)

Returns the value of attribute type.



426
427
428
# File 'lib/a2a/transport/sse.rb', line 426

def type
  @type
end

Instance Method Details

#has_data?Boolean

Check if event has data

Returns:

  • (Boolean)

    True if event has data



494
495
496
# File 'lib/a2a/transport/sse.rb', line 494

def has_data?
  !@data.nil?
end

#to_hHash

Convert to hash representation

Returns:

  • (Hash)

    Event as hash



469
470
471
472
473
474
475
476
477
# File 'lib/a2a/transport/sse.rb', line 469

def to_h
  {
    type: @type,
    data: @data,
    id: @id,
    retry: @retry,
    timestamp: @timestamp.iso8601
  }.compact
end

#to_sse_formatString

Convert event to SSE format

Returns:

  • (String)

    SSE formatted string



449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/a2a/transport/sse.rb', line 449

def to_sse_format
  lines = []
  lines << "event: #{@type}" if @type != "message"
  lines << "id: #{@id}" if @id
  lines << "retry: #{@retry}" if @retry

  data_json = @data.is_a?(String) ? @data : @data.to_json
  data_json.split("\n").each do |line|
    lines << "data: #{line}"
  end

  lines << ""
  lines.join("\n")
end

#type?(event_type) ⇒ Boolean

Check if event is of specific type

Parameters:

  • event_type (String)

    Event type to check

Returns:

  • (Boolean)

    True if event matches type



485
486
487
# File 'lib/a2a/transport/sse.rb', line 485

def type?(event_type)
  @type == event_type.to_s
end