webim 0.1.2 → 2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +6 -0
- data/lib/webim.rb +57 -13
- data/test/webim_test.rb +8 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6a9f61021da60564de9cbce023429446b0fed302
|
4
|
+
data.tar.gz: 48f3c2fb09648060614bc0057fc9ac21fce43e1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b33a72d4a793eafdf946db5fa9aa85491e011dde0f341c850c8de1d567cd1f7c3cf28b853200e851f2f50457944d938091afbdd076e404cea49ff7ea9711eba
|
7
|
+
data.tar.gz: c3f9ddfb06fd001a3be88b1efc8e7eaa6c5b520d0a15bad44b2101764d3680a9052a1a599a97db086d0d84ec3b1f5d6e0496cb8ce9d886f7b0fc4d067fe955d4
|
data/README.md
CHANGED
@@ -25,6 +25,12 @@ Once connected, you can forward requests or route messages to the server:
|
|
25
25
|
# 向消息服务器转发用户状态,例如正在输入...
|
26
26
|
client.status("uid2", "typing", "I am typing")
|
27
27
|
|
28
|
+
# 向消息服务器push即时消息
|
29
|
+
client.push("uid1", "uid2", "hahaha")
|
30
|
+
|
31
|
+
# 从消息服务器读取用户现场信息
|
32
|
+
client.presences(["uid1", "uid2"])
|
33
|
+
|
28
34
|
# 从消息服务器读取当前群组在线用户
|
29
35
|
client.members('g1')
|
30
36
|
|
data/lib/webim.rb
CHANGED
@@ -6,7 +6,7 @@ module Webim
|
|
6
6
|
|
7
7
|
APIVSN = "v5"
|
8
8
|
|
9
|
-
VERSION = "0.
|
9
|
+
VERSION = "0.2"
|
10
10
|
|
11
11
|
##
|
12
12
|
# A Ruby webim client library for NexTalk.im.
|
@@ -31,7 +31,7 @@ module Webim
|
|
31
31
|
|
32
32
|
##
|
33
33
|
# 网站域名
|
34
|
-
|
34
|
+
|
35
35
|
attr_reader :domain
|
36
36
|
|
37
37
|
##
|
@@ -54,6 +54,10 @@ module Webim
|
|
54
54
|
#
|
55
55
|
attr_reader :ticket
|
56
56
|
|
57
|
+
##
|
58
|
+
# HTTP Timeout
|
59
|
+
attr_reader :timeout
|
60
|
+
|
57
61
|
##
|
58
62
|
# 创建Client对象
|
59
63
|
#
|
@@ -73,6 +77,7 @@ module Webim
|
|
73
77
|
@apikey = config[:apikey] || ""
|
74
78
|
@host = config[:host] || "127.0.0.1"
|
75
79
|
@port = (config[:port] || DEFAULT_PORT).to_i
|
80
|
+
@timeout = (config[:timeout] || 10 ).to_i
|
76
81
|
end
|
77
82
|
|
78
83
|
##
|
@@ -159,6 +164,30 @@ module Webim
|
|
159
164
|
})
|
160
165
|
end
|
161
166
|
|
167
|
+
##
|
168
|
+
# 向消息服务器推送消息
|
169
|
+
# from: 发送者
|
170
|
+
# to: 接收者
|
171
|
+
# body: 消息体
|
172
|
+
# timestam: 时间戳
|
173
|
+
# type: chat | grpchat
|
174
|
+
# style: 消息CSS
|
175
|
+
#
|
176
|
+
def push(from, to, body, timestamp = nil, type = "chat", style = "")
|
177
|
+
http_post('/messages', {
|
178
|
+
:version => APIVSN,
|
179
|
+
:apikey => @apikey,
|
180
|
+
:domain => @domain,
|
181
|
+
:nick => @user[:nick],
|
182
|
+
:type => type,
|
183
|
+
:from => from,
|
184
|
+
:to => to,
|
185
|
+
:body => body,
|
186
|
+
:style => style,
|
187
|
+
:timestamp => timestamp ? timestamp : Time.now.to_f * 1000
|
188
|
+
})
|
189
|
+
end
|
190
|
+
|
162
191
|
##
|
163
192
|
# 向消息服务器转发用户状态,例如正在输入...
|
164
193
|
#
|
@@ -179,6 +208,26 @@ module Webim
|
|
179
208
|
})
|
180
209
|
end
|
181
210
|
|
211
|
+
##
|
212
|
+
# 从消息服务器读取当前在线用户
|
213
|
+
#
|
214
|
+
# ids: 用户id列表
|
215
|
+
#
|
216
|
+
def presences(ids)
|
217
|
+
res = http_get('/presences', {
|
218
|
+
:version => APIVSN,
|
219
|
+
:ticket => @ticket,
|
220
|
+
:apikey => @apikey,
|
221
|
+
:domain => @domain,
|
222
|
+
:ids => ids.join(",")
|
223
|
+
})
|
224
|
+
if res and (200 == res.code.to_i)
|
225
|
+
JSON.parse(res.body)
|
226
|
+
else
|
227
|
+
nil
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
182
231
|
##
|
183
232
|
# 从消息服务器读取当前群组在线用户
|
184
233
|
#
|
@@ -193,8 +242,7 @@ module Webim
|
|
193
242
|
:group => gid
|
194
243
|
})
|
195
244
|
if res and (200 == res.code.to_i)
|
196
|
-
|
197
|
-
data[gid]
|
245
|
+
JSON.parse(res.body)
|
198
246
|
else
|
199
247
|
nil
|
200
248
|
end
|
@@ -215,11 +263,7 @@ module Webim
|
|
215
263
|
:group => gid
|
216
264
|
})
|
217
265
|
if res and (200 == res.code.to_i)
|
218
|
-
|
219
|
-
{
|
220
|
-
:id => gid,
|
221
|
-
:count => data[gid]
|
222
|
-
}
|
266
|
+
JSON.parse(res.body)
|
223
267
|
else
|
224
268
|
nil
|
225
269
|
end
|
@@ -253,16 +297,16 @@ module Webim
|
|
253
297
|
def http_get(path, params = {})
|
254
298
|
params = params.map {|k,v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}" }.join("&")
|
255
299
|
http = Net::HTTP.new(@host, @port)
|
256
|
-
http.open_timeout =
|
257
|
-
http.read_timeout =
|
300
|
+
http.open_timeout = @timeout
|
301
|
+
http.read_timeout = @timeout
|
258
302
|
http.get('/' + APIVSN + path + "?" + params)
|
259
303
|
end
|
260
304
|
|
261
305
|
def http_post path, params = {}
|
262
306
|
params = params.map {|k,v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}" }.join("&")
|
263
307
|
http = Net::HTTP.new(@host, @port)
|
264
|
-
http.open_timeout =
|
265
|
-
http.read_timeout =
|
308
|
+
http.open_timeout = @timeout
|
309
|
+
http.read_timeout = @timeout
|
266
310
|
http.post('/' + APIVSN + path, params)
|
267
311
|
end
|
268
312
|
|
data/test/webim_test.rb
CHANGED
@@ -18,6 +18,10 @@ class WebimTest < Test::Unit::TestCase
|
|
18
18
|
puts @client.message("uid2", "hahaha")
|
19
19
|
end
|
20
20
|
|
21
|
+
def test_push
|
22
|
+
puts @client.push("uid1", "uid2", "ahbh")
|
23
|
+
end
|
24
|
+
|
21
25
|
def test_presence
|
22
26
|
puts @client.presence('busy', "Busy")
|
23
27
|
end
|
@@ -26,6 +30,10 @@ class WebimTest < Test::Unit::TestCase
|
|
26
30
|
puts @client.status("uid2", "typing", "I am typing")
|
27
31
|
end
|
28
32
|
|
33
|
+
def test_presences
|
34
|
+
puts @client.presences(['uid1', 'ui2'])
|
35
|
+
end
|
36
|
+
|
29
37
|
def test_members
|
30
38
|
puts @client.members('g1')
|
31
39
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webim
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0
|
4
|
+
version: '2.0'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ery Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Ruby webim client library for NexTalk.IM
|
14
14
|
email: ery.lee@gmail.com
|