Class: Campline::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



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

def initialize(options)
  @config = options
  @me = nil
  @room_users = []
  @output_buffer = ""
  @input_buffer = Queue.new
end

Instance Method Details

#backspace!Object



92
93
94
95
# File 'lib/campline.rb', line 92

def backspace!
  @output_buffer.chop!
  print "#{GO_BACK}> #{@output_buffer}"
end

#blue(str) ⇒ Object



121
122
123
# File 'lib/campline.rb', line 121

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

#commandsObject



57
58
59
60
61
62
63
64
# File 'lib/campline.rb', line 57

def commands
  {
    "/help" => lambda { print_inline(white("Available commands: /users (list users on the room), /exit (quit!), /log (show latest messages)")) },
    "/exit" => lambda { exit! },
    "/users" => lambda { list_users },
    "/log" => lambda { print_transcript }
  }
end

#exit!Object



102
103
104
105
106
# File 'lib/campline.rb', line 102

def exit!
  @campfire_room.leave
  print "\r\nGoodbye..."
  exit    
end

#flush_input_buffer!Object



108
109
110
111
112
113
114
115
# File 'lib/campline.rb', line 108

def flush_input_buffer!
  unless @input_buffer.empty? # read from server
    notify_growl!
    print GO_BACK
    print "#{@input_buffer.shift}\r\n" until @input_buffer.empty?
    show_prompt
  end      
end

#green(str) ⇒ Object



125
126
127
# File 'lib/campline.rb', line 125

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

#highlight(str) ⇒ Object



133
134
135
# File 'lib/campline.rb', line 133

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

#list_usersObject



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

def list_users
  update_user_list
  print_inline(white("In the room right now: #{@room_users.collect(&:name).join(', ')}"))
end

#listen!Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/campline.rb', line 192

def listen!
  print "Logging in...\r\n"
  begin
    params = { :ssl => true }
    if @config[:api_key]
      params[:token] = @config[:api_key]
    else
      params.merge!(:username => @config[:username], :password => @config[:password])
    end
    campfire = Campfire.new(@config[:domain], params)
  rescue Tinder::AuthenticationFailed
    raise "There was an authentication error - check your login information\r\n"
  end
  @me = campfire.me

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

  start_message_listener!(@campfire_room)
  start_typing_agent!.join
end

#notify_growl!Object



117
118
119
# File 'lib/campline.rb', line 117

def notify_growl!
  $growl.notify("ruby-growl", "ruby-growl", "Greetings!") if $growl
end


52
53
54
55
# File 'lib/campline.rb', line 52

def print_inline(msg)
  print "#{GO_BACK}#{msg}\r\n"
  show_prompt
end


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/campline.rb', line 33

def print_message(msg, flush = true, ignore_current_user = true)
  return if msg[:user].nil?

  return if (msg[:user] && msg[:user][:id] == @me[:id] && ignore_current_user)
  @input_buffer << case msg[:type]
    when "KickMessage","LeaveMessage"
      white("#{msg[:user][:name]} left the room")
    when "EnterMessage"
      white("#{msg[:user][:name]} joined the room")
    when "SoundMessage"
      "#{green(msg[:user][:name])} #{white('played some sound. Sound is for dummies.')}" 
    when "PasteMessage", "TextMessage", "TweetMessage"
      "#{green(msg[:user][:name])}: #{msg[:body]}"
    else
      msg
  end
  flush_input_buffer! if flush
end


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/campline.rb', line 71

def print_transcript
  update_user_list
  transcript = @campfire_room.transcript(Date.today) || []
  
  # load user names, as these don't come on the transcript...
  talking_users = {}
  @room_users.each do |user|
    talking_users[user.id] = user
  end

  transcript.reverse[0..15].reverse.each do |m| 
    print_message(m.merge(:user => talking_users[m[:user_id]], :type => "TextMessage", :body => m[:message]), false, false)
  end
  flush_input_buffer!
  print_inline(white("Last message received at #{transcript[-1][:timestamp]}"))
end

#send_line!Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/campline.rb', line 137

def send_line!
  buffer = @output_buffer
  @output_buffer = ""
  if commands[buffer]
    commands[buffer].call
  else
    return if buffer.blank?
    Thread.new do
      begin
        print_message({ :user => @me, :type => "TextMessage", :body => buffer }, true, false)
        @campfire_room.speak(buffer)
      rescue => e
        print_inline(white("A message could not be sent: #{buffer}"))
      end
    end
  end        
end

#show_promptObject



97
98
99
100
# File 'lib/campline.rb', line 97

def show_prompt
  print "#{GO_BACK}> #{@output_buffer}"
  $stdout.flush
end

#start_message_listener!(room) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/campline.rb', line 155

def start_message_listener!(room)
  Thread.new do
    while true
      begin
        room.listen do |msg|
          print_message(msg)
        end
      rescue => e
        # ignore errors!
        #  puts e
      end
    end
  end
end

#start_typing_agent!Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/campline.rb', line 170

def start_typing_agent!
  Thread.new do
    while character = $stdin.getc
      case character
        when ?\C-c
          exit!
        when ?\r, ?\n
          send_line!
          show_prompt
        when ?\e # arrow keys & fn keys
          # do nothing
        when ?\u007F, ?\b
          backspace!
        else
          @output_buffer << character
          print character.chr
          $stdout.flush
      end
    end
  end
end

#update_user_listObject



88
89
90
# File 'lib/campline.rb', line 88

def update_user_list
  @room_users = @campfire_room.users
end

#white(str) ⇒ Object



129
130
131
# File 'lib/campline.rb', line 129

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