Class: Campline::Client

Inherits:
Object
  • Object
show all
Includes:
CLIColorize, Tinder
Defined in:
lib/campline.rb

Instance Method Summary collapse

Constructor Details

#initialize(domain, room, username, password) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
21
22
23
# File 'lib/campline.rb', line 14

def initialize(domain, room, username, password)
  @domain = domain
  @room = room
  @username = username
  @password = password
  @user_id = nil
  @room_users = []
  @output_buffer = ""
  @input_buffer = Queue.new
end

Instance Method Details

#backspace!Object



61
62
63
64
65
# File 'lib/campline.rb', line 61

def backspace!
  @output_buffer.chop!
  go_back = "\r\e[0K" # return to beginning of line and use the ANSI clear command "\e" or "\003"
  print "#{go_back}> #{@output_buffer}"
end

#blue(str) ⇒ Object



73
74
75
# File 'lib/campline.rb', line 73

def blue(str)
  colorize(str, :foreground => :blue)
end

#commandsObject



37
38
39
40
41
42
43
# File 'lib/campline.rb', line 37

def commands
  {
    "/help" => lambda { print "\r\nAvailable commands: /users (list users on the room), /exit (quit!)"},
    "/exit" => lambda { @campfire_room.leave; exit; },
    "/users" => lambda { list_users }
  }
end

#green(str) ⇒ Object



77
78
79
# File 'lib/campline.rb', line 77

def green(str)
  colorize(str, :foreground => :green)
end

#highlight(str) ⇒ Object



85
86
87
# File 'lib/campline.rb', line 85

def highlight(str)
  colorize(str, :background => :red)
end

#list_usersObject



45
46
47
# File 'lib/campline.rb', line 45

def list_users
  print white("\r\nIn the room right now: #{@room_users.join(', ')}")
end

#listen!Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/campline.rb', line 98

def listen!
  puts "Logging in...\r\n"
  begin
    campfire = Campfire.new @domain, :username => @username, :password => @password, :ssl => true
  rescue Tinder::AuthenticationFailed
    raise "There was an authentication error - check your username and password\r\n"
  end
  @user_id = campfire.me.id

  puts "Joining #{@room}...\r\n"
  @campfire_room = campfire.find_room_by_name @room
  raise "Can't find room named #{@room}!\r\n" if @campfire_room.nil?
  
  @campfire_room.join
  update_user_list
  
  puts "You're up! For a list of available commands, type #{highlight('/help')}\r\n"

  Thread.new(@campfire_room) do |listener|
    while true
      listener.listen do |msg|
        print_message msg
      end
    end
  end

  Thread.new do
    while true
      sleep(10)
      update_user_list
    end
  end

  Thread.new do
    while true #msg = Readline.readline('> ', true)
      if $stdin.ready?
        character = $stdin.getc
        case character
          when ?\C-c
            break
          when ?\r, ?\n
            send_line!
            show_prompt
          when ?\u007F, ?\b
            backspace!
          else
            @output_buffer << character
            print character.chr
            $stdout.flush
        end
      end

      unless @input_buffer.empty?   # read from server
        puts "\r\n"
        puts "#{@input_buffer.shift}\r\n" until @input_buffer.empty?
        show_prompt
      end

    end
  end.join
end


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/campline.rb', line 25

def print_message(msg)
  return if (msg[:user] && msg[:user][:id] == @user_id)
  case msg[:type]
    when "SoundMessage" then
      @input_buffer << "#{green(msg[:user][:name])} played some annoying sound" 
    when "PasteMessage" then
      @input_buffer << "#{green(msg[:user][:name])}: #{msg[:body]}"
    when "TextMessage" then
      @input_buffer << "#{green(msg[:user][:name])}: #{msg[:body]}"
  end
end

#send_line!Object



89
90
91
92
93
94
95
96
# File 'lib/campline.rb', line 89

def send_line!
  if commands[@output_buffer]
    commands[@output_buffer].call
  else
    @campfire_room.speak @output_buffer
  end
  @output_buffer = ""
end

#show_promptObject



67
68
69
70
71
# File 'lib/campline.rb', line 67

def show_prompt
  puts "\r\n"
  print "> #{@output_buffer}"
  $stdout.flush
end

#update_user_listObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/campline.rb', line 49

def update_user_list
  new_list = @campfire_room.users.collect(&:name)
  @room_users = new_list if @room_users.empty?

  new_guys = new_list - @room_users
  new_guys.each { |n| @input_buffer << "#{n} joined the room" }

  dead_guys = @room_users - new_list
  dead_guys.each { |n| @input_buffer << "#{n} left the room" }
  @room_users = new_list
end

#white(str) ⇒ Object



81
82
83
# File 'lib/campline.rb', line 81

def white(str)
  colorize(str, :foreground => :white)
end