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 = ChatCore.new, rws: DummyRws.new(self), config: nil, debug: false) ⇒ AjaxChat

Returns a new instance of AjaxChat.



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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/h2g_ajaxchat.rb', line 376

def initialize(chatobj=ChatCore.new, 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
            
      next if settings[:disabled]
      pluginklass_name = 'AjaxChatPlugin' + name.to_s
      
      #Kernel.const_get(pluginklass_name).new(self, settings, debug: false)
      eval("#{pluginklass_name}.new(self, #{settings}, debug: false)")
      
    end.compact  
    
  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 if plugin.respond_to? :apply }
  
end

Instance Attribute Details

#rwsObject (readonly)

Returns the value of attribute rws.



374
375
376
# File 'lib/h2g_ajaxchat.rb', line 374

def rws
  @rws
end

#viewsObject (readonly)

Returns the value of attribute views.



374
375
376
# File 'lib/h2g_ajaxchat.rb', line 374

def views
  @views
end

Instance Method Details

#chatter(newmsg = nil) ⇒ Object



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/h2g_ajaxchat.rb', line 422

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    
  
  plugins = @plugins

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



485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/h2g_ajaxchat.rb', line 485

def index()

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

end

#loginObject



458
459
460
461
462
# File 'lib/h2g_ajaxchat.rb', line 458

def ()

  view @login

end

#login_post(username) ⇒ Object



464
465
466
467
468
469
470
# File 'lib/h2g_ajaxchat.rb', line 464

def (username)

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

end

#logoutObject



472
473
474
475
476
# File 'lib/h2g_ajaxchat.rb', line 472

def logout()

  view @logout

end

#logout_postObject



478
479
480
481
482
483
# File 'lib/h2g_ajaxchat.rb', line 478

def logout_post()

  @chat.logout @rws.req            
  view @logout_post 

end

#messagesObject

used for debugging



501
502
503
# File 'lib/h2g_ajaxchat.rb', line 501

def messages()
  @chat.messages
end

#req(obj) ⇒ Object



505
506
507
508
509
510
# File 'lib/h2g_ajaxchat.rb', line 505

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

#usersObject

used for debugging



513
514
515
# File 'lib/h2g_ajaxchat.rb', line 513

def users()
  @chat.users
end