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), config: nil, debug: false) ⇒ AjaxChat

Returns a new instance of AjaxChat.



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/h2g_ajaxchat.rb', line 338

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

  plugins =  if config then
  
    SimpleConfig.new(config).to_h.map do |name, settings|
      
      settings = {} if settings.is_a? String
            
      pluginklass_name = 'AjaxChatPlugin' + name.to_s
      
      Kernel.const_get(pluginklass_name).new(settings)
      
    end  
    
  else
    
    []
    
  end
  
  @h = {}
  
  @index, @login, @logout, @logout_post  = \
      [Index, Login, Logout, LogoutPost].map do |klass|
    
    obj = klass.new @h
    
    plugins.each {|plugin| plugin.apply obj }
    
    obj
    
  end
  
end

Instance Attribute Details

#rwsObject (readonly)

Returns the value of attribute rws.



336
337
338
# File 'lib/h2g_ajaxchat.rb', line 336

def rws
  @rws
end

Instance Method Details

#chatter(newmsg = nil) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/h2g_ajaxchat.rb', line 375

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



420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/h2g_ajaxchat.rb', line 420

def index()

  if @rws.req.session[:username] then
    
    @h[:username] = @rws.req.session[:username]
    view @index
    
  else
    
    @rws.redirect 'login'
    
  end

end

#loginObject



393
394
395
396
397
# File 'lib/h2g_ajaxchat.rb', line 393

def ()

  view @login

end

#login_post(username) ⇒ Object



399
400
401
402
403
404
405
# File 'lib/h2g_ajaxchat.rb', line 399

def (username)

  @chat. @rws.req, username
  @h[:username] = username
  @rws.redirect 'index'

end

#logoutObject



407
408
409
410
411
# File 'lib/h2g_ajaxchat.rb', line 407

def logout()

  view @logout

end

#logout_postObject



413
414
415
416
417
418
# File 'lib/h2g_ajaxchat.rb', line 413

def logout_post()

  @chat.logout @rws.req            
  view @logout_post 

end

#messagesObject

user for debugging



436
437
438
# File 'lib/h2g_ajaxchat.rb', line 436

def messages()
  @chat.messages
end

#req(obj) ⇒ Object



440
441
442
443
444
445
# File 'lib/h2g_ajaxchat.rb', line 440

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

#usersObject

user for debugging



448
449
450
# File 'lib/h2g_ajaxchat.rb', line 448

def users()
  @chat.users
end