Class: MadChatter::User
- Inherits:
-
Object
- Object
- MadChatter::User
- Defined in:
- lib/mad_chatter/user.rb
Instance Attribute Summary collapse
-
#token ⇒ Object
Returns the value of attribute token.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #connected ⇒ Object
- #disconnected ⇒ Object
- #generate_new_token ⇒ Object
- #has_token?(token) ⇒ Boolean
-
#initialize(token = nil, username = nil) ⇒ User
constructor
A new instance of User.
- #on_send(&blk) ⇒ Object
- #send(json) ⇒ Object
- #send_channels ⇒ Object
- #send_token ⇒ Object
- #send_users_list ⇒ Object
- #update_username(username) ⇒ Object
Constructor Details
#initialize(token = nil, username = nil) ⇒ User
Returns a new instance of User.
6 7 8 9 |
# File 'lib/mad_chatter/user.rb', line 6 def initialize(token = nil, username = nil) @token = token @username = username end |
Instance Attribute Details
#token ⇒ Object
Returns the value of attribute token.
4 5 6 |
# File 'lib/mad_chatter/user.rb', line 4 def token @token end |
#username ⇒ Object
Returns the value of attribute username.
4 5 6 |
# File 'lib/mad_chatter/user.rb', line 4 def username @username end |
Instance Method Details
#connected ⇒ Object
19 20 21 22 23 24 |
# File 'lib/mad_chatter/user.rb', line 19 def connected @token = generate_new_token unless @token MadChatter.users << self send_token send_channels end |
#disconnected ⇒ Object
62 63 64 65 66 |
# File 'lib/mad_chatter/user.rb', line 62 def disconnected MadChatter.channels.each do |channel| channel.remove_user(self) end end |
#generate_new_token ⇒ Object
26 27 28 |
# File 'lib/mad_chatter/user.rb', line 26 def generate_new_token Digest::SHA1.hexdigest(Time.now.to_s) end |
#has_token?(token) ⇒ Boolean
68 69 70 |
# File 'lib/mad_chatter/user.rb', line 68 def has_token?(token) @token == token end |
#on_send(&blk) ⇒ Object
11 12 13 |
# File 'lib/mad_chatter/user.rb', line 11 def on_send(&blk) @on_send = blk end |
#send(json) ⇒ Object
15 16 17 |
# File 'lib/mad_chatter/user.rb', line 15 def send(json) @on_send.call(json) if @on_send end |
#send_channels ⇒ Object
37 38 39 |
# File 'lib/mad_chatter/user.rb', line 37 def send_channels send MadChatter.channels_list end |
#send_token ⇒ Object
30 31 32 33 34 35 |
# File 'lib/mad_chatter/user.rb', line 30 def send_token send JSON.generate({ type: 'token', text: @token, }) end |
#send_users_list ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/mad_chatter/user.rb', line 54 def send_users_list MadChatter.channels.each do |channel| channel.users.each do |user| channel.send_users_list if user == self end end end |
#update_username(username) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mad_chatter/user.rb', line 41 def update_username(username) old_username = @username @username = username send_users_list MadChatter.channels.each do |channel| channel.users.each do |user| if user == self channel. MadChatter::Message.new('status', "#{old_username} is now known as #{@username}") end end end end |