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.



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/h2g_ajaxchat.rb', line 361

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|
      puts 'name: ' + name.inspect if @debug
      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|
    
    klass.new @h
    
  end
  
  @views = {
    index: @index,
    login: @login,
    logout: @logout,
    logout_post: @logout_post
  }
  
  plugins.each {|plugin| plugin.apply(self) if plugin.respond_to? :apply }
  
end

Instance Attribute Details

#rwsObject (readonly)

Returns the value of attribute rws.



359
360
361
# File 'lib/h2g_ajaxchat.rb', line 359

def rws
  @rws
end

#viewsObject (readonly)

Returns the value of attribute views.



359
360
361
# File 'lib/h2g_ajaxchat.rb', line 359

def views
  @views
end

Instance Method Details

#chatter(newmsg = nil) ⇒ Object



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/h2g_ajaxchat.rb', line 403

def chatter(newmsg=nil)
  
  id, users = @rws.req.session[:session_id].to_s, @chat.users
  
  chat = @chat
  
  # check for new messages to be added via the plugins
  @plugins.each do |plugin|
    
    next unless plugin.respond_to? :messages
    plugin.messages.each {|x| chat.append_message *x }
    
  end    

  @chat.chatter(@rws.req, newmsg) do  |t, uid, username, msg|
          
    @plugins.each do |plugin|
      
      plugin.messages.each do |x|
        
        x.on_newmesage(t, uid, username, msg) if x.respond_to? :on_newmessage
        
      end
      
    end   
        
    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



468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/h2g_ajaxchat.rb', line 468

def index()

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

end

#loginObject



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

def ()

  view @login

end

#login_post(username) ⇒ Object



447
448
449
450
451
452
453
# File 'lib/h2g_ajaxchat.rb', line 447

def (username)

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

end

#logoutObject



455
456
457
458
459
# File 'lib/h2g_ajaxchat.rb', line 455

def logout()

  view @logout

end

#logout_postObject



461
462
463
464
465
466
# File 'lib/h2g_ajaxchat.rb', line 461

def logout_post()

  @chat.logout @rws.req            
  view @logout_post 

end

#messagesObject

user for debugging



484
485
486
# File 'lib/h2g_ajaxchat.rb', line 484

def messages()
  @chat.messages
end

#req(obj) ⇒ Object



488
489
490
491
492
493
# File 'lib/h2g_ajaxchat.rb', line 488

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

#usersObject

user for debugging



496
497
498
# File 'lib/h2g_ajaxchat.rb', line 496

def users()
  @chat.users
end