Class: Hey::Pubsub::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/hey/pubsub/event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, started_at: nil, ended_at: nil, metadata: {}) ⇒ Event

Returns a new instance of Event.



5
6
7
8
9
10
# File 'lib/hey/pubsub/event.rb', line 5

def initialize(name:, started_at: nil, ended_at: nil, metadata: {})
  @name = name
  @started_at = started_at
  @ended_at = ended_at
  @metadata = 
end

Instance Attribute Details

#ended_atObject



25
26
27
28
# File 'lib/hey/pubsub/event.rb', line 25

def ended_at
  return if @ended_at.nil?
  @ended_at.strftime("%Y-%m-%dT%H:%M:%S.%L")
end

#metadataObject



30
31
32
33
34
35
36
# File 'lib/hey/pubsub/event.rb', line 30

def 
   = Hey::ThreadCargo.contexts.reverse.map(&:to_h).reduce(Hash.new, :merge)
  merged_data = .merge(@metadata)
  hash = Hey::SanitizedHash.new(merged_data).to_h
  hash.delete(:sanitize)
  hash
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/hey/pubsub/event.rb', line 3

def name
  @name
end

#started_atObject



20
21
22
23
# File 'lib/hey/pubsub/event.rb', line 20

def started_at
  return if @started_at.nil?
  @started_at.strftime("%Y-%m-%dT%H:%M:%S.%L")
end

Instance Method Details

#durationObject



38
39
40
41
# File 'lib/hey/pubsub/event.rb', line 38

def duration
  return if @ended_at.nil? || @started_at.nil?
  1000.0 * (@ended_at - @started_at)
end

#to_hObject



12
13
14
15
16
17
18
# File 'lib/hey/pubsub/event.rb', line 12

def to_h
  hash = { uuid: Hey::ThreadCargo.uuid, name: name, metadata:  }
  hash[:started_at] = started_at unless started_at.nil?
  hash[:ended_at] = ended_at unless ended_at.nil?
  hash[:duration] = duration unless duration.nil?
  hash
end