Class: Lita::User
- Inherits:
-
Object
- Object
- Lita::User
- Defined in:
- lib/lita/user.rb
Overview
A user in the chat service. Persisted in Redis.
Instance Attribute Summary collapse
-
#id ⇒ String
readonly
The user’s unique ID.
-
#metadata ⇒ Hash
readonly
A hash of arbitrary metadata about the user.
-
#name ⇒ String
readonly
The user’s name as displayed in the chat.
Class Method Summary collapse
-
.create(id, metadata = {}) ⇒ Lita::User
Creates a new user with the given ID, or merges and saves supplied metadata to an existing user with the given ID.
-
.find_by_id(id) ⇒ Lita::User?
Finds a user by ID.
-
.find_by_mention_name(mention_name) ⇒ Lita::User?
Finds a user by mention name.
-
.find_by_name(name) ⇒ Lita::User?
Finds a user by display name.
-
.find_by_partial_name(name) ⇒ Lita::User?
Attempts to find a user with a name starting with the provided string.
-
.fuzzy_find(identifier) ⇒ Lita::User?
Finds a user by ID, mention name, name, or partial name.
-
.redis ⇒ Redis::Namespace
The
Redis::Namespace
for user persistence.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Compares the user against another user object to determine equality.
-
#hash ⇒ Fixnum
Generates a
Fixnum
hash value for this user object. -
#initialize(id, metadata = {}) ⇒ User
constructor
A new instance of User.
-
#mention_name ⇒ String
The name used to “mention” the user in a group chat.
-
#save ⇒ void
Saves the user record to Redis, overwriting any previous data for the current ID and user name.
Constructor Details
#initialize(id, metadata = {}) ⇒ User
Returns a new instance of User.
89 90 91 92 93 94 |
# File 'lib/lita/user.rb', line 89 def initialize(id, = {}) @id = id.to_s @metadata = Util.stringify_keys() @name = @metadata["name"] || @id end |
Instance Attribute Details
#id ⇒ String (readonly)
The user’s unique ID.
76 77 78 |
# File 'lib/lita/user.rb', line 76 def id @id end |
#metadata ⇒ Hash (readonly)
A hash of arbitrary metadata about the user.
80 81 82 |
# File 'lib/lita/user.rb', line 80 def @metadata end |
#name ⇒ String (readonly)
The user’s name as displayed in the chat.
84 85 86 |
# File 'lib/lita/user.rb', line 84 def name @name end |
Class Method Details
.create(id, metadata = {}) ⇒ Lita::User
Creates a new user with the given ID, or merges and saves supplied metadata to an existing user with the given ID.
17 18 19 20 21 22 23 24 |
# File 'lib/lita/user.rb', line 17 def create(id, = {}) existing_user = find_by_id(id) = Util.stringify_keys() = existing_user..merge() if existing_user user = new(id, ) user.save user end |
.find_by_id(id) ⇒ Lita::User?
Finds a user by ID.
29 30 31 32 |
# File 'lib/lita/user.rb', line 29 def find_by_id(id) = redis.hgetall("id:#{id}") new(id, ) if .key?("name") end |
.find_by_mention_name(mention_name) ⇒ Lita::User?
Finds a user by mention name.
38 39 40 41 |
# File 'lib/lita/user.rb', line 38 def find_by_mention_name(mention_name) id = redis.get("mention_name:#{mention_name}") find_by_id(id) if id end |
.find_by_name(name) ⇒ Lita::User?
Finds a user by display name.
46 47 48 49 |
# File 'lib/lita/user.rb', line 46 def find_by_name(name) id = redis.get("name:#{name}") find_by_id(id) if id end |
.find_by_partial_name(name) ⇒ Lita::User?
Attempts to find a user with a name starting with the provided string.
55 56 57 58 59 60 61 62 |
# File 'lib/lita/user.rb', line 55 def find_by_partial_name(name) keys = redis.keys("name:#{name}*") if keys.length == 1 id = redis.get(keys.first) find_by_id(id) end end |
.fuzzy_find(identifier) ⇒ Lita::User?
Finds a user by ID, mention name, name, or partial name.
68 69 70 71 |
# File 'lib/lita/user.rb', line 68 def fuzzy_find(identifier) find_by_id(identifier) || find_by_mention_name(identifier) || find_by_name(identifier) || find_by_partial_name(identifier) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Compares the user against another user object to determine equality. Users are considered equal if they have the same ID and name.
120 121 122 |
# File 'lib/lita/user.rb', line 120 def ==(other) other.respond_to?(:id) && id == other.id && other.respond_to?(:name) && name == other.name end |
#hash ⇒ Fixnum
Generates a Fixnum
hash value for this user object. Implemented to support equality.
128 129 130 |
# File 'lib/lita/user.rb', line 128 def hash id.hash ^ name.hash end |
#mention_name ⇒ String
The name used to “mention” the user in a group chat.
99 100 101 |
# File 'lib/lita/user.rb', line 99 def mention_name ["mention_name"] || name end |
#save ⇒ void
This method returns an undefined value.
Saves the user record to Redis, overwriting any previous data for the current ID and user name.
106 107 108 109 110 111 112 113 114 |
# File 'lib/lita/user.rb', line 106 def save mention_name = [:mention_name] || ["mention_name"] redis.pipelined do redis.hmset("id:#{id}", *.to_a.flatten) redis.set("name:#{name}", id) redis.set("mention_name:#{mention_name}", id) if mention_name end end |