webim 2.0 → 5.4.2

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.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -0
  3. data/README.md +18 -8
  4. data/lib/webim.rb +114 -156
  5. data/test/webim_test.rb +11 -10
  6. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6a9f61021da60564de9cbce023429446b0fed302
4
- data.tar.gz: 48f3c2fb09648060614bc0057fc9ac21fce43e1c
3
+ metadata.gz: 51fa31548cf086f85ddf724170e3ffe8512703aa
4
+ data.tar.gz: 7c1fe4912d6d0559094ef58e1ff8471f8f2c63b5
5
5
  SHA512:
6
- metadata.gz: 9b33a72d4a793eafdf946db5fa9aa85491e011dde0f341c850c8de1d567cd1f7c3cf28b853200e851f2f50457944d938091afbdd076e404cea49ff7ea9711eba
7
- data.tar.gz: c3f9ddfb06fd001a3be88b1efc8e7eaa6c5b520d0a15bad44b2101764d3680a9052a1a599a97db086d0d84ec3b1f5d6e0496cb8ce9d886f7b0fc4d067fe955d4
6
+ metadata.gz: 5f2482e0ba0998cec7a6d81c1199713587643219e71042e482a548846be342f28f7b03850799cbb266bacc67eefcc574b6ed7c48b8fa06528f5633e9a3ffa798
7
+ data.tar.gz: 4b48d9ebf3acad34045e3c9587907e518e9c197eefbc68cbaefea0a4652b5bca09bf341268f1f0f771a3f68157c11ac14dee0932a992266ce8c47551fb0c650e
@@ -1,4 +1,10 @@
1
1
 
2
+ # 5.4.2 (2014-03-30)
3
+
4
+ * version number synchronized with nextalk server
5
+ * room api changed
6
+ * http basic authentication
7
+
2
8
  # 0.1.2 (2013-12-13)
3
9
 
4
10
  * fix lib/webim.rb:146: syntax error, unexpected ')'
data/README.md CHANGED
@@ -1,23 +1,29 @@
1
- # webim
1
+ # WebIM
2
2
 
