Class: Slk::Models::User
- Inherits:
-
Data
- Object
- Data
- Slk::Models::User
- Defined in:
- lib/slk/models/user.rb
Instance Attribute Summary collapse
-
#display_name ⇒ Object
readonly
Returns the value of attribute display_name.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#is_bot ⇒ Object
readonly
Returns the value of attribute is_bot.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#real_name ⇒ Object
readonly
Returns the value of attribute real_name.
Class Method Summary collapse
Instance Method Summary collapse
- #best_name ⇒ Object
-
#initialize(id:, name: nil, real_name: nil, display_name: nil, is_bot: false) ⇒ User
constructor
A new instance of User.
- #mention ⇒ Object
- #to_s ⇒ Object
- #validate_id!(id_str) ⇒ Object
Constructor Details
#initialize(id:, name: nil, real_name: nil, display_name: nil, is_bot: false) ⇒ User
Returns a new instance of User.
21 22 23 24 25 26 27 28 29 |
# File 'lib/slk/models/user.rb', line 21 def initialize(id:, name: nil, real_name: nil, display_name: nil, is_bot: false) id_str = id.to_s.strip validate_id!(id_str) super( id: id_str.freeze, name: name&.freeze, real_name: real_name&.freeze, display_name: display_name&.freeze, is_bot: is_bot ) end |
Instance Attribute Details
#display_name ⇒ Object (readonly)
Returns the value of attribute display_name
8 9 10 |
# File 'lib/slk/models/user.rb', line 8 def display_name @display_name end |
#id ⇒ Object (readonly)
Returns the value of attribute id
8 9 10 |
# File 'lib/slk/models/user.rb', line 8 def id @id end |
#is_bot ⇒ Object (readonly)
Returns the value of attribute is_bot
8 9 10 |
# File 'lib/slk/models/user.rb', line 8 def is_bot @is_bot end |
#name ⇒ Object (readonly)
Returns the value of attribute name
8 9 10 |
# File 'lib/slk/models/user.rb', line 8 def name @name end |
#real_name ⇒ Object (readonly)
Returns the value of attribute real_name
8 9 10 |
# File 'lib/slk/models/user.rb', line 8 def real_name @real_name end |
Class Method Details
.from_api(data) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/slk/models/user.rb', line 9 def self.from_api(data) profile = data['profile'] || {} new( id: data['id'], name: data['name'], real_name: profile['real_name'] || data['real_name'], display_name: profile['display_name'] || profile['display_name_normalized'], is_bot: data['is_bot'] || false ) end |
Instance Method Details
#best_name ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/slk/models/user.rb', line 38 def best_name return display_name unless display_name.to_s.empty? return real_name unless real_name.to_s.empty? return name unless name.to_s.empty? id end |
#mention ⇒ Object
46 47 48 |
# File 'lib/slk/models/user.rb', line 46 def mention "@#{best_name}" end |
#to_s ⇒ Object
50 51 52 |
# File 'lib/slk/models/user.rb', line 50 def to_s best_name end |
#validate_id!(id_str) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/slk/models/user.rb', line 31 def validate_id!(id_str) raise ArgumentError, 'user id cannot be empty' if id_str.empty? return if id_str.match?(USER_ID_PATTERN) raise ArgumentError, "invalid user id format: #{id_str} (expected U or W prefix)" end |