Module: SimpleAcp::Models::Events

Defined in:
lib/simple_acp/models/events.rb

Overview

Factory module for parsing events from hashes.

Constant Summary collapse

EVENT_CLASSES =

Mapping of event types to classes

{
  Types::EventType::MESSAGE_CREATED => MessageCreatedEvent,
  Types::EventType::MESSAGE_PART => MessagePartEvent,
  Types::EventType::MESSAGE_COMPLETED => MessageCompletedEvent,
  Types::EventType::RUN_CREATED => RunCreatedEvent,
  Types::EventType::RUN_IN_PROGRESS => RunInProgressEvent,
  Types::EventType::RUN_AWAITING => RunAwaitingEvent,
  Types::EventType::RUN_COMPLETED => RunCompletedEvent,
  Types::EventType::RUN_CANCELLED => RunCancelledEvent,
  Types::EventType::RUN_FAILED => RunFailedEvent,
  Types::EventType::GENERIC => GenericEvent,
  Types::EventType::ERROR => ErrorEvent
}.freeze

Class Method Summary collapse

Class Method Details

.from_hash(hash) ⇒ Event?

Parse an event from a hash.

Parameters:

  • hash (Hash, nil) —

    event data with "type" key

Returns:

  • (Event, nil) —

    the appropriate event subclass or nil



237
238
239
240
241
242
243
# File 'lib/simple_acp/models/events.rb', line 237

def self.from_hash(hash)
  return nil if hash.nil?

  type = hash["type"] || hash[:type]
  klass = EVENT_CLASSES[type] || GenericEvent
  klass.from_hash(hash)
end

.parse_sse(data) ⇒ Hash?

Parse JSON SSE data.

Parameters:

  • data (String) —

    JSON string

Returns:

  • (Hash, nil) —

    parsed hash or nil if invalid



249
250
251
252
253
# File 'lib/simple_acp/models/events.rb', line 249

def self.parse_sse(data)
  JSON.parse(data)
rescue JSON::ParserError
  nil
end