h2g_ajaxchat 0.1.0 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: af5575220d85e86b2c18aabfc6427cb88924328d4fc40c824038108727af5da1
4
- data.tar.gz: a39989028a432c1c46d40aaecbe42012787261ef73a3f7cd199e31fb2ecf1e30
3
+ metadata.gz: 7db5c15f7f4833080876a74afc5aa0a5076e8a08e5401c2d40a5a2b3c5082d89
4
+ data.tar.gz: 57f9b086460821188a16c3f028abc3528683afcb4efb23be2e460e1b7c84a442
5
5
  SHA512:
6
- metadata.gz: 27cbfb9a5c8c87de83bd632bd71f98035d3a5c4c666b769f736eadb8a037c53752c4be4ab3b945c1c8d3720cdebc288e6fcb660bcc48a2280d64311d15378699
7
- data.tar.gz: 4f00dd6a6f1f5e77e05ed2bc86a1043fcc0889c6663edf6b63b2dd48cacc0d92e9900901e72abf163bc8742b186aedc3e9925defc56bb21c476a1c43825ee545
6
+ metadata.gz: 1d8252c053581cc82b74299d5fb022ef064a08e1533824725b8af01fd44980bf487731b4653b30f0ed48b817a4ed4452601858af482784251f3115fc70307014
7
+ data.tar.gz: 340a2b64327b71919d839c9a860343ad55a93fa51f774090d991eec0ef4835c026beb8485f31e12cb6b00e4cf6cce58c3fdc50de0fbb8a2191aca196c8ce9d3d
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -5,6 +5,47 @@
5
5
  # description: This gem makes it easier to build an AJAX chat project.
6
6
  # Designed for Rack-rscript.
7
7
 
8
+ require 'erb'
9
+ require 'simple-config'
10
+
11
+ # This file includes the following classes
12
+ #
13
+ # * AjaxChatPlugin Base class for plugins
14
+ # * DummyRequest (used for testing)
15
+ # * ChatCore (chat engine)
16
+ # * DummyRws (used for testing)
17
+ # * WebPage
18
+ # * Index < WebPage
19
+ # * Login < WebPage
20
+ # * Logout < WebPage
21
+ # * LogoutPost < WebPage
22
+ # * AjaxChat (the main class)
23
+
24
+
25
+ class AjaxChatPlugin
26
+
27
+ # ac = ajaxchat object
28
+ #
29
+ def initialize(ac, settings={}, debug: false)
30
+ @ac, @settings, @debug = ac, settings, debug
31
+ end
32
+
33
+ # Customises the ajax chat html etc;
34
+ #
35
+ def apply()
36
+ end
37
+
38
+ # messages from the plugin to be added to the chat timeline
39
+ #
40
+ def messages()
41
+ []
42
+ end
43
+
44
+ # messages from the chat timeline
45
+ #
46
+ def on_newmessage(time, userid, username, msg)
47
+ end
48
+ end
8
49
 
9
50
  class DummyRequest
10
51
 
@@ -60,7 +101,6 @@ class ChatCore
60
101
  else
61
102
 
62
103
  return '' unless newmsg
63
- puts '_messages: ' + @messages.inspect
64
104
  @messages.length - 1
65
105
  end
66
106
 
@@ -93,15 +133,12 @@ class ChatCore
93
133
 
94
134
  end
95
135
 
96
- protected
97
136
 
98
- def append_message(msg)
99
-
100
- u = @session[:username]
101
- id = @session[:session_id].to_s
102
- @users[id] = u.to_s
103
-
104
- @messages << [Time.now, id, u, msg]
137
+ def append_message(t=Time.now, id=@session[:session_id].to_s,
138
+ u=@session[:username], msg)
139
+
140
+ @users[id] = u.to_s
141
+ @messages << [t, id, u, msg]
105
142
 
106
143
  end
107
144
 
@@ -109,22 +146,25 @@ end
109
146
 
110
147
  class DummyRws
111
148
 
112
- attr_accessor :req
149
+ attr_accessor :req, :ac
150
+ attr_reader :rsc
113
151
 
114
- def initialize(obj)
152
+ def initialize(ac=nil, rsc: nil)
115
153
 
