Module: Groupme::Cli

Defined in:
lib/groupme/cli.rb,
lib/groupme/cli/message.rb,
lib/groupme/cli/session.rb,
lib/groupme/cli/version.rb

Defined Under Namespace

Classes: Message, Session

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.run(token) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/groupme/cli.rb', line 8

def self.run token
  puts "Welcome to the GroupMe CLI v#{VERSION}"
  puts "Start by entering '%open chat_name', where chat_name is the name of the chat you want to open."

  sesh = Session.new token

  begin
    # For status messages coming from the input thread
    status_buffer = Array.new

    # input loop/thread
    input_thread = Thread.new do
      loop do
        input = gets
        if input[0..6] == "%image "
          sesh.send_msg("", input[7..-1].strip)
        elsif input.strip == "%groups"
          status_buffer << "Your groups:\n"

          sesh.get_groups.each do |group|
            status_buffer << group["name"]
          end
        elsif input[0..4] == "%quit"
          exit
        elsif input[0..5] == "%open "
          name = input[6..-1].chomp

          if sesh.open_chat(name)
            status_buffer << "<Opened #{name}>"
          else
            status_buffer << "That group does not exist. Check the name you entered to make sure it's correct."
          end
        else
          sesh.send_msg input
        end
      end
    end

    # output loop
    loop do
      # check for signals and catch them in order to exit gracefully
      Signal.trap("INT") { exit }
      Signal.trap("TERM") { exit }

      status_buffer.each { |m| puts m }
      status_buffer.clear

      if sesh.chat_is_open?
        # fetch new messages
        sesh.update_messages
        msgs = sesh.new_messages
        msgs.each {|m| puts m}
      end
    end

  rescue SystemExit
    puts "Goodbye"
  rescue Exception => e
    puts "Something went horribly wrong! See ya later."
    puts "Exception details: #{e}"
  end

end