Class: QQQ::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/qqq/events.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuid:, message:, recorded_at:) ⇒ Event

Returns a new instance of Event.



17
18
19
20
21
# File 'lib/qqq/events.rb', line 17

def initialize(uuid:, message:, recorded_at:)
  @uuid = uuid
  @recorded_at = recorded_at
  @message = message
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



4
5
6
# File 'lib/qqq/events.rb', line 4

def message
  @message
end

#recorded_atObject (readonly)

Returns the value of attribute recorded_at.



4
5
6
# File 'lib/qqq/events.rb', line 4

def recorded_at
  @recorded_at
end

#uuidObject (readonly)

Returns the value of attribute uuid.



4
5
6
# File 'lib/qqq/events.rb', line 4

def uuid
  @uuid
end

Class Method Details

.from_json_string(json_string) ⇒ Object



12
13
14
15
# File 'lib/qqq/events.rb', line 12

def self.from_json_string(json_string)
  payload = JSON.parse json_string
  Event.new(uuid: payload["uuid"], message: payload["message"], recorded_at: payload["recorded_at"])
end

.from_message(message) ⇒ Object



6
7
8
9
10
# File 'lib/qqq/events.rb', line 6

def self.from_message(message)
  timestamp = Time.now
  uuid = UUIDTools::UUID.random_create().to_s
  Event.new(uuid: uuid, message: message, recorded_at: timestamp)
end

Instance Method Details

#as_json(options = {}) ⇒ Object



27
28
29
30
31
32
# File 'lib/qqq/events.rb', line 27

def as_json(options={})
  { uuid: @uuid,
    message: @message,
    recorded_at: @recorded_at,
  }
end

#for_humansObject



23
24
25
# File 'lib/qqq/events.rb', line 23

def for_humans
  "[#{@uuid}] [#{@recorded_at}] #{@message}"
end

#to_json(*options) ⇒ Object



34
35
36
# File 'lib/qqq/events.rb', line 34

def to_json(*options)
  as_json(*options).to_json(*options)
end