h2g_ajaxchat 0.1.0 → 0.2.0

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: f85b352a4b0b8983a25914a435145f0721e000e16b40c7bdfc815f8160ac8e81
4
+ data.tar.gz: 43e914e361c7c26c8ab9c9ecbed796c98adc8dffb9d835ede597afb9a0eaf2fa
5
5
  SHA512:
6
- metadata.gz: 27cbfb9a5c8c87de83bd632bd71f98035d3a5c4c666b769f736eadb8a037c53752c4be4ab3b945c1c8d3720cdebc288e6fcb660bcc48a2280d64311d15378699
7
- data.tar.gz: 4f00dd6a6f1f5e77e05ed2bc86a1043fcc0889c6663edf6b63b2dd48cacc0d92e9900901e72abf163bc8742b186aedc3e9925defc56bb21c476a1c43825ee545
6
+ metadata.gz: 90224175128a2631c1b7f37c159e6afb1302258ecc79842b6601ec3f2e5b4a41f903023b4709474f4cf43b65e75c67f4ad846e6fdd7fc0e7da61e2cadb73f12d
7
+ data.tar.gz: 07af2f963551f55c683797c3ba59eefde49a3220ce28c9690a44b475e29242e6f32bca392fae02a3b113044f98b0892cb8ed02d678eb3070b0372fb42f6f5778
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -5,6 +5,20 @@
5
5
  # description: This gem makes it easier to build an AJAX chat project.
6
6
  # Designed for Rack-rscript.
7
7
 
8
+ require 'simple-config'
9
+
10
+ # This file includes the following classes
11
+ #
12
+ # * DummyRequest (used for testing)
13
+ # * ChatCore
14
+ # * DummyRws (used for testing)
15
+ # * WebPage
16
+ # * Index < WebPage
17
+ # * Login < WebPage
18
+ # * Logout < WebPage
19
+ # * LogoutPost < WebPage
20
+ # * AjaxChat
21
+
8
22
 
9
23
  class DummyRequest
10
24
 
@@ -60,7 +74,6 @@ class ChatCore
60
74
  else
61
75
 
62
76
  return '' unless newmsg
63
- puts '_messages: ' + @messages.inspect
64
77
  @messages.length - 1
65
78
  end
66
79
 
@@ -123,9 +136,11 @@ class DummyRws
123
136
  end
124
137
 
125
138
  class WebPage
139
+
140
+ attr_accessor :css, :html, :js, :s
126
141
 
127
- def initialize(h={})
128
- @h = h
142
+ def initialize(name, h={} )
143
+ @h, @name = h, name
129
144
  end
130
145
 
131
146
  def to_css()
@@ -138,7 +153,7 @@ class WebPage
138
153
  end
139
154
 
140
155
  def to_s()
141
- html_template()
156
+ @s = html_template()
142
157
  end
143
158
 
144
159
  protected
@@ -166,142 +181,24 @@ end
166
181
 
167
182
 
168
183
 
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
184
 
252
- end
185
+ class Index < WebPage
253
186
 
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
-
187
+ def initialize(h)
188
+ @name, @h = :index, h
272
189
  end
273
190
 
274
- def messages()
275
- @chat.messages
276
- end
277
-
278
- def req(obj)
279
- @rws.req = obj
280
- self
281
- end
282
-
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
- '
191
+ def to_css()
192
+ @css ||= '
297
193
  body {font-family: Arial;}
298
194
  #chatbox {overflow: scroll; height: 40%}
