Class: Webim::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/webim.rb

Overview

A Ruby webim client library for NexTalk.im.

Constant Summary collapse

DEFAULT_PORT =

Default port.

8000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, ticket = "", config = {}) ⇒ Client

创建Client对象

user: 用户Hash对象ticket: 通信令牌

config配置参数:

domain: 


73
74
75
76
77
78
79
80
81
# File 'lib/webim.rb', line 73

def initialize(user, ticket = "", config = {})
  @user = user
  @ticket = ticket
  @domain = config[:domain] || ""
  @apikey = config[:apikey] || ""
  @host = config[:host] || "127.0.0.1"
  @port = (config[:port] || DEFAULT_PORT).to_i
    @timeout = (config[:timeout] || 10 ).to_i
end

Instance Attribute Details

#apikeyObject (readonly)

通信APIKEY



40
41
42
# File 'lib/webim.rb', line 40

def apikey
  @apikey
end

#domainObject (readonly)

网站域名



35
36
37
# File 'lib/webim.rb', line 35

def domain
  @domain
end

#hostObject (readonly)

消息服务器地址



45
46
47
# File 'lib/webim.rb', line 45

def host
  @host
end

#portObject (readonly)

消息服务器端口



50
51
52
# File 'lib/webim.rb', line 50

def port
  @port
end

#ticketObject (readonly)

消息服务器通信令牌



55
56
57
# File 'lib/webim.rb', line 55

def ticket
  @ticket
end

#timeoutObject (readonly)

HTTP Timeout



59
60
61
# File 'lib/webim.rb', line 59

def timeout
  @timeout
end

#userObject (readonly)

用户Hash对象

属性:

id: 


30
31
32
# File 'lib/webim.rb', line 30

def user
  @user
end

Instance Method Details

#inspectObject

Clien对象描述



290
291
292
293
# File 'lib/webim.rb', line 290

def inspect
  "<Webim::Client: ticket=%s, domain=%s, apikey=%s, host=%s , port=%p>" %
    [@ticket, @domain, @apikey, @host, @port]
end

#join(gid) ⇒ Object

转发加入群组消息

gid: 


256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/webim.rb', line 256

def join(gid)
  res = http_post('/group/join', {
    :version => APIVSN,
    :ticket => @ticket,
    :apikey => @apikey,
    :domain => @domain,
    :nick => @user[:nick],
    :group => gid 
  })
  if res and (200 == res.code.to_i)
    JSON.parse(res.body)
  else 
    nil
  end
end

#leave(gid) ⇒ Object

转发离开群组消息

gid: 


277
278
279
280
281
282
283
284
285
286
# File 'lib/webim.rb', line 277

def leave(gid)
  http_post('/group/leave', {
    :version => APIVSN,
    :ticket => @ticket,
    :apikey => @apikey,
    :domain => @domain,
    :nick => @user[:nick],
    :group => gid 
  })
end

#members(gid) ⇒ Object

从消息服务器读取当前群组在线用户

gid: 


236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/webim.rb', line 236

def members(gid)
  res = http_get('/group/members', {
    :version => APIVSN,
    :ticket => @ticket,
    :apikey => @apikey,
    :domain => @domain,
    :group => gid
  })
  if res and (200 == res.code.to_i)
    JSON.parse(res.body)
else
 nil
  end
end

#message(to, body, timestamp = nil, type = "chat", style = "") ⇒ Object

type: chat | grpchat

style: 


152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/webim.rb', line 152

def message(to, body, timestamp = nil, type = "chat", style = "")
  http_post('/messages', {
    :version => APIVSN,
    :ticket => @ticket,
    :apikey => @apikey,
    :domain => @domain,
    :nick => @user[:nick],
    :type => type,
    :to => to,
    :body => body,
    :style => style,
    :timestamp => timestamp ? timestamp : Time.now.to_f * 1000
  })
end

#offlineObject

向消息服务器转发用户下线消息



116
117
118
119
120
121
122
123
# File 'lib/webim.rb', line 116

def offline
  http_post('/presences/offline', {
    :version => APIVSN,
    :ticket => @ticket,
    :apikey => @apikey,
    :domain => @domain
  })
end

#online(buddies, groups) ⇒ Object

向消息服务器转发用户上线消息,同时发送用户的好友id列表、群组id列表

buddies: 


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/webim.rb', line 89

def online(buddies, groups)
  res = http_post('/presences/online', {
    :version => APIVSN,
    :buddies => buddies.join(','),
    :groups => groups.join(','),
    :domain => @domain,
    :apikey => @apikey,
    :name => @user[:id],
    :nick => @user[:nick],
    :show => @user[:show],
    :status => @user[:status]
  })
  if res and 200 == res.code.to_i
    data = JSON.parse(res.body)
    @ticket = data["ticket"]
    return data
  end
  return {
    :success => false,
    :error_code => res ? res.code : -1,
    :error_msg => res ? res.body : ""
  }
end

#presence(show, status = "") ⇒ Object

向消息服务器转发用户现场变化

show: 'away' | 'chat' | 'dnd' | 'invisible'
status: 


131
132
133
134
135
136
137
138
139
140
141
# File 'lib/webim.rb', line 131

def presence(show, status = "")
  http_post('/presences/show', {
    :version => APIVSN,
    :ticket => @ticket,
    :apikey => @apikey,
    :domain => @domain,
    :nick => @user[:nick],
    :show => show,
    :status => status
  })
end

#presences(ids) ⇒ Object

从消息服务器读取当前在线用户

ids: 


216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/webim.rb', line 216

def presences(ids)
     res = http_get('/presences', {
       :version => APIVSN,
       :ticket => @ticket,
       :apikey => @apikey,
       :domain => @domain,
       :ids => ids.join(",")
     })
     if res and (200 == res.code.to_i)
       JSON.parse(res.body)
  else
  nil
  end
end

#push(from, to, body, timestamp = nil, type = "chat", style = "") ⇒ Object

type: chat | grpchat

style: 


176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/webim.rb', line 176

def push(from, to, body, timestamp = nil, type = "chat", style = "")
  http_post('/messages', {
    :version => APIVSN,
    :apikey => @apikey,
    :domain => @domain,
    :nick => @user[:nick],
    :type => type,
    :from => from,
    :to => to,
    :body => body,
    :style => style,
    :timestamp => timestamp ? timestamp : Time.now.to_f * 1000
  })
end

#status(to, show, status = "") ⇒ Object

向消息服务器转发用户状态,例如正在输入…

to: 


198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/webim.rb', line 198

def status(to, show, status="")
  http_post('/statuses', {
    :version => APIVSN,
    :ticket => @ticket,
    :apikey => @apikey,
    :domain => @domain,
    :nick => @user[:nick],
    :to => to,
    :show => show,
    :status => status
  })
end