Class: TChatter::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/t_chatter/entry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEntry



5
6
7
8
9
10
# File 'lib/t_chatter/entry.rb', line 5

def initialize
  @config = ConfigSetup.instance
  setup_user
  @chat = Chat.new(@user, @config)
  @threads = []
end

Instance Attribute Details

#chatObject (readonly)

Returns the value of attribute chat.



3
4
5
# File 'lib/t_chatter/entry.rb', line 3

def chat
  @chat
end

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/t_chatter/entry.rb', line 3

def config
  @config
end

#threadsObject (readonly)

Returns the value of attribute threads.



3
4
5
# File 'lib/t_chatter/entry.rb', line 3

def threads
  @threads
end

#userObject (readonly)

Returns the value of attribute user.



3
4
5
# File 'lib/t_chatter/entry.rb', line 3

def user
  @user
end

Instance Method Details

#commandsObject



74
75
76
77
78
79
80
# File 'lib/t_chatter/entry.rb', line 74

def commands
  {
    '#all_users' => :peep,
    '#help' => :help,
    '#quit' => :quit
  }
end

#control_exit_strategyObject



96
97
98
99
100
# File 'lib/t_chatter/entry.rb', line 96

def control_exit_strategy
  trap('INT') do
    quit
  end
end

#helpObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/t_chatter/entry.rb', line 57

def help
  var_help = variables_subs.keys.join(', ')
  command_help = commands.keys.join(', ')
  puts "    Use the following variables in any message to refer to its inferred\n    meaning in any message: \#{var_help}\n    You can use any of these commands to also call the inferred action\n    \#{command_help}\n  EOS\nend\n"

#peepObject



53
54
55
# File 'lib/t_chatter/entry.rb', line 53

def peep
  chat.show_all_users
end

#quitObject



68
69
70
71
72
# File 'lib/t_chatter/entry.rb', line 68

def quit
  puts "Gracefully shutting down the chat"
  chat.quit
  @threads.each(&:exit)
end

#send_messagesObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/t_chatter/entry.rb', line 82

def send_messages
  Thread.new do
    loop do
      message = gets.chomp.strip
      unless commands.keys.include? message
        message = substitute_variables(message) if message.match(/@\w+/)
        chat.send_message(message) unless message.blank?
      else
        send(commands[message])
      end
    end
  end
end

#setup_threadsObject



102
103
104
105
# File 'lib/t_chatter/entry.rb', line 102

def setup_threads
  @threads << stream_messages if want_to_receive?
  @threads << send_messages if want_to_send?
end

#setup_userObject



26
27
28
29
30
31
32
# File 'lib/t_chatter/entry.rb', line 26

def setup_user
  @user = config.configuration[:user]
  unless @user
    print 'Would you mind telling me your name? : '
    @user = gets.chomp
  end
end

#start_chatObject



12
13
14
15
16
# File 'lib/t_chatter/entry.rb', line 12

def start_chat
  setup_threads
  control_exit_strategy
  start_threads
end

#start_threadsObject



107
108
109
# File 'lib/t_chatter/entry.rb', line 107

def start_threads
  @threads.each(&:join)
end

#stream_messagesObject



34
35
36
37
38
39
40
# File 'lib/t_chatter/entry.rb', line 34

def stream_messages
  Thread.new do
    loop do
      chat.receive_message
    end
  end
end

#substitute_variables(message) ⇒ Object



46
47
48
49
50
51
# File 'lib/t_chatter/entry.rb', line 46

def substitute_variables(message)
  variables_subs.each do |var, val|
    message = message.gsub(var, val)
  end
  message
end

#variables_subsObject



42
43
44
# File 'lib/t_chatter/entry.rb', line 42

def variables_subs
  { '@my_name' => chat.user.to_s, '@last_message' => chat.messages.last.to_s, '@each_user' => chat.each_user }
end

#want_to_receive?Boolean



22
23
24
# File 'lib/t_chatter/entry.rb', line 22

def want_to_receive?
  @config.configuration.blank? ? true : @config.configuration[:receive]
end

#want_to_send?Boolean



18
19
20
# File 'lib/t_chatter/entry.rb', line 18

def want_to_send?
  @config.configuration.blank? ? true : @config.configuration[:send]
end