116
- @obj = obj
154
+ @ac, @rsc = ac, rsc
117
155
 
118
156
  end
119
157
 
120
158
  def redirect(s)
121
- @obj.method(s.to_sym).call
159
+ @ac.method(s.to_sym).call
122
160
  end
123
161
  end
124
162
 
125
163
  class WebPage
164
+
165
+ attr_accessor :css, :html, :js, :s
126
166
 
127
- def initialize(h={})
167
+ def initialize( h={} )
128
168
  @h = h
129
169
  end
130
170
 
@@ -132,13 +172,21 @@ class WebPage
132
172
  end
133
173
 
134
174
  def to_html()
175
+ b = binding
176
+
177
+ ERB.new(html()).result(b)
135
178
  end
136
179
 
137
180
  def to_js()
181
+ js()
138
182
  end
139
183
 
184
+ def s()
185
+ @s ||= html_template()
186
+ end
187
+
140
188
  def to_s()
141
- html_template()
189
+ s()
142
190
  end
143
191
 
144
192
  protected
@@ -166,146 +214,20 @@ end
166
214
 
167
215
 
168
216
 
169
- class AjaxChat
170
-
171
- attr_reader :rws
172
-
173
- def initialize(chatobj, rws=DummyRws.new(self), debug: false)
174
- @chat, @rws, @debug = chatobj, rws, debug
175
- end
176
-
177
- def chatter(newmsg=nil)
178
-
179
- id, users = @rws.req.session[:session_id].to_s, @chat.users
180
-
181
- @chat.chatter(@rws.req, newmsg) do |t, uid, username, msg|
182
-
183
- s2 = if id == uid then
184
- "you: %s" % msg
185
- else
186
- "%s: %s" % [users[uid], msg]
187
- end
188
-
189
- "<p><span id='time'>%s</span> %s</p>" % [t.strftime("%H:%M:%S"), s2]
190
-
191
- end
192
-
193
-
194
- end
195
-
196
- def login()
197
-
198
- wp = WebPage.new
199
-
200
- def wp.to_html()
201
- '
202
- <div id="loginform">
203
- <form action="login" method="post">
204
- <p>Please enter your name to continue:</p>
205
- <label for="name">Name:</label>
206
- <input type="text" name="name" id="name" autofocus="true"/>
207
- <input type="submit" name="enter" id="enter" value="Enter" />
208
- </form>
209
- </div>
210
- '
211
-
212
- end
213
-
214
- def wp.to_s()
215
- to_html()
216
- end
217
-
218
- return wp
219
-
220
- end
221
-
222
- def login_post(username)
223
-
224
- @chat.login @rws.req, username
225
- @rws.redirect 'index'
226
-
227
- end
228
-
229
- def logout()
230
-
231
- wp = WebPage.new
232
-
233
- def wp.to_html()
234
- '
235
- <div id="logoutform">
236
- <form action="logout" method="post">
237
- <p>Are you sure you want to logout?</p>
238
- <input type="submit" name="enter" id="enter" value="Yes" />
239
- </form>
240
- <a href="index">no, return to the chat page</a>
241
- </div>
242
- '
243
-
244
- end
245
-
246
- def wp.to_s()
247
- to_html()
248
- end
249
-
250
- return wp
251
217
 
252
- end
218
+ class Index < WebPage
253
219
 
254
- def logout_post()
255
-
256
- @chat.logout @rws.req
257
-
258
- wp = WebPage.new
259
-
260
- def wp.to_s()
261
- 'You have successfully logged out'
262
- end
263
-
264
- return wp
265
-
266
- end
267
-
268
- def index()
269
-
270
- @rws.req.session[:username] ? view_index() : login()
271
-
272
- end
273
-
274
- def messages()
275
- @chat.messages
276
- end
277
-
278
- def req(obj)
279
- @rws.req = obj
280
- self
220
+ def initialize(h)
221
+ @h = h
281
222
  end
282
223
 
