Class: Aikido::Zen::Actor

Inherits:
Object
  • Object
show all
Defined in:
lib/aikido/zen/actor.rb

Overview

Represents someone connecting to the application and making requests.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, name: nil, ip: Aikido::Zen.current_context&.request&.ip, seen_at: Time.now.utc) ⇒ Actor

Returns a new instance of Actor.

Parameters:

  • id (String)
  • name (String, nil) (defaults to: nil)
  • ip (String, nil) (defaults to: Aikido::Zen.current_context&.request&.ip)
  • seen_at (Time) (defaults to: Time.now.utc)


54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/aikido/zen/actor.rb', line 54

def initialize(
  id:,
  name: nil,
  ip: Aikido::Zen.current_context&.request&.ip,
  seen_at: Time.now.utc
)
  @id = id
  @name = name
  @first_seen_at = seen_at
  @last_seen_at = Concurrent::AtomicReference.new(seen_at)
  @ip = Concurrent::AtomicReference.new(ip)
end

Instance Attribute Details

#first_seen_atTime (readonly)

Returns:

  • (Time)


48
49
50
# File 'lib/aikido/zen/actor.rb', line 48

def first_seen_at
  @first_seen_at
end

#idString (readonly)

Returns a unique identifier for this user.

Returns:

  • (String)

    a unique identifier for this user.



42
43
44
# File 'lib/aikido/zen/actor.rb', line 42

def id
  @id
end

#nameString? (readonly)

Returns an optional name to display in the Aikido UI.

Returns:

  • (String, nil)

    an optional name to display in the Aikido UI.



45
46
47
# File 'lib/aikido/zen/actor.rb', line 45

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



97
98
99
# File 'lib/aikido/zen/actor.rb', line 97

def ==(other)
  other.is_a?(Actor) && id == other.id
end

#as_jsonObject



106
107
108
109
110
111
112
113
114
# File 'lib/aikido/zen/actor.rb', line 106

def as_json
  {
    id: id,
    name: name,
    lastIpAddress: ip,
    firstSeenAt: first_seen_at.to_i * 1000,
    lastSeenAt: last_seen_at.to_i * 1000
  }.compact
end

#hashObject



102
103
104
# File 'lib/aikido/zen/actor.rb', line 102

def hash
  id.hash
end

#ipString?

Returns the IP address last used by this user, if available.

Returns:

  • (String, nil)

    the IP address last used by this user, if available.



73
74
75
# File 'lib/aikido/zen/actor.rb', line 73

def ip
  @ip.get
end

#last_seen_atTime

Returns:

  • (Time)


68
69
70
# File 'lib/aikido/zen/actor.rb', line 68

def last_seen_at
  @last_seen_at.get
end

#to_aikido_actorself

Returns:

  • (self)


93
94
95
# File 'lib/aikido/zen/actor.rb', line 93

def to_aikido_actor
  self
end

#update(seen_at: Time.now.utc, ip: Aikido::Zen.current_context&.request&.ip) ⇒ void

This method returns an undefined value.

Atomically update the last IP used by the user, and the last time they’ve been “seen” connecting to the app.

Parameters:

  • ip (String, nil) (defaults to: Aikido::Zen.current_context&.request&.ip)

    the last-seen IP address for the user. If nil and we had a non-empty value before, we won’t update it. Defaults to the current HTTP request’s IP address, if any.

  • seen_at (Time) (defaults to: Time.now.utc)

    the time at which we’re making the update. We will always keep the most recent time if this conflicts with the current value.



87
88
89
90
# File 'lib/aikido/zen/actor.rb', line 87

def update(seen_at: Time.now.utc, ip: Aikido::Zen.current_context&.request&.ip)
  @last_seen_at.try_update { |last_seen_at| [last_seen_at, seen_at].max }
  @ip.try_update { |last_ip| [ip, last_ip].compact.first }
end