Class: AjaxChat

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chatobj, rws = DummyRws.new(self), debug: false) ⇒ AjaxChat

Returns a new instance of AjaxChat.



173
174
175
# File 'lib/h2g_ajaxchat.rb', line 173

def initialize(chatobj, rws=DummyRws.new(self), debug: false)
  @chat, @rws, @debug = chatobj, rws, debug
end

Instance Attribute Details

#rwsObject (readonly)

Returns the value of attribute rws.



171
172
173
# File 'lib/h2g_ajaxchat.rb', line 171

def rws
  @rws
end

Instance Method Details

#chatter(newmsg = nil) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/h2g_ajaxchat.rb', line 177

def chatter(newmsg=nil)
  
  id, users = @rws.req.session[:session_id].to_s, @chat.users

  @chat.chatter(@rws.req, newmsg) do  |t, uid, username, msg|
        
    s2 = if id == uid then
      "you: %s" % msg
    else
      "%s: %s" % [users[uid], msg]
    end
    
    "<p><span id='time'>%s</span> %s</p>" % [t.strftime("%H:%M:%S"), s2]
    
  end        
  
  
end

#indexObject



268
269
270
271
272
# File 'lib/h2g_ajaxchat.rb', line 268

def index()

  @rws.req.session[:username] ? view_index() : ()

end

#loginObject



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

def ()

  wp = WebPage.new

  def wp.to_html()
'
  <div id="loginform">
  <form action="login" method="post">
<p>Please enter your name to continue:</p>
<label for="name">Name:</label>
<input type="text" name="name" id="name" autofocus="true"/>
<input type="submit" name="enter" id="enter" value="Enter" />
  </form>
  </div>
'

  end

  def wp.to_s()
    to_html()
  end

  return wp

end

#login_post(username) ⇒ Object



222
223
224
225
226
227
# File 'lib/h2g_ajaxchat.rb', line 222

def (username)

  @chat. @rws.req, username
  @rws.redirect 'index'

end

#logoutObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/h2g_ajaxchat.rb', line 229

def logout()

  wp = WebPage.new

  def wp.to_html()
'
  <div id="logoutform">
  <form action="logout" method="post">
<p>Are you sure you want to logout?</p>
<input type="submit" name="enter" id="enter" value="Yes" />
  </form>
  <a href="index">no, return to the chat page</a>
  </div>
  '

  end

  def wp.to_s()
    to_html()
  end

  return wp

end

#logout_postObject



254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/h2g_ajaxchat.rb', line 254

def logout_post()

  @chat.logout @rws.req
          
  wp = WebPage.new

  def wp.to_s()
    'You have successfully logged out'
  end

  return wp    

end

#messagesObject



274
275
276
# File 'lib/h2g_ajaxchat.rb', line 274

def messages()
  @chat.messages
end

#req(obj) ⇒ Object



278
279
280
281
# File 'lib/h2g_ajaxchat.rb', line 278

def req(obj)
  @rws.req = obj
  self
end

#usersObject



283
284
285
# File 'lib/h2g_ajaxchat.rb', line 283

def users()
  @chat.users
end