299
195
  div p span {colour: #dde}
300
196
  '
301
- end
197
+ end
302
198
 
303
- def wp.to_html()
304
- <<EOF
199
+ def to_html()
200
+
201
+ @html ||= <<EOF
305
202
  <body onload="refresh()">
306
203
  <div id="wrapper">
307
204
  <div id="menu">
@@ -313,12 +210,13 @@ div p span {colour: #dde}
313
210
  <input name="usermsg" type="text" id="usermsg" size="33" onkeyup='ajaxCall1(event.keyCode, this)' autofocus='true'/>
314
211
 
315
212
  </div>
316
-
317
213
  EOF
318
- end
319
214
 
320
- def wp.to_js()
321
- <<EOF
215
+ end
216
+
217
+ def to_js()
218
+
219
+ @js ||= <<EOF
322
220
 
323
221
  function updateScroll(){
324
222
  var element = document.getElementById("chatbox");
@@ -365,12 +263,194 @@ function ajaxCall2() {
365
263
 
366
264
  EOF
367
265
 
266
+ end
267
+
268
+ end
368
269
 
369
- end
270
+ class Login < WebPage
271
+
272
+ def initialize(h)
273
+ @name = :login
274
+ end
370
275
 
371
- return wp
276
+ def to_html()
372
277
 
278
+ @html ||= '
279
+ <div id="loginform">
280
+ <form action="login" method="post">
281
+ <p>Please enter your name to continue:</p>
282
+ <label for="name">Name:</label>
283
+ <input type="text" name="name" id="name" autofocus="true"/>
284
+ <input type="submit" name="enter" id="enter" value="Enter" />
285
+ </form>
286
+ </div>
287
+ '
288
+
289
+ end
290
+
291
+ end
292
+
293
+ class Logout < WebPage
294
+
295
+ def initialize(h)
296
+ @name = :logout
373
297
  end
374
298
 
299
+ def to_html()
300
+
301
+ @html ||= '
302
+ <div id="logoutform">
303
+ <form action="logout" method="post">
304
+ <p>Are you sure you want to logout?</p>
305
+ <input type="submit" name="enter" id="enter" value="Yes" />
306
+ </form>
307
+ <a href="index">no, return to the chat page</a>
308
+ </div>
309
+ '
310
+
311
+ end
312
+
375
313
  end
376
314
 
315
+ class LogoutPost < WebPage
316
+
317
+ def initialize(h)
318
+ @name = :logout_post
319
+ end
320
+
321
+ def to_s()
322
+
323
+ @s ||= 'You have successfully logged out'
324
+
325
+ end
326
+
327
+ end
328
+
329
+
330
+ class AjaxChat
331
+
332
+ attr_reader :rws
333
+
334
+ def initialize(chatobj, rws=DummyRws.new(self), config: nil, debug: false)
335
+
336
+ @chat, @rws, @debug = chatobj, rws, debug
337
+
338
+ plugins = if config then
339
+
340
+ SimpleConfig.new(config).to_h.map do |name, settings|
341
+
342
+ settings = {} if settings.is_a? String
343
+
344
+ pluginklass_name = 'AjaxChatPlugin' + name.to_s
345
+
346
+ Kernel.const_get(pluginklass_name).new(settings)
347
+
348
+ end
349
+
350
+ else
351
+
352
+ []
353
+
354
+ end
355
+
356
+ @h = {}
357
+
358
+ @index, @login, @logout, @logout_post = \
359
+ [Index, Login, Logout, LogoutPost].map do |klass|
360
+
361
+ obj = klass.new @h
362
+
363
+ plugins.each {|plugin| plugin.apply obj }
364
+
365
+ obj
366
+
367
+ end
368
+
369
+ end
370
+
371
+ def chatter(newmsg=nil)
372
+
373
+ id, users = @rws.req.session[:session_id].to_s, @chat.users
374
+
375
+ @chat.chatter(@rws.req, newmsg) do |t, uid, username, msg|
376
+
377
+ s2 = if id == uid then
378
+ "you: %s" % msg
379
+ else
380
+ "%s: %s" % [users[uid], msg]
381
+ end
382
+
383
+ "<p><span id='time'>%s</span> %s</p>" % [t.strftime("%H:%M:%S"), s2]
384
+
385
+ end
386
+
387
+ end
388
+
389
+ def login()
390
+
391
+ view @login
392
+
393
+ end
394
+
395
+ def login_post(username)
396
+
397
+ @chat.login @rws.req, username
398
+ @rws.redirect 'index'
399
+
400
+ end
401
+
402
+ def logout()
403
+
404
+ view @logout
405
+
406
+ end
407
+
408
+ def logout_post()
409
+
410
+ @chat.logout @rws.req
411
+ view @logout_post
412
+
413
+ end
414
+
415
+ def index()
416
+
417
+ if @rws.req.session[:username] then
418
+
419
+ @h[:username] = @rws.req.session[:username]
420
+ view @index
421
+
422
+ else
423
+
424
+ @rws.redirect 'login'
425
+
426
+ end
427
+
428
+ end
429
+
430
+ def messages()
431
+ @chat.messages
432
+ end
433
+
434
+ def req(obj)
435
+
436
+ @rws.req = obj
437
+ self
438
+
439
+ end
440
+
441
+ def users()
442
+ @chat.users
443
+ end
444
+
445
+ private
446
+
447
+ def view(obj)
448
+
449
+ if obj.to_s.length > 0 then
450
+ obj.to_s
451
+ else
452
+ obj
453
+ end
454
+ end
455
+
456
+ 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.2.0
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-05 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.6'
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 0.6.4
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '0.6'
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 0.6.4
40
60
  description:
41
61
  email: james@jamesrobertson.eu
42
62
  executables: []
metadata.gz.sig CHANGED
Binary file