283
- def users()
284
- @chat.users
285
- end
286
-
287
- private
288
-
289
- def view_index()
290
-
291
- h = {username: @rws.req.session[:username]}
292
-
293
- wp = WebPage.new h
294
-
295
- def wp.to_css()
296
- '
297
- body {font-family: Arial;}
298
- #chatbox {overflow: scroll; height: 40%}
299
- div p span {colour: #dde}
300
- '
301
- end
302
-
303
- def wp.to_html()
304
- <<EOF
224
+ def html()
225
+
226
+ @html ||= <<EOF
305
227
  <body onload="refresh()">
306
228
  <div id="wrapper">
307
229
  <div id="menu">
308
- <p class="welcome">Welcome, <b> #{@h[:username]} </b></p>
230
+ <p class="welcome">Welcome, <b> <%= @h[:username] %> </b></p>
309
231
  <p class="logout"><a id="exit" href="logout">Exit Chat</a></p>
310
232
  <div style="clear:both"></div>
311
233
  </div>
@@ -313,12 +235,26 @@ div p span {colour: #dde}
313
235
  <input name="usermsg" type="text" id="usermsg" size="33" onkeyup='ajaxCall1(event.keyCode, this)' autofocus='true'/>
314
236
 
315
237
  </div>
316
-
317
238
  EOF
318
- end
319
239
 
320
- def wp.to_js()
321
- <<EOF
240
+ end
241
+
242
+ def to_css()
243
+ @css ||= '
244
+ body {font-family: Arial;}
245
+ #chatbox {overflow: scroll; height: 40%}
246
+ div p span {colour: #dde}
247
+ '
248
+ end
249
+
250
+ def to_html()
251
+
252
+ super()
253
+ end
254
+
255
+ def js()
256
+
257
+ @js ||= <<EOF
322
258
 
323
259
  function updateScroll(){
324
260
  var element = document.getElementById("chatbox");
@@ -365,12 +301,230 @@ function ajaxCall2() {
365
301
 
366
302
  EOF
367
303
 
304
+ end
368
305
 
369
- end
306
+
307
+ end
308
+
309
+ class Login < WebPage
310
+
311
+ def initialize(h={})
370
312
 
371
- return wp
313
+ end
314
+
315
+ def html()
316
+
317
+ @html ||= '
318
+ <div id="loginform">
319
+ <form action="login" method="post">
320
+ <p>Please enter your name to continue:</p>
321
+ <label for="name">Name:</label>
322
+ <input type="text" name="name" id="name" autofocus="true"/>
323
+ <input type="submit" name="enter" id="enter" value="Enter" />
324
+ </form>
325
+ </div>
326
+ '
372
327
 
373
328
  end
329
+
330
+
331
+ end
374
332
 
333
+ class Logout < WebPage
334
+
335
+ def initialize(h={})
336
+
337
+ end
338
+
339
+ def html()
340
+
341
+ @html ||= '
342
+ <div id="logoutform">
343
+ <form action="logout" method="post">
344
+ <p>Are you sure you want to logout?</p>
345
+ <input type="submit" name="enter" id="enter" value="Yes" />
346
+ </form>
347
+ <a href="index">no, return to the chat page</a>
348
+ </div>
349
+ '
350
+
351
+ end
352
+
353
+
354
+ end
355
+
356
+ class LogoutPost < WebPage
357
+
358
+ def initialize(h={})
359
+
360
+ end
361
+
362
+ def s()
363
+
364
+ @s ||= 'You have successfully logged out'
365
+
366
+ end
367
+
368
+
375
369
  end
376
370
 
371
+
372
+ class AjaxChat
373
+
374
+ attr_reader :rws, :views
375
+
376
+ def initialize(chatobj=ChatCore.new, rws: DummyRws.new(self), config: nil,
377
+ debug: false)
378
+
379
+ @chat, @rws, @debug = chatobj, rws, debug
380
+
381
+ @plugins = if config then
382
+
383
+ SimpleConfig.new(config).to_h.map do |name, settings|
384
+
385
+ puts 'name: ' + name.inspect if @debug
386
+ settings = {} if settings.is_a? String
387
+
388
+ next if settings[:disabled]
389
+ pluginklass_name = 'AjaxChatPlugin' + name.to_s
390
+
391
+ #Kernel.const_get(pluginklass_name).new(self, settings, debug: false)
392
+ eval("#{pluginklass_name}.new(self, #{settings}, debug: false)")
393
+
394
+ end.compact
395
+
396
+ else
397
+
398
+ []
399
+
400
+ end
401
+
402
+ @h = {}
403
+
404
+ @index, @login, @logout, @logout_post = \
405
+ [Index, Login, Logout, LogoutPost].map do |klass|
406
+
407
+ klass.new @h
408
+
409
+ end
410
+
411
+ @views = {
412
+ index: @index,
413
+ login: @login,
414
+ logout: @logout,
415
+ logout_post: @logout_post
416
+ }
417
+
418
+ @plugins.each {|plugin| plugin.apply if plugin.respond_to? :apply }
419
+
420
+ end
421
+
422
+ def chatter(newmsg=nil)
423
+
424
+ id, users = @rws.req.session[:session_id].to_s, @chat.users
425
+
426
+ chat = @chat
427
+
428
+ # check for new messages to be added via the plugins
429
+ @plugins.each do |plugin|
430
+
431
+ next unless plugin.respond_to? :messages
432
+ plugin.messages.each {|x| chat.append_message *x }
433
+
434
+ end
435
+
436
+ plugins = @plugins
437
+
438
+ @chat.chatter(@rws.req, newmsg) do |t, uid, username, msg|
439
+
440
+ plugins.each do |x|
441
+
442
+ x.on_newmessage(t, uid, username, msg) if x.respond_to? :on_newmessage
443
+
444
+ end
445
+
446
+ s2 = if id == uid then
447
+ "you: %s" % msg
448
+ else
449
+ "%s: %s" % [users[uid], msg]
450
+ end
451
+
452
+ "<p><span id='time'>%s</span> %s</p>" % [t.strftime("%H:%M:%S"), s2]
453
+
454
+ end
455
+
456
+ end
457
+
458
+ def login()
459
+
460
+ view @login
461
+
462
+ end
463
+
464
+ def login_post(username)
465
+
466
+ @chat.login @rws.req, username
467
+ @h[:username] = username
468
+ @rws.redirect 'index'
469
+
470
+ end
471
+
472
+ def logout()
473
+
474
+ view @logout
475
+
476
+ end
477
+
478
+ def logout_post()
479
+
480
+ @chat.logout @rws.req
481
+ view @logout_post
482
+
483
+ end
484
+
485
+ def index()
486
+
487
+ if @rws.req.session[:username] then
488
+
489
+ @h[:username] = @rws.req.session[:username]
490
+ view @index
491
+
492
+ else
493
+
494
+ @rws.redirect 'login'
495
+
496
+ end
497
+
498
+ end
499
+
500
+ # used for debugging
501
+ def messages()
502
+ @chat.messages
503
+ end
504
+
505
+ def req(obj)
506
+
507
+ @rws.req = obj
508
+ self
509
+
510
+ end
511
+
512
+ # used for debugging
513
+ def users()
514
+ @chat.users
515
+ end
516
+
517
+ private
518
+
519
+ def view(obj)
520
+
521
+ puts 'inside view obj: ' + obj.inspect if @debug
522
+
523
+ if obj.to_s.length > 0 then
524
+ obj.to_s
525
+ else
526
+ obj
527
+ end
528
+ end
529
+
530
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: h2g_ajaxchat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -35,8 +35,28 @@ cert_chain:
35
35
  PrknRa5wCOkWn6MNeaZ10MuRCv0E4IRGb8q/2WczGDXI3lmwDWOzPzia3/A32vdt
36
36
  hlwWFeE0gWND9J9tivS5qxBI
37
37
  -----END CERTIFICATE-----
38
- date: 2020-07-04 00:00:00.000000000 Z
39
- dependencies: []
38
+ date: 2020-07-06 00:00:00.000000000 Z
39
+ dependencies:
40
+ - !ruby/object:Gem::Dependency
41
+ name: simple-config
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 0.7.0
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0.7'
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 0.7.0
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '0.7'
40
60
  description:
41
61
  email: james@jamesrobertson.eu
42
62
  executables: []
metadata.gz.sig CHANGED
Binary file