Class: ChatCore
- Inherits:
-
Object
- Object
- ChatCore
- Defined in:
- lib/h2g_ajaxchat.rb
Instance Attribute Summary collapse
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#users ⇒ Object
readonly
Returns the value of attribute users.
Instance Method Summary collapse
- #append_message(t = Time.now, id = @session[:session_id].to_s, u = , msg) ⇒ Object
- #chatter(req, newmsg = nil) ⇒ Object
-
#initialize(debug: false) ⇒ ChatCore
constructor
A new instance of ChatCore.
- #login(req, username) ⇒ Object
- #logout(req) ⇒ Object
Constructor Details
#initialize(debug: false) ⇒ ChatCore
Returns a new instance of ChatCore.
70 71 72 73 74 75 76 |
# File 'lib/h2g_ajaxchat.rb', line 70 def initialize(debug: false) @debug = debug @messages = [] @count = 0 @users = {} end |
Instance Attribute Details
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
68 69 70 |
# File 'lib/h2g_ajaxchat.rb', line 68 def @messages end |
#users ⇒ Object (readonly)
Returns the value of attribute users.
68 69 70 |
# File 'lib/h2g_ajaxchat.rb', line 68 def users @users end |
Instance Method Details
#append_message(t = Time.now, id = @session[:session_id].to_s, u = , msg) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/h2g_ajaxchat.rb', line 137 def (t=Time.now, id=@session[:session_id].to_s, u=@session[:username], msg) @users[id] = u.to_s @messages << [t, id, u, msg] end |
#chatter(req, newmsg = nil) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/h2g_ajaxchat.rb', line 86 def chatter(req, newmsg=nil) @session = req.session (newmsg) if newmsg return '' if @messages.empty? c = @session[:last_id] pos = if c then last_msg = @messages.find {|x| x.object_id.to_i == c} last_msg ? @messages.index(last_msg) + 1 : @messages.length - 1 else return '' unless newmsg @messages.length - 1 end if @debug then puts 'pos: ' + pos.inspect puts '@messages: ' + @messages.inspect end @session[:last_id] = @messages.last.object_id.to_i a = @messages[pos..-1].map do |t, id, u, msg| if block_given? then yield(t, id, u, msg) else s = "user%s: %s" % [id, msg] [t.strftime("%H:%M:%S"), s].join(' ') end end a.join("\n") end |
#login(req, username) ⇒ Object
78 79 80 |
# File 'lib/h2g_ajaxchat.rb', line 78 def login(req, username) req.session[:username] = username end |
#logout(req) ⇒ Object
82 83 84 |
# File 'lib/h2g_ajaxchat.rb', line 82 def logout(req) req.session.clear end |