webim 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 018cfb15bf5938d1909314a2797127959e8b7bea
4
+ data.tar.gz: 85b5d031659a78e7f6be1ea0906c4e25206d2057
5
+ SHA512:
6
+ metadata.gz: ec27f9a54ffb5f8e0becc4387ba8ffd1b4e46a794091faf86164369dd52ca85013aa4dacc1a1175398c57742175265bd0937327128c2bf37baf67bacb9745f8f
7
+ data.tar.gz: 4720f02fb183d9dcb864de4ceb3b2ccbb97e62310c7aa07b86b454b689a3ce59b592c0b5a985253d223a8ba0a977b9d4b846bf542b84a4af1fa524da27e8d86a
@@ -0,0 +1,4 @@
1
+ # 0.1 (2013-12-12)
2
+
3
+ * first release
4
+
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 WebIM
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,38 @@
1
+ # webim
2
+
3
+ A Ruby webim library for the [nextalk.im](http://nextal.im).
4
+
5
+ ## Getting started
6
+
7
+ You can connect to NexTalk server by instantiating the `Webim::Client` class:
8
+
9
+ require "webim"
10
+
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})
13
+
14
+ Once connected, you can forward requests or route messages to the server:
15
+
16
+ # 向消息服务器转发用户上线消息,同时发送用户的好友id列表、群组id列表
17
+ client.online(['uid2','uid3', 'uid4'], ['g1', 'g2', 'g3'])
18
+
19
+ # 向消息服务器转发聊天消息
20
+ client.message("uid2", "hahaha")
21
+
22
+ # 向消息服务器转发用户现场变化
23
+ client.presence('busy', "Busy")
24
+
25
+ # 向消息服务器转发用户状态,例如正在输入...
26
+ client.status("uid2", "typing", "I am typing")
27
+
28
+ # 从消息服务器读取当前群组在线用户
29
+ client.members('g1')
30
+
31
+ # 向消息服务器转发用户下线消息
32
+ client.offline
33
+
34
+ ## Gem build and install
35
+
36
+ gem build webim.gemspec
37
+
38
+ gem install webim-0.1.0.gem
data/TODO.md ADDED
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,270 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+
5
+ module Webim
6
+
7
+ APIVSN = "v5"
8
+
9
+ VERSION = "0.1"
10
+
11
+ ##
12
+ # A Ruby webim client library for NexTalk.im.
13
+ #
14
+ class Client
15
+
16
+ ##
17
+ # Default port.
18
+
19
+ DEFAULT_PORT = 8000
20
+
21
+ ##
22
+ # 用户Hash对象
23
+ #
24
+ # 属性:
25
+ # id: 用户id
26
+ # nick: 用户昵称
27
+ # show: 'available' | 'away' | 'chat' | 'dnd' | 'invisible' | 'unavailable'
28
+ # status: 状态信息
29
+ #
30
+ attr_reader :user
31
+
32
+ ##
33
+ # 网站域名
34
+ #
35
+ attr_reader :domain
36
+
37
+ ##
38
+ # 通信APIKEY
39
+ #
40
+ attr_reader :apikey
41
+
42
+ ##
43
+ # 消息服务器地址
44
+ #
45
+ attr_reader :host
46
+
47
+ ##
48
+ # 消息服务器端口
49
+ #
50
+ attr_reader :port
51
+
52
+ ##
53
+ # 消息服务器通信令牌
54
+ #
55
+ attr_reader :ticket
56
+
57
+ ##
58
+ # 创建Client对象
59
+ #
60
+ # user: 用户Hash对象
61
+ # ticket: 通信令牌
62
+ #
63
+ # config配置参数:
64
+ # domain: 域名
65
+ # apikey: 通信APIKEY
66
+ # host: 服务器地址
67
+ # port: 服务器端口号
68
+ #
69
+ def initialize(user, ticket = "", config = {})
70
+ @user = user
71
+ @ticket = ticket
72
+ @domain = config[:domain] || ""
73
+ @apikey = config[:apikey] || ""
74
+ @host = config[:host] || "127.0.0.1"
75
+ @port = (config[:port] || DEFAULT_PORT).to_i
76
+ end
77
+
78
+ ##
79
+ # 向消息服务器转发用户上线消息,同时发送用户的好友id列表、群组id列表
80
+ #
81
+ # buddies: 好友id列表
82
+ # groups: 群组id列表
83
+ #
84
+ def online(buddies, groups)
85
+ res = http_post('/presences/online', {
86
+ :version => APIVSN,
87
+ :buddies => buddies.join(','),
88
+ :groups => groups.join(','),
89
+ :domain => @domain,
90
+ :apikey => @apikey,
91
+ :name => @user[:id],
92
+ :nick => @user[:nick],
93
+ :show => @user[:show],
94
+ :status => @user[:status]
95
+ })
96
+ if res and 200 == res.code.to_i
97
+ data = JSON.parse(res.body)
98
+ @ticket = data["ticket"]
99
+ return data
100
+ end
101
+ return {
102
+ :success => false,
103
+ :error_code => res ? res.code : -1,
104
+ :error_msg => res ? res.body : ""
105
+ }
106
+ end
107
+
108
+ ##
109
+ # 向消息服务器转发用户下线消息
110
+ #
111
+ def offline
112
+ http_post('/presences/offline', {
113
+ :version => APIVSN,
114
+ :ticket => @ticket,
115
+ :apikey => @apikey,
116
+ :domain => @domain
117
+ })
118
+ end
119
+
120
+ ##
121
+ # 向消息服务器转发用户现场变化
122
+ #
123
+ # show: 'away' | 'chat' | 'dnd' | 'invisible'
124
+ # status: 状态信息
125
+ #
126
+ def presence(show, status = "")
127
+ http_post('/presences/show', {
128
+ :version => APIVSN,
129
+ :ticket => @ticket,
130
+ :apikey => @apikey,
131
+ :domain => @domain,
132
+ :nick => @user[:nick],
133
+ :show => show,
134
+ :status => status
135
+ })
136
+ end
137
+
138
+ ##
139
+ # 向消息服务器转发聊天消息
140
+ #
141
+ # to: 接收者
142
+ # body: 消息体
143
+ # type: chat | grpchat
144
+ # style: 消息CSS
145
+ #
146
+ def message(to, body, type = "chat", style = "")
147
+ http_post('/messages', {
148
+ :version => APIVSN,
149
+ :ticket => @ticket,
150
+ :apikey => @apikey,
151
+ :domain => @domain,
152
+ :nick => @user[:nick],
153
+ :type => type,
154
+ :to => to,
155
+ :body => body,
156
+ :style => style,
157
+ :timestamp => Time.now.to_f * 1000
158
+ })
159
+ end
160
+
161
+ ##
162
+ # 向消息服务器转发用户状态,例如正在输入...
163
+ #
164
+ # to: 接受者
165
+ # show: typing
166
+ # status: 状态信息
167
+ #
168
+ def status(to, show, status="")
169
+ http_post('/statuses', {
170
+ :version => APIVSN,
171
+ :ticket => @ticket,
172
+ :apikey => @apikey,
173
+ :domain => @domain,
174
+ :nick => @user[:nick],
175
+ :to => to,
176
+ :show => show,
177
+ :status => status
178
+ })
179
+ end
180
+
181
+ ##
182
+ # 从消息服务器读取当前群组在线用户
183
+ #
184
+ # gid: 群组id
185
+ #
186
+ def members(gid)
187
+ res = http_get('/group/members', {
188
+ :version => APIVSN,
189
+ :ticket => @ticket,
190
+ :apikey => @apikey,
191
+ :domain => @domain,
192
+ :group => gid
193
+ })
194
+ if res and (200 == res.code.to_i)
195
+ data = JSON.parse(res.body)
196
+ data[gid]
197
+ else
198
+ nil
199
+ end
200
+ end
201
+
202
+ ##
203
+ # 转发加入群组消息
204
+ #
205
+ # gid: 群组id
206
+ #
207
+ def join(gid)
208
+ res = http_post('/group/join', {
209
+ :version => APIVSN,
210
+ :ticket => @ticket,
211
+ :apikey => @apikey,
212
+ :domain => @domain,
213
+ :nick => @user[:nick],
214
+ :group => gid
215
+ })
216
+ if res and (200 == res.code.to_i)
217
+ data = JSON.parse(res.body)
218
+ {
219
+ :id => gid,
220
+ :count => data[gid]
221
+ }
222
+ else
223
+ nil
224
+ end
225
+ end
226
+
227
+ ##
228
+ # 转发离开群组消息
229
+ #
230
+ # gid: 群组id
231
+ #
232
+ def leave(gid)
233
+ http_post('/group/leave', {
234
+ :version => APIVSN,
235
+ :ticket => @ticket,
236
+ :apikey => @apikey,
237
+ :domain => @domain,
238
+ :nick => @user[:nick],
239
+ :group => gid
240
+ })
241
+ end
242
+
243
+ ##
244
+ # Clien对象描述
245
+ def inspect
246
+ "<Webim::Client: ticket=%s, domain=%s, apikey=%s, host=%s , port=%p>" %
247
+ [@ticket, @domain, @apikey, @host, @port]
248
+ end
249
+
250
+ private
251
+
252
+ def http_get(path, params = {})
253
+ params = params.map {|k,v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}" }.join("&")
254
+ http = Net::HTTP.new(@host, @port)
255
+ http.open_timeout = 10
256
+ http.read_timeout = 10
257
+ http.get('/' + APIVSN + path + "?" + params)
258
+ end
259
+
260
+ def http_post path, params = {}
261
+ params = params.map {|k,v| "#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}" }.join("&")
262
+ http = Net::HTTP.new(@host, @port)
263
+ http.open_timeout = 10
264
+ http.read_timeout = 10
265
+ http.post('/' + APIVSN + path, params)
266
+ end
267
+
268
+ end
269
+
270
+ end
@@ -0,0 +1,49 @@
1
+ require "../lib/webim"
2
+
3
+ require 'test/unit'
4
+
5
+ class WebimTest < Test::Unit::TestCase
6
+
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'])
11
+ end
12
+
13
+ def test_online
14
+ puts @client.online(['uid2','uid3', 'uid4'], ['g1', 'g2', 'g3'])
15
+ end
16
+
17
+ def test_message
18
+ puts @client.message("uid2", "hahaha")
19
+ end
20
+
21
+ def test_presence
22
+ puts @client.presence('busy', "Busy")
23
+ end
24
+
25
+ def test_status
26
+ puts @client.status("uid2", "typing", "I am typing")
27
+ end
28
+
29
+ def test_members
30
+ puts @client.members('g1')
31
+ end
32
+
33
+ def test_join
34
+ puts @client.join('g1')
35
+ end
36
+
37
+ def test_leave
38
+ puts @client.leave('g1')
39
+ end
40
+
41
+ def test_offline
42
+ puts @client.offline
43
+ end
44
+
45
+ def teardown
46
+ @client.offline
47
+ end
48
+
49
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webim
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ery Lee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruby webim client library for NexTalk.IM
14
+ email: ery.lee@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/webim.rb
20
+ - test/webim_test.rb
21
+ - LICENSE
22
+ - README.md
23
+ - TODO.md
24
+ - CHANGELOG.md
25
+ homepage: https://github.com/webim/webim-gem
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.0.3
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Ruby webim client library for NexTalk.IM
49
+ test_files: []