3
3
  A Ruby webim library for the [nextalk.im](http://nextal.im).
4
4
 
5
+ ## Require
6
+
7
+ Ruby2.0+
8
+
5
9
  ## Getting started
6
10
 
7
11
  You can connect to NexTalk server by instantiating the `Webim::Client` class:
8
12
 
9
13
  require "webim"
10
14
 
11
- user = {:id => "vid:1", :nick => "user1", :show => "available", :status => 'Online'}
12
- client = Webim::Client.new(user, "", {:domain => "localhost", :apikey => "public", :host => "localhost", :port => 8000})
15
+ endpoint = {:id => "uid1", :nick => "user1", :show => "available", :status => 'Online'}
16
+ client = WebIM::Client.new(endpoint, "domain", "apikey", "localhost", 8000)
13
17
 
14
18
  Once connected, you can forward requests or route messages to the server:
15
19
 
20
+ ```ruby
21
+
16
22
  # 向消息服务器转发用户上线消息,同时发送用户的好友id列表、群组id列表
17
- client.online(['uid2','uid3', 'uid4'], ['g1', 'g2', 'g3'])
23
+ client.online(['uid2','uid3', 'uid4'], ['room1', 'room2', 'room3'])
18
24
 
19
25
  # 向消息服务器转发聊天消息
20
- client.message("uid2", "hahaha")
26
+ client.message({:to => "uid2", :body => "hahaha"})
21
27
 
22
28
  # 向消息服务器转发用户现场变化
23
29
  client.presence('busy', "Busy")
@@ -26,19 +32,23 @@ Once connected, you can forward requests or route messages to the server:
26
32
  client.status("uid2", "typing", "I am typing")
27
33
 
28
34
  # 向消息服务器push即时消息
29
- client.push("uid1", "uid2", "hahaha")
35
+ client.message({:from => "uid1", :to => "uid2", :body => "ahbh"})
30
36
 
31
37
  # 从消息服务器读取用户现场信息
32
38
  client.presences(["uid1", "uid2"])
33
39
 
34
40
  # 从消息服务器读取当前群组在线用户
35
- client.members('g1')
41
+ client.members('room1')
36
42
 
37
43
  # 向消息服务器转发用户下线消息
38
44
  client.offline
39
45
 
46
+ ```
47
+
40
48
  ## Gem build and install
41
49
 
42
50
  gem build webim.gemspec
43
51
 
44
- gem install webim-0.1.0.gem
52
+ gem install webim-5.4.2.gem
53
+
54
+
@@ -1,12 +1,19 @@
1
+
1
2
  require 'net/http'
2
3
  require 'uri'
3
4
  require 'json'
4
5
 
5
- module Webim
6
+ module WebIM
6
7
 
7
8
  APIVSN = "v5"
8
9
 
9
- VERSION = "0.2"
10
+ VERSION = "5.4.2"
11
+
12
+ ##
13
+ # A Ruby webim error
14
+ #
15
+ class Error < StandardError
16
+ end
10
17
 
11
18
  ##
12
19
  # A Ruby webim client library for NexTalk.im.
@@ -27,7 +34,7 @@ module Webim
27
34
  # show: 'available' | 'away' | 'chat' | 'dnd' | 'invisible' | 'unavailable'
28
35
  # status: 状态信息
29
36
  #
30
- attr_reader :user
37
+ attr_reader :endpoint
31
38
 
32
39
  ##
33
40
  # 网站域名
@@ -61,52 +68,52 @@ module Webim
61
68
  ##
62
69
  # 创建Client对象
63
70
  #
64
- # user: 用户Hash对象
71
+ # endpoint: 用户Hash对象
72
+ # domain: 域名
73
+ # apikey: 通信APIKEY
74
+ # host: 服务器地址
75
+ # port: 服务器端口号
65
76
  # ticket: 通信令牌
66
- #
67
- # config配置参数:
68
- # domain: 域名
69
- # apikey: 通信APIKEY
70
- # host: 服务器地址
71
- # port: 服务器端口号
77
+ # timeout: HTTP超时时间
72
78
  #
73
- def initialize(user, ticket = "", config = {})
74
- @user = user
79
+ def initialize(endpoint, domain, apikey, host = 'localhost', port = 8000, ticket = nil, timeout = 15)
80
+ @endpoint = endpoint
81
+ @domain = domain
82
+ @apikey = apikey
83
+ @host = host
84
+ @port = port
75
85
  @ticket = ticket
76
- @domain = config[:domain] || ""
77
- @apikey = config[:apikey] || ""
78
- @host = config[:host] || "127.0.0.1"
79
- @port = (config[:port] || DEFAULT_PORT).to_i
80
- @timeout = (config[:timeout] || 10 ).to_i
86
+ @timeout = timeout
81
87
  end
82
88
 
83
89
  ##
84
90
  # 向消息服务器转发用户上线消息,同时发送用户的好友id列表、群组id列表
85
91
  #
86
92
  # buddies: 好友id列表
87
- # groups: 群组id列表
93
+ # rooms: 群组id列表
88
94
  #
89
- def online(buddies, groups)
90
- res = http_post('/presences/online', {
91
- :version => APIVSN,
95
+ def online(buddies, rooms)
96
+ resp = httpost('/presences/online', reqdata().merge({
92
97
  :buddies => buddies.join(','),
93
- :groups => groups.join(','),
98
+ :rooms => rooms.join(','),
99
+ :name => @endpoint[:id],
100
+ :nick => @endpoint[:nick],
101
+ :show => @endpoint[:show],
102
+ :status => @endpoint[:status]
103
+ }))
104
+ @ticket = resp["ticket"]
105
+ connection = {
106
+ :ticket => @ticket,
94
107
  :domain => @domain,
95
- :apikey => @apikey,
96
- :name => @user[:id],
97
- :nick => @user[:nick],
98
- :show => @user[:show],
99
- :status => @user[:status]
100
- })
101
- if res and 200 == res.code.to_i
102
- data = JSON.parse(res.body)
103
- @ticket = data["ticket"]
104
- return data
105
- end
106
- return {
107
- :success => false,
108
- :error_code => res ? res.code : -1,
109
- :error_msg => res ? res.body : ""
108
+ :server => resp['server'],
109
+ :jsonpd => resp['jsonpd']
110
+ }
111
+ connection[:websocket] = resp['websocket'] if resp['websocket']
112
+ connection[:mqtt] = resp['mqtt'] if resp['mqtt']
113
+ {
114
+ :success => true,
115
+ :connection => connection,
116
+ :presences => resp['presences']
110
117
  }
111
118
  end
112
119
 
@@ -114,12 +121,7 @@ module Webim
114
121
  # 向消息服务器转发用户下线消息
115
122
  #
116
123
  def offline
117
- http_post('/presences/offline', {
118
- :version => APIVSN,
119
- :ticket => @ticket,
120
- :apikey => @apikey,
121
- :domain => @domain
122
- })
124
+ httpost('/presences/offline', reqdata())
123
125
  end
124
126
 
125
127
  ##
@@ -129,15 +131,11 @@ module Webim
129
131
  # status: 状态信息
130
132
  #
131
133
  def presence(show, status = "")
132
- http_post('/presences/show', {
133
- :version => APIVSN,
134
- :ticket => @ticket,
135
- :apikey => @apikey,
136
- :domain => @domain,
137
- :nick => @user[:nick],
134
+ httpost('/presences/show', reqdata().merge({
135
+ :nick => @endpoint[:nick],
138
136
  :show => show,
139
137
  :status => status
140
- })
138
+ }))
141
139
  end
142
140
 
143
141
  ##
@@ -149,43 +147,16 @@ module Webim
149
147
  # type: chat | grpchat
150
148
  # style: 消息CSS
151
149
  #
152
- def message(to, body, timestamp = nil, type = "chat", style = "")
153
- http_post('/messages', {
154
- :version => APIVSN,
155
- :ticket => @ticket,
156
- :apikey => @apikey,
157
- :domain => @domain,
158
- :nick => @user[:nick],
159
- :type => type,
160
- :to => to,
161
- :body => body,
162
- :style => style,
163
- :timestamp => timestamp ? timestamp : Time.now.to_f * 1000
164
- })
165
- end
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
150
+ def message(message)
151
+ reqdata = reqdata()
152
+ reqdata.merge!({
153
+ :nick => @endpoint[:nick],
154
+ :type => "chat",
155
+ :style => "",
156
+ :timestamp => Time.now.to_f * 1000
188
157
  })
158
+ reqdata.merge!(message)
159
+ httpost('/messages', reqdata)
189
160
  end
190
161
 
191
162
  ##
@@ -196,16 +167,12 @@ module Webim
196
167
  # status: 状态信息
197
168
  #
198
169
  def status(to, show, status="")
199
- http_post('/statuses', {
200
- :version => APIVSN,
201
- :ticket => @ticket,
202
- :apikey => @apikey,
203
- :domain => @domain,
204
- :nick => @user[:nick],
170
+ httpost('/statuses', reqdata().merge({
171
+ :nick => @endpoint[:nick],
205
172
  :to => to,
206
173
  :show => show,
207
174
  :status => status
208
- })
175
+ }))
209
176
  end
210
177
 
211
178
  ##
@@ -214,100 +181,91 @@ module Webim
214
181
  # ids: 用户id列表
215
182
  #
216
183
  def presences(ids)
217
- res = http_get('/presences', {
218
- :version => APIVSN,
219
- :ticket => @ticket,
220
- :apikey => @apikey,
221
- :domain => @domain,
184
+ httpget('/presences', reqdata().merge({
222
185
  :ids => ids.join(",")
223
- })
224
- if res and (200 == res.code.to_i)
225
- JSON.parse(res.body)
226
- else
227
- nil
228
- end
186
+ }))
229
187
  end
230
188
 
231
189
  ##
232
190
  # 从消息服务器读取当前群组在线用户
233
191
  #
234
- # gid: 群组id
192
+ # room: 群组room
235
193
  #
236
- def members(gid)
237
- res = http_get('/group/members', {
238
- :version => APIVSN,
239
- :ticket => @ticket,
240
- :apikey => @apikey,
241
- :domain => @domain,
242
- :group => gid
243
- })
244
- if res and (200 == res.code.to_i)
245
- JSON.parse(res.body)
246
- else
247
- nil
248
- end
194
+ def members(room)
195
+ httpget("/rooms/#{room}/members", reqdata().merge({
196
+ :room => room
197
+ }))
249
198
  end
250
199
 
251
200
  ##
252
201
  # 转发加入群组消息
253
202
  #
254
- # gid: 群组id
203
+ # room: 群组id
255
204
  #
256
- def join(gid)
257
- res = http_post('/group/join', {
258
- :version => APIVSN,
259
- :ticket => @ticket,
260
- :apikey => @apikey,
261
- :domain => @domain,
262
- :nick => @user[:nick],
263
- :group => gid
264
- })
265
- if res and (200 == res.code.to_i)
266
- JSON.parse(res.body)
267
- else
268
- nil
269
- end
205
+ def join(room)
206
+ httpost("/rooms/#{room}/join", reqdata().merge({
207
+ :nick => @endpoint[:nick],
208
+ :room => room
209
+ }))
270
210
  end
271
211
 
272
212
  ##
273
213
  # 转发离开群组消息
274
214
  #
275
- # gid: 群组id
215
+ # room: 群组id
276
216
  #
277
- def leave(gid)
278
- http_post('/group/leave', {
279
- :version => APIVSN,
280
- :ticket => @ticket,
281
- :apikey => @apikey,
282
- :domain => @domain,
283
- :nick => @user[:nick],
284
- :group => gid
285
- })
217
+ def leave(room)
218
+ httpost("/rooms/#{room}/leave", reqdata().merge({
219
+ :nick => @endpoint[:nick],
220
+ :room => room
221
+ }))
286
222
  end
287
223
 
288
224
  ##
289
225
  # Clien对象描述
290
226
  def inspect
291
- "<Webim::Client: ticket=%s, domain=%s, apikey=%s, host=%s , port=%p>" %
227
+ "<Webim::Client: ticket=%s, domain=%s, apikey=%s, host=%s, port=%p>" %
292
228
  [@ticket, @domain, @apikey, @host, @port]
293
229
  end
294
230
 
295
231
  private
296
232
 
297
- def http_get(path, params = {})
298
- params = params.map {|k,v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}" }.join("&")
299
- http = Net::HTTP.new(@host, @port)
300
- http.open_timeout = @timeout
301
- http.read_timeout = @timeout
302
- http.get('/' + APIVSN + path + "?" + params)
233
+ def httpget(api, params = {})
234
+ uri = URI("http://#{@host}:#{@port}/#{APIVSN}#{api}")
235
+ uri.query = URI.encode_www_form(params)
236
+ req = Net::HTTP::Get.new(uri)
237
+ req.basic_auth @domain, @apikey
238
+ resp = Net::HTTP.start(@host, @port) {|http|
239
+ http.open_timeout = @timeout
240
+ http.read_timeout = @timeout
241
+ http.request(req)
242
+ }
243
+ return JSON.parse(resp.body) if resp and resp.code == '200'
244
+ raise Error, "#{resp.code} #{resp.message} #{resp.body}"
303
245
  end
304
246
 
305
- def http_post path, params = {}
306
- params = params.map {|k,v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}" }.join("&")
307
- http = Net::HTTP.new(@host, @port)
308
- http.open_timeout = @timeout
309
- http.read_timeout = @timeout
310
- http.post('/' + APIVSN + path, params)
247
+ def httpost(api, params = {})
248
+ uri = URI("http://#{@host}:#{@port}/#{APIVSN}#{api}")
249
+ req = Net::HTTP::Post.new(uri)
250
+ req.basic_auth @domain, @apikey
251
+ req.set_form_data(params)
252
+ resp = Net::HTTP.start(@host, @port) {|http|
253
+ http.open_timeout = @timeout
254
+ http.read_timeout = @timeout
255
+ http.request(req)
256
+ }
257
+ return JSON.parse(resp.body) if resp and resp.code == '200'
258
+ raise Error, "#{resp.code} #{resp.message} #{resp.body}"
259
+ end
260
+
261
+ def reqdata()
262
+ data = {
263
+ :version => VERSION,
264
+ :ticket => @ticket,
265
+ :domain => @domain
266
+ }
267
+ data[:ticket] = @ticket if @ticket
268
+ data
311
269
  end
312
270
 
313
271
  end
@@ -5,21 +5,21 @@ require 'test/unit'
5
5
  class WebimTest < Test::Unit::TestCase
6
6
 
7
7
  def setup
8
- user = {:id => "vid:1", :nick => "user1", :show => "available", :status => 'Online'}
9
- @client = Webim::Client.new(user, "", {:domain => "localhost", :apikey => "public", :host => "localhost", :port => 8000})
10
- @client.online(['uid2','uid3', 'uid4'], ['g1', 'g2', 'g3'])
8
+ endpoint = {:id => "uid1", :nick => "user1", :show => "available", :status => 'Online'}
9
+ @client = WebIM::Client.new(endpoint, "localhost", "public", "localhost", 8000)
10
+ @client.online(['uid2','uid3', 'uid4'], ['room1', 'room2', 'room3'])
11
11
  end
12
12
 
13
13
  def test_online
14
- puts @client.online(['uid2','uid3', 'uid4'], ['g1', 'g2', 'g3'])
14
+ puts @client.online(['uid2','uid3', 'uid4'], ['room1', 'room2', 'room3'])
15
15
  end
16
16
 
17
17
  def test_message
18
- puts @client.message("uid2", "hahaha")
18
+ puts @client.message({:to => "uid2", :body => "hahaha"})
19
19
  end
20
20
 
21
21
  def test_push
22
- puts @client.push("uid1", "uid2", "ahbh")
22
+ puts @client.message({:from => "uid1", :to => "uid2", :body => "ahbh"})
23
23
  end
24
24
 
25
25
  def test_presence
@@ -31,19 +31,20 @@ class WebimTest < Test::Unit::TestCase
31
31
  end
32
32
 
33
33
  def test_presences
34
- puts @client.presences(['uid1', 'ui2'])
34
+ puts @client.presences(['uid1', 'uid2'])
35
35
  end
36
36
 
37
37
  def test_members
38
- puts @client.members('g1')
38
+ @client.join('room1')
39
+ puts @client.members('room1')
39
40
  end
40
41
 
41
42
  def test_join
42
- puts @client.join('g1')
43
+ puts @client.join('room1')
43
44
  end
44
45
 
45
46
  def test_leave
46
- puts @client.leave('g1')
47
+ puts @client.leave('room1')
47
48
  end
48
49
 
49
50
  def test_offline
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: '2.0'
4
+ version: 5.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ery Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-10 00:00:00.000000000 Z
11
+ date: 2014-03-30 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