h2g_ajaxchat 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: af5575220d85e86b2c18aabfc6427cb88924328d4fc40c824038108727af5da1
4
+ data.tar.gz: a39989028a432c1c46d40aaecbe42012787261ef73a3f7cd199e31fb2ecf1e30
5
+ SHA512:
6
+ metadata.gz: 27cbfb9a5c8c87de83bd632bd71f98035d3a5c4c666b769f736eadb8a037c53752c4be4ab3b945c1c8d3720cdebc288e6fcb660bcc48a2280d64311d15378699
7
+ data.tar.gz: 4f00dd6a6f1f5e77e05ed2bc86a1043fcc0889c6663edf6b63b2dd48cacc0d92e9900901e72abf163bc8742b186aedc3e9925defc56bb21c476a1c43825ee545
Binary file
@@ -0,0 +1,5 @@
1
+ ��ܪ�M#���*5�~~u��`V&�m½*�G$Tp� Zj�L������d���]��qa{h)0�^�7`U"��(E
2
+ �����_Vuk8�ov�#�T�4\)�X��n�|�K���PJ�-��=D�tA�Y�п�����0{�`���/ �E䂀�^T�
3
+ X���M��2��� �����mZ2�#3:��ޥ ���������
4
+ �Q��o�*�9�?����eU�?��Q���* ߞ��r9�cUw ��
5
+ ?������p:k;-��L�Hx�(>�A�Q @^��1������%]��Y�)� 
@@ -0,0 +1,376 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # file: h2g_ajaxchat.rb
4
+
5
+ # description: This gem makes it easier to build an AJAX chat project.
6
+ # Designed for Rack-rscript.
7
+
8
+
9
+ class DummyRequest
10
+
11
+ def initialize()
12
+
13
+ @session = {}
14
+ @@id ||= 0
15
+ @session[:session_id] = @@id += 1
16
+
17
+ end
18
+
19
+ def session()
20
+ @session
21
+ end
22
+ end
23
+
24
+
25
+ class ChatCore
26
+
27
+ attr_reader :messages, :users
28
+
29
+ def initialize(debug: false)
30
+
31
+ @debug = debug
32
+ @messages = []
33
+ @count = 0
34
+ @users = {}
35
+ end
36
+
37
+ def login(req, username)
38
+ req.session[:username] = username
39
+ end
40
+
41
+ def logout(req)
42
+ req.session.clear
43
+ end
44
+
45
+ def chatter(req, newmsg=nil)
46
+
47
+ @session = req.session
48
+ append_message(newmsg) if newmsg
49
+
50
+ return '' if @messages.empty?
51
+
52
+ c = @session[:last_id]
53
+
54
+
55
+ pos = if c then
56
+
57
+ last_msg = @messages.find {|x| x.object_id.to_i == c}
58
+ last_msg ? @messages.index(last_msg) + 1 : @messages.length - 1
59
+
60
+ else
61
+
62
+ return '' unless newmsg
63
+ puts '_messages: ' + @messages.inspect
64
+ @messages.length - 1
65
+ end
66
+
67
+
68
+ if @debug then
69
+ puts 'pos: ' + pos.inspect
70
+ puts '@messages: ' + @messages.inspect
71
+ end
72
+
73
+
74
+ @session[:last_id] = @messages.last.object_id.to_i
75
+
76
+ a = @messages[pos..-1].map do |t, id, u, msg|
77
+
78
+ if block_given? then
79
+
80
+ yield(t, id, u, msg)
81
+
82
+ else
83
+
84
+ s = "user%s: %s" % [id, msg]
85
+
86
+ [t.strftime("%H:%M:%S"), s].join(' ')
87
+ end
88
+
89
+ end
90
+
91
+ a.join("\n")
92
+
93
+
94
+ end
95
+
96
+ protected
97
+
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]
105
+
106
+ end
107
+
108
+ end
109
+
110
+ class DummyRws
111
+
112
+ attr_accessor :req
113
+
114
+ def initialize(obj)
115
+
116
+ @obj = obj
117
+
118
+ end
119
+
120
+ def redirect(s)
121
+ @obj.method(s.to_sym).call
122
+ end
123
+ end
124
+
125
+ class WebPage
126
+
127
+ def initialize(h={})
128
+ @h = h
129
+ end
130
+
131
+ def to_css()
132
+ end
133
+
134
+ def to_html()
135
+ end
136
+
137
+ def to_js()
138
+ end
139
+
140
+ def to_s()
141
+ html_template()
142
+ end
143
+
144
+ protected
145
+
146
+ def html_template()
147
+
148
+ <<EOF
149
+ <?xml version="1.0" encoding="UTF-8"?>
150
+ <html>
151
+ <head>
152
+ <meta name="viewport" content="width=device-width, initial-scale=1"/>
153
+ <style>
154
+ #{to_css()}
155
+ </style>
156
+ </head>
157
+ #{to_html()}
158
+ <script>
159
+ #{to_js()}
160
+ </script>
161
+ </body>
162
+ </html>
163
+ EOF
164
+ end
165
+ end
166
+
167
+
168
+
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
+
252
+ end
253
+
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
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
+ '
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
305
+ <body onload="refresh()">
306
+ <div id="wrapper">
307
+ <div id="menu">
308
+ <p class="welcome">Welcome, <b> #{@h[:username]} </b></p>
309
+ <p class="logout"><a id="exit" href="logout">Exit Chat</a></p>
310
+ <div style="clear:both"></div>
311
+ </div>
312
+ <div id="chatbox"></div>
313
+ <input name="usermsg" type="text" id="usermsg" size="33" onkeyup='ajaxCall1(event.keyCode, this)' autofocus='true'/>
314
+
315
+ </div>
316
+
317
+ EOF
318
+ end
319
+
320
+ def wp.to_js()
321
+ <<EOF
322
+
323
+ function updateScroll(){
324
+ var element = document.getElementById("chatbox");
325
+ element.scrollTop = element.scrollHeight;
326
+ }
327
+ // ajaxCall1();
328
+
329
+ function ajaxRequest(url, cFunction) {
330
+ var xhttp;
331
+ xhttp = new XMLHttpRequest();
332
+ xhttp.onreadystatechange = function() {
333
+ if (this.readyState == 4 && this.status == 200) {
334
+ cFunction(this);
335
+ }
336
+ };
337
+ xhttp.open("GET", url, true);
338
+ xhttp.send();
339
+ }
340
+
341
+
342
+ function ajaxCall1(keyCode, e) {
343
+ if (keyCode==13){
344
+ ajaxRequest('chatter?msg=' + e.value, ajaxResponse1)
345
+ e.value = '';
346
+ }
347
+ }
348
+
349
+ function ajaxResponse1(xhttp) {
350
+ e = document.getElementById('chatbox')
351
+ s = xhttp.responseText;
352
+ e.innerHTML = e.innerHTML + s;
353
+
354
+ if (s.length > 1)
355
+ updateScroll();
356
+ }
357
+
358
+ function refresh() {
359
+ setInterval(ajaxCall2,2000);
360
+ }
361
+
362
+ function ajaxCall2() {
363
+ ajaxRequest('chatter', ajaxResponse1)
364
+ }
365
+
366
+ EOF
367
+
368
+
369
+ end
370
+
371
+ return wp
372
+
373
+ end
374
+
375
+ end
376
+
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: h2g_ajaxchat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Robertson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIEXjCCAsagAwIBAgIBATANBgkqhkiG9w0BAQsFADAsMSowKAYDVQQDDCFnZW1t
14
+ YXN0ZXIvREM9amFtZXNyb2JlcnRzb24vREM9ZXUwHhcNMjAwNzA0MTg1MjA5WhcN
15
+ MjEwNzA0MTg1MjA5WjAsMSowKAYDVQQDDCFnZW1tYXN0ZXIvREM9amFtZXNyb2Jl
16
+ cnRzb24vREM9ZXUwggGiMA0GCSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQCrZL7w
17
+ t6UFwOZQ4woFqGXULgkq1zEd5MeYdxi93klU009fKgqUDecjxQ3q3QJJi/1ACBms
18
+ jEQh5AweJ0O4n7q5ed0uLcjaTk7OLPFC4fIjNkAG9G0yCwFU7HTIIELfWQOmW1dw
19
+ Hm0b5vgLiccuEi4XYeOd+9YXv0yAzMLuyvUe7wOw5l4IovGR5nomsZouP5LW8fqe
20
+ M7jem0G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/A32vdt
36
+ hlwWFeE0gWND9J9tivS5qxBI
37
+ -----END CERTIFICATE-----
38
+ date: 2020-07-04 00:00:00.000000000 Z
39
+ dependencies: []
40
+ description:
41
+ email: james@jamesrobertson.eu
42
+ executables: []
43
+ extensions: []
44
+ extra_rdoc_files: []
45
+ files:
46
+ - lib/h2g_ajaxchat.rb
47
+ homepage: https://github.com/jrobertson/h2g_ajaxchat
48
+ licenses:
49
+ - MIT
50
+ metadata: {}
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 3.0.3
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: This gem makes it easier to build an AJAX chat project. Designed for Rack-rscript
70
+ test_files: []
@@ -0,0 +1 @@
1
+ 5k�g3�r�# ��_>�V���_+j���IM�9�fKN�~�Ŧ������N[��m�w�Di��b�X3��L�<�l���>�k;�E_�S����!��'����ԑ!.E@7?��IT�j�;���d�h���&I l'��%��v ��G��ޘ "f��+2�{ͮ}V'��$�Tʰ����sO�g"�Ս��ꏄO'q���Lv���U�F�w���A��#��,�3��O��U�,�8iRDJ_�Ux��A���7� ��5���=ebۈ�?C��F���^���J�� ����喉