-
Notifications
You must be signed in to change notification settings - Fork 5
/
lingr.rb
395 lines (334 loc) · 10.1 KB
/
lingr.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#-*- coding: utf-8 -*-
# Created by Satoshi Nakagawa.
# You can redistribute it and/or modify it under the Ruby's license or the GPL2.
require 'net/http'
require 'open-uri'
require 'cgi'
require 'logger'
require 'timeout'
require 'rubygems'
require 'json'
module Lingr
DEFAULT_BACKLOG_COUNT = 30
class Member
attr_reader :username, :name, :icon_url, :owner
attr_accessor :presence
def initialize(res)
@username = res["username"]
@name = res["name"]
@icon_url = res["icon_url"]
@owner = res["is_owner"]
@presence = res["is_online"] == 1
end
def inspect
%Q[<#{self.class} #{username} #{name}>]
end
end
class Room
attr_reader :id, :name, :blurb, :public, :backlog, :members
def initialize(res)
@id = res["id"]
@name = res["name"]
@blurb = res["blurb"]
@public = res["public"]
@backlog = []
@members = {}
if msgs = res["messages"]
msgs.each do |m|
@backlog << Message.new(m)
end
end
if roster = res["roster"]
if members = roster["members"]
members.each do |u|
m = Member.new(u)
@members[m.username] = m
end
end
end
end
def add_member(member)
@members[m.username] = member
end
def add_backlog(msgs)
msgs.reverse_each do |m|
@backlog.insert(0, m)
end
end
def inspect
%Q[<#{self.class} #{id}>]
end
end
class Message
attr_reader :id, :type, :nickname, :speaker_id, :public_session_id, :text, :timestamp, :mine
def initialize(res)
@id = res["id"]
@type = res["type"]
@nickname = res["nickname"]
@speaker_id = res["speaker_id"]
@public_session_id = res["public_session_id"]
@text = res["text"]
@timestamp = Time.iso8601(res["timestamp"])
@mine = false
end
def decide_mine(my_public_session_id)
@mine = @public_session_id == my_public_session_id
end
def inspect
%Q[<#{self.class} #{speaker_id}: #{text}>]
end
end
class APIError < Exception
attr_reader :code, :detail
def initialize(res)
@code = res["code"]
@detail = res["detail"]
end
def inspect
%Q[<#{self.class} code="#{@code}", detail="#{@detail}">]
end
end
class Connection
URL_BASE = "http://lingr.com/api/"
URL_BASE_OBSERVE = "http://lingr.com:8080/api/"
REQUEST_TIMEOUT = 100
RETRY_INTERVAL = 60
attr_reader :user, :password, :auto_reconnect
attr_reader :nickname, :public_id, :presence, :name, :username
attr_reader :room_ids, :rooms
attr_reader :connected_hooks, :error_hooks, :message_hooks, :join_hooks, :leave_hooks
def initialize(user, password, backlog_count, auto_reconnect=true, logger=nil, api_key=nil)
@user = user
@password = password
@backlog_count = backlog_count
@auto_reconnect = auto_reconnect
@logger = logger
@api_key = api_key
@connected_hooks = []
@error_hooks = []
@message_hooks = []
@join_hooks = []
@leave_hooks = []
end
def start
begin
create_session
get_rooms
show_room(@room_ids.join(','))
subscribe(@room_ids.join(','))
if @backlog_count > DEFAULT_BACKLOG_COUNT
@rooms.each do |room_id, room|
if m = room.backlog[0]
res = get_archives(room_id, m.id, @backlog_count - DEFAULT_BACKLOG_COUNT)
if msgs = res['messages']
room.add_backlog(msgs.map {|e| Message.new(e)})
end
end
end
end
@connected_hooks.each {|h| h.call(self) }
loop do
observe
end
rescue APIError => e
raise e if e.code == "invalid_user_credentials"
on_error(e)
retry if @auto_reconnect
rescue IOError,JSON::ParserError => e
on_error(e)
retry if @auto_reconnect
end
end
def create_session
debug { "requesting session/create: #{@user}" }
params = {:user => @user, :password => @password}
params.merge!(:api_key => @api_key) if @api_key
res = post("session/create", params)
debug { "session/create response: #{res.inspect}" }
@session = res["session"]
@nickname = res["nickname"]
@public_id = res["public_id"]
@presence = res["presence"]
if user = res["user"]
@name = user["name"]
@username = user["username"]
end
@rooms = {}
res
end
def destroy_session
debug { "requesting session/destroy" }
begin
res = post("session/destroy", :session => @session)
debug { "session/destroy response: #{res.inspect}" }
rescue Exception => e
log_error { "error in destroy_session: #{e.inspect}" }
end
@session = nil
@nickname = nil
@public_id = nil
@presence = nil
@name = nil
@username = nil
@rooms = {}
res
end
def set_presence(presence)
debug { "requesting session/set_presence: #{presence}" }
res = post("session/set_presence", :session => @session, :presence => presence, :nickname => @nickname)
debug { "session/set_presence response: #{res.inspect}" }
res
end
def get_rooms
debug { "requesting user/response" }
res = get("user/get_rooms", :session => @session)
debug { "user/get_rooms response: #{res.inspect}" }
@room_ids = res["rooms"]
res
end
def show_room(room_id)
debug { "requesting room/show: #{room_id}" }
res = get("room/show", :session => @session, :room => room_id)
debug { "room/show response: #{res.inspect}" }
if rooms = res["rooms"]
rooms.each do |d|
r = Room.new(d)
r.backlog.each do |m|
m.decide_mine(@public_id)
end
@rooms[r.id] = r
end
end
res
end
def get_archives(room_id, max_message_id, limit=100)
debug { "requesting room/get_archives: #{room_id} #{max_message_id}" }
res = get("room/get_archives", :session => @session, :room => room_id, :before => max_message_id, :limit => limit)
debug { "room/get_archives response: #{res.inspect}" }
res
end
def subscribe(room_id, reset=true)
debug { "requesting room/subscribe: #{room_id}" }
res = post("room/subscribe", :session => @session, :room => room_id, :reset => reset.to_s)
debug { "room/subscribe response: #{res.inspect}" }
@counter = res["counter"]
res
end
def unsubscribe(room_id)
debug { "requesting room/unsubscribe: #{room_id}" }
res = post("room/unsubscribe", :session => @session, :room => room_id)
debug { "room/unsubscribe response: #{res.inspect}" }
res
end
def say(room_id, text)
debug { "requesting room/say: #{room_id} #{text}" }
res = post("room/say", :session => @session, :room => room_id, :nickname => @nickname, :text => text)
debug { "room/say response: #{res.inspect}" }
res
end
def observe
debug { "requesting event/observe: #{@counter}" }
res = get("event/observe", :session => @session, :counter => @counter)
debug { "observe response: #{res.inspect}" }
@counter = res["counter"] if res["counter"]
if events = res["events"]
events.each do |event|
if d = event["message"]
if room = @rooms[d["room"]]
m = Message.new(d)
m.decide_mine(@public_id)
@message_hooks.each {|h| h.call(self, room, m) }
end
elsif d = event["presence"]
if room = @rooms[d["room"]]
username = d["username"]
id = d["public_session_id"]
if status = d["status"]
case status
when "online"
if m = room.members[username]
m.presence = true
@join_hooks.each {|h| h.call(self, room, m) }
end
when "offline"
if m = room.members[username]
m.presence = false
@leave_hooks.each {|h| h.call(self, room, m) }
end
end
end
end
end
end
end
res
end
private
def on_error(e)
log_error { "error: #{e.inspect}" }
destroy_session if @session
@error_hooks.each {|h| h.call(self, e) }
sleep RETRY_INTERVAL if @auto_reconnect
end
def get(path, params=nil)
is_observe = path == "event/observe"
url = is_observe ? URL_BASE_OBSERVE : URL_BASE
url += path
if params
url += '?' + params.map{|k,v| "#{k}=#{CGI.escape(v.to_s)}"}.join('&')
end
res = nil
begin
timeout(REQUEST_TIMEOUT) do
open(url) do |r|
res = JSON.parse(r.read)
end
end
rescue TimeoutError
debug { "get request timed out: #{url}" }
if is_observe
res = { "status" => "ok" }
else
raise
end
end
if res["status"] == "ok"
res
else
raise APIError.new(res)
end
end
def post(path, params=nil)
url = URL_BASE + path
if params
url += '?' + params.map{|k,v| "#{k}=#{CGI.escape(v.to_s)}"}.join('&')
end
u = URI.parse(url)
res = nil
begin
timeout(REQUEST_TIMEOUT) do
http = Net::HTTP.new(u.host, u.port)
response = http.post(u.path, u.query)
res = JSON.parse(response.body)
end
rescue TimeoutError
debug { "post request timed out: #{url}" }
raise
end
if res["status"] == "ok"
res
else
raise APIError.new(res)
end
end
def debug(&block)
@logger.debug(&block) if @logger
end
def log(&block)
@logger.info(&block) if @logger
end
def log_error(&block)
@logger.error(&block) if @logger
end
end
end