Module: Chatrix::Events

Defined in:
lib/chatrix/events.rb

Overview

Keeps track of what events have already been processed.

Class Method Summary collapse

Class Method Details

.parse_event(event) ⇒ String

Extract the event ID from an event object. If this is a string, it's returned verbatim.

Parameters:

  • event (String, Hash)

    The event object to extract information from.

Returns:

  • (String)

    The event ID.

Raises:

  • ArgumentError if the event object is of an invalid type or does not contain an ID.



32
33
34
35
36
37
38
39
40
# File 'lib/chatrix/events.rb', line 32

def self.parse_event(event)
  if event.is_a? String
    event
  elsif event.is_a?(Hash) && event.key?('event_id')
    event['event_id']
  else
    raise ArgumentError, 'Invalid event object'
  end
end

.processed(event) ⇒ Object

Marks an event as having been processed.

Parameters:

  • event (String, Hash)

    The affected event.



11
12
13
# File 'lib/chatrix/events.rb', line 11

def self.processed(event)
  @processed.push parse_event event
end

.processed?(event) ⇒ Boolean

Checks if an event has been processed.

Parameters:

  • event (String, Hash)

    The event to check.

Returns:

  • (Boolean)

    true if the event has been processed, otherwise false.



20
21
22
# File 'lib/chatrix/events.rb', line 20

def self.processed?(event)
  @processed.member? parse_event event
end