Class: Aikido::Zen::Actor
- Inherits:
-
Object
- Object
- Aikido::Zen::Actor
- Defined in:
- lib/aikido/zen/actor.rb
Overview
Represents someone connecting to the application and making requests.
Instance Attribute Summary collapse
- #first_seen_at ⇒ Time readonly
-
#id ⇒ String
readonly
A unique identifier for this user.
-
#name ⇒ String?
readonly
An optional name to display in the Aikido UI.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #as_json ⇒ Object
- #hash ⇒ Object
-
#initialize(id:, name: nil, ip: Aikido::Zen.current_context&.request&.ip, seen_at: Time.now.utc) ⇒ Actor
constructor
A new instance of Actor.
-
#ip ⇒ String?
The IP address last used by this user, if available.
- #last_seen_at ⇒ Time
- #to_aikido_actor ⇒ self
-
#update(seen_at: Time.now.utc, ip: Aikido::Zen.current_context&.request&.ip) ⇒ void
Atomically update the last IP used by the user, and the last time they’ve been “seen” connecting to the app.
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.
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_at ⇒ Time (readonly)
48 49 50 |
# File 'lib/aikido/zen/actor.rb', line 48 def first_seen_at @first_seen_at end |
#id ⇒ String (readonly)
Returns a unique identifier for this user.
42 43 44 |
# File 'lib/aikido/zen/actor.rb', line 42 def id @id end |
#name ⇒ String? (readonly)
Returns 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_json ⇒ Object
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 |
#hash ⇒ Object
102 103 104 |
# File 'lib/aikido/zen/actor.rb', line 102 def hash id.hash end |
#ip ⇒ String?
Returns 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_at ⇒ Time
68 69 70 |
# File 'lib/aikido/zen/actor.rb', line 68 def last_seen_at @last_seen_at.get end |
#to_aikido_actor ⇒ 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.
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 |