Class: MadChatter::User

Inherits:
Object
  • Object
show all
Defined in:
lib/mad_chatter/user.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#tokenObject

Returns the value of attribute token.



4
5
6
# File 'lib/mad_chatter/user.rb', line 4

def token
  @token
end

#usernameObject

Returns the value of attribute username.



4
5
6
# File 'lib/mad_chatter/user.rb', line 4

def username
  @username
end

Instance Method Details

#connectedObject



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

#disconnectedObject



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_tokenObject



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

Returns:

  • (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_channelsObject



37
38
39
# File 'lib/mad_chatter/user.rb', line 37

def send_channels
  send MadChatter.channels_list
end

#send_tokenObject



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_listObject



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.send_message MadChatter::Message.new('status', "#{old_username} is now known as #{@username}")
      end
    end
  end
end