Class: EventMeter::EventPayload

Inherits:
Object
  • Object
show all
Defined in:
lib/event_meter/event_payload.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, params:, status:, started_at:, duration_ms:) ⇒ EventPayload

Returns a new instance of EventPayload.



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

def initialize(name:, params:, status:, started_at:, duration_ms:)
  now = Time.now.utc

  @name = normalize_name(name)
  @status = normalize_status(status || "success")
  @started_at = parse_time(started_at || now)
  @duration_ms = integer_or_nil(duration_ms)
  @params = stringify(params)
end

Instance Attribute Details

#duration_msObject (readonly)

Returns the value of attribute duration_ms.



8
9
10
# File 'lib/event_meter/event_payload.rb', line 8

def duration_ms
  @duration_ms
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/event_meter/event_payload.rb', line 8

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/event_meter/event_payload.rb', line 8

def params
  @params
end

#started_atObject (readonly)

Returns the value of attribute started_at.



8
9
10
# File 'lib/event_meter/event_payload.rb', line 8

def started_at
  @started_at
end

#statusObject (readonly)

Returns the value of attribute status.



8
9
10
# File 'lib/event_meter/event_payload.rb', line 8

def status
  @status
end

Class Method Details

.build(name, params:, status: nil, started_at: nil, duration_ms: nil) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/event_meter/event_payload.rb', line 10

def self.build(name, params:, status: nil, started_at: nil, duration_ms: nil)
  new(
    name: name,
    params: params,
    status: status,
    started_at: started_at,
    duration_ms: duration_ms
  )
end

.load(hash) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/event_meter/event_payload.rb', line 20

def self.load(hash)
  hash = stringify(hash)

  new(
    name: hash.fetch("name"),
    params: hash.fetch("params", {}),
    status: hash["status"],
    started_at: hash.fetch("started_at"),
    duration_ms: hash["duration_ms"]
  )
end

.stringify(hash) ⇒ Object



62
63
64
65
66
# File 'lib/event_meter/event_payload.rb', line 62

def self.stringify(hash)
  HashInput.coerce(hash, "event params").each_with_object({}) do |(key, value), result|
    result[key.to_s] = value
  end
end

Instance Method Details

#started_msObject



56
57
58
# File 'lib/event_meter/event_payload.rb', line 56

def started_ms
  (started_at.to_f * 1000).to_i
end

#to_hObject



42
43
44
45
46
47
48
49
50
# File 'lib/event_meter/event_payload.rb', line 42

def to_h
  {
    "name" => name,
    "status" => status,
    "started_at" => started_at.iso8601(6),
    "duration_ms" => duration_ms,
    "params" => params
  }.delete_if { |_key, value| value.nil? || value == {} }
end

#to_json(*args) ⇒ Object



52
53
54
# File 'lib/event_meter/event_payload.rb', line 52

def to_json(*args)
  to_h.to_json(*args)
end