pusher-chatkit-server 0.7.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 97b9c094a0c0badebd620b39d97a9c02588be71f5a7e71fbf673956ad4f21ad7
4
- data.tar.gz: f88fd18b50cddb239d9e04c103a4da55d182cb16a39a123400462b2903ec5fd2
2
+ SHA1:
3
+ metadata.gz: fd50747b8046dc072876ab55a6d14fdeff1ced9b
4
+ data.tar.gz: d7cf32176bd955ac6fec9688dc6084848206d5e1
5
5
  SHA512:
6
- metadata.gz: 360a38b3e0cdffa1a2714d084613f6e145976c9daf333a2f122f784d2082e9744b099ef896d73164b97fa7de9d3fc9fb81107ab73fc213312f5aaba8b22c2f92
7
- data.tar.gz: 4107ba5646b2804e3ae5041014984939a6fb20d44233f432698a380458ccf72439ba2cf7383f79471f6c32d284a6b7a8947d21e40c03f6b7ca23c4dcb86a1ee7
6
+ metadata.gz: a3bdffe4d4c45b5b327d37da0d203a4692147172045ae22e857e8052039c7090b8a1dd583bb3a394434063792a7f8dc2f07150e859bf2894676acd54cac432c2
7
+ data.tar.gz: ed6bbf3cb9da4888ebfb9780cb19b30217a86251681fc50817fc45e73b062f5716a5034098b91ddb4d1bcd8715164ccc08cfe3c850b0e7066182ada9f68c2595
@@ -1,15 +1,18 @@
1
1
  require 'pusher-platform'
2
2
  require 'json'
3
+ require 'cgi'
4
+
5
+ require_relative './error'
6
+ require_relative './missing_parameter_error'
7
+ require_relative './response_error'
3
8
 
4
9
  module Chatkit
10
+
5
11
  ROOM_SCOPE = "room"
6
12
  GLOBAL_SCOPE = "global"
7
13
 
8
- class Error < RuntimeError
9
- end
10
-
11
14
  class Client
12
- attr_accessor :api_instance, :authorizer_instance
15
+ attr_accessor :api_instance, :authorizer_instance, :cursors_instance
13
16
 
14
17
  def initialize(options)
15
18
  base_options = {
@@ -17,20 +20,31 @@ module Chatkit
17
20
  key: options[:key],
18
21
  port: options[:port],
19
22
  host: options[:host],
20
- client: options[:client]
23
+ client: options[:client],
24
+ sdk_info: PusherPlatform::SDKInfo.new({
25
+ product_name: 'chatkit',
26
+ version: '0.7.2'
27
+ })
21
28
  }
22
29
 
23
30
  @api_instance = PusherPlatform::Instance.new(
24
- base_options.merge!({
31
+ base_options.merge({
25
32
  service_name: 'chatkit',
26
- service_version: 'v1'
33
+ service_version: 'v2'
27
34
  })
28
35
  )
29
36
 
30
37
  @authorizer_instance = PusherPlatform::Instance.new(
31
- base_options.merge!({
38
+ base_options.merge({
32
39
  service_name: 'chatkit_authorizer',
33
- service_version: 'v1'
40
+ service_version: 'v2'
41
+ })
42
+ )
43
+
44
+ @cursors_instance = PusherPlatform::Instance.new(
45
+ base_options.merge({
46
+ service_name: 'chatkit_cursors',
47
+ service_version: 'v2'
34
48
  })
35
49
  )
36
50
  end
@@ -43,269 +57,658 @@ module Chatkit
43
57
  @api_instance.authenticate(auth_payload, { user_id: user_id })
44
58
  end
45
59
 
46
- def authenticate_with_request(request, options)
47
- @api_instance.authenticate_with_request(request, options)
48
- end
49
-
50
60
  def generate_access_token(options)
61
+ if options.empty?
62
+ raise Chatkit::Error.new("You must provide a either a user_id or `su: true`")
63
+ end
64
+
51
65
  @api_instance.generate_access_token(options)
52
66
  end
53
67
 
54
68
  def generate_su_token(options = {})
55
- generate_access_token({ su: true }.merge(options))[:token]
69
+ generate_access_token({ su: true }.merge(options))
56
70
  end
57
71
 
58
72
  # User API
59
73
 
60
- def create_user(id, name, avatar_url = nil, custom_data = nil)
74
+ def create_user(options)
75
+ if options[:id].nil?
76
+ raise Chatkit::MissingParameterError.new("You must provide an ID for the user you want to create")
77
+ end
78
+
79
+ if options[:name].nil?
80
+ raise Chatkit::MissingParameterError.new("You must provide a name for the user you want to create")
81
+ end
82
+
61
83
  body = {
62
- id: id,
63
- name: name
84
+ id: options[:id],
85
+ name: options[:name]
64
86
  }
65
87
 
66
- unless avatar_url.nil?
67
- body[:avatar_url] = avatar_url
88
+ unless options[:avatar_url].nil?
89
+ body[:avatar_url] = options[:avatar_url]
68
90
  end
69
91
 
70
- unless custom_data.nil?
71
- body[:custom_data] = custom_data
92
+ unless options[:custom_data].nil?
93
+ body[:custom_data] = options[:custom_data]
72
94
  end
73
95
 
74
- @api_instance.request(
96
+ api_request({
75
97
  method: "POST",
76
98
  path: "/users",
77
- headers: {
78
- "Content-Type": "application/json",
79
- },
80
99
  body: body,
81
- jwt: generate_su_token
82
- )
100
+ jwt: generate_su_token[:token]
101
+ })
102
+ end
103
+
104
+ def create_users(options)
105
+ if options[:users].nil?
106
+ raise Chatkit::MissingParameterError.new("You must provide a list of users that you want to create")
107
+ end
108
+
109
+ api_request({
110
+ method: "POST",
111
+ path: "/batch_users",
112
+ body: options,
113
+ jwt: generate_su_token[:token]
114
+ })
115
+ end
116
+
117
+ def update_user(options)
118
+ if options[:id].nil?
119
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the user you want to update")
120
+ end
121
+
122
+ payload = {}
123
+ payload[:name] = options[:name] unless options[:name].nil?
124
+ payload[:avatar_url] = options[:avatar_url] unless options[:avatar_url].nil?
125
+ payload[:custom_data] = options[:custom_data] unless options[:custom_data].nil?
126
+
127
+ api_request({
128
+ method: "PUT",
129
+ path: "/users/#{CGI::escape options[:id]}",
130
+ body: payload,
131
+ jwt: generate_su_token({ user_id: options[:id] })[:token]
132
+ })
83
133
  end
84
134
 
85
- def delete_user(id)
86
- @api_instance.request(
135
+ def delete_user(options)
136
+ if options[:id].nil?
137
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the user you want to delete")
138
+ end
139
+
140
+ api_request({
87
141
  method: "DELETE",
88
- path: "/users/#{id}",
89
- jwt: generate_su_token
90
- )
142
+ path: "/users/#{CGI::escape options[:id]}",
143
+ jwt: generate_su_token[:token]
144
+ })
145
+ end
146
+
147
+ def get_user(options)
148
+ if options[:id].nil?
149
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the user you want to fetch")
150
+ end
151
+
152
+ api_request({
153
+ method: "GET",
154
+ path: "/users/#{CGI::escape options[:id]}",
155
+ jwt: generate_su_token[:token]
156
+ })
91
157
  end
92
158
 
93
- def get_users(from_id)
159
+ def get_users(options = nil)
94
160
  request_options = {
95
161
  method: "GET",
96
162
  path: "/users",
97
- jwt: generate_su_token
163
+ jwt: generate_su_token[:token]
98
164
  }
99
165
 
100
- unless from_id == nil && from_id == ""
101
- request_options.merge!({ query: { from_id: from_id }})
166
+ unless options.nil?
167
+ query = {}
168
+ query[:from_ts] = options[:from_timestamp] unless options[:from_timestamp].nil?
169
+ query[:limit] = options[:limit] unless options[:limit].nil?
170
+
171
+ request_options.merge!({
172
+ query: query
173
+ })
102
174
  end
103
175
 
104
- @api_instance.request(request_options)
176
+ api_request(request_options)
105
177
  end
106
178
 
107
- def get_users_by_ids(user_ids)
108
- @api_instance.request(
179
+ def get_users_by_id(options)
180
+ if options[:user_ids].nil?
181
+ raise Chatkit::MissingParameterError.new("You must provide the IDs of the users you want to fetch")
182
+ end
183
+
184
+ api_request({
109
185
  method: "GET",
110
186
  path: "/users_by_ids",
111
187
  query: {
112
- user_ids: user_ids.join(",")
113
- },
114
- jwt: generate_su_token
115
- )
188
+ id: options[:user_ids],
189
+ },
190
+ jwt: generate_su_token[:token]
191
+ })
116
192
  end
117
193
 
118
194
  # Room API
119
195
 
120
- def get_room(room_id)
121
- @api_instance.request(
122
- method: "GET",
123
- path: "/rooms/#{room_id}",
124
- jwt: generate_su_token
125
- )
196
+ def create_room(options)
197
+ if options[:creator_id].nil?
198
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the user creating the room")
199
+ end
200
+
201
+ if options[:name].nil?
202
+ raise Chatkit::MissingParameterError.new("You must provide a name for the room")
203
+ end
204
+
205
+ body = {
206
+ name: options[:name],
207
+ private: options[:private] || false
208
+ }
209
+
210
+ unless options[:user_ids].nil?
211
+ body[:user_ids] = options[:user_ids]
212
+ end
213
+
214
+ api_request({
215
+ method: "POST",
216
+ path: "/rooms",
217
+ body: body,
218
+ jwt: generate_su_token({ user_id: options[:creator_id] })[:token]
219
+ })
126
220
  end
127
221
 
128
- def get_room_messages(user_id, room_id, initial_id = nil, direction = nil, limit = nil)
129
- query_params = {}
222
+ def update_room(options)
223
+ if options[:id].nil?
224
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the room to update")
225
+ end
226
+
227
+ payload = {}
228
+ payload[:name] = options[:name] unless options[:name].nil?
229
+ payload[:private] = options[:private] unless options[:private].nil?
230
+
231
+ api_request({
232
+ method: "PUT",
233
+ path: "/rooms/#{CGI::escape options[:id]}",
234
+ body: payload,
235
+ jwt: generate_su_token[:token]
236
+ })
237
+ end
130
238
 
131
- query_params[:initial_id] = initial_id unless initial_id.nil?
132
- query_params[:direction] = direction unless direction.nil?
133
- query_params[:limit] = limit unless limit.nil?
239
+ def delete_room(options)
240
+ if options[:id].nil?
241
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the room to delete")
242
+ end
134
243
 
135
- @api_instance.request(
244
+ api_request({
245
+ method: "DELETE",
246
+ path: "/rooms/#{CGI::escape options[:id]}",
247
+ jwt: generate_su_token[:token]
248
+ })
249
+ end
250
+
251
+ def get_room(options)
252
+ if options[:id].nil?
253
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the room to fetch")
254
+ end
255
+
256
+ api_request({
136
257
  method: "GET",
137
- path: "/rooms/#{room_id}/messages",
138
- query: query_params,
139
- jwt: generate_su_token({ user_id: user_id })
140
- )
258
+ path: "/rooms/#{CGI::escape options[:id]}",
259
+ jwt: generate_su_token[:token]
260
+ })
141
261
  end
142
262
 
143
- def get_rooms(user_id, from_id = nil)
263
+ def get_rooms(options = nil)
144
264
  request_options = {
145
265
  method: "GET",
146
266
  path: "/rooms",
147
- jwt: generate_su_token({ user_id: user_id })
267
+ jwt: generate_su_token[:token]
268
+ }
269
+
270
+ unless options.nil?
271
+ query = {}
272
+ query[:include_private] = !options[:include_private].nil? ? options[:include_private] : false
273
+ query[:from_id] = options[:from_id] unless options[:from_id].nil?
274
+
275
+ request_options.merge!({
276
+ query: query
277
+ })
278
+ end
279
+
280
+ api_request(request_options)
281
+ end
282
+
283
+ def get_user_rooms(options)
284
+ get_rooms_for_user(options)
285
+ end
286
+
287
+ def get_user_joinable_rooms(options)
288
+ options[:joinable] = true
289
+ get_rooms_for_user(options)
290
+ end
291
+
292
+ def add_users_to_room(options)
293
+ if options[:room_id].nil?
294
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the room you want to add users to")
295
+ end
296
+
297
+ if options[:user_ids].nil?
298
+ raise Chatkit::MissingParameterError.new("You must provide a list of IDs of the users you want to add to the room")
299
+ end
300
+
301
+ api_request({
302
+ method: "PUT",
303
+ path: "/rooms/#{CGI::escape options[:room_id]}/users/add",
304
+ body: { user_ids: options[:user_ids] },
305
+ jwt: generate_su_token[:token]
306
+ })
307
+ end
308
+
309
+ def remove_users_from_room(options)
310
+ if options[:room_id].nil?
311
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the room you want to remove users from")
312
+ end
313
+
314
+ if options[:user_ids].nil?
315
+ raise Chatkit::MissingParameterError.new("You must provide a list of IDs of the users you want to remove from the room")
316
+ end
317
+
318
+ api_request({
319
+ method: "PUT",
320
+ path: "/rooms/#{CGI::escape options[:room_id]}/users/remove",
321
+ body: { user_ids: options[:user_ids] },
322
+ jwt: generate_su_token[:token]
323
+ })
324
+ end
325
+
326
+ # Messages API
327
+
328
+ def get_room_messages(options)
329
+ if options[:room_id].nil?
330
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the room to fetch messages from")
331
+ end
332
+
333
+ query_params = {}
334
+ query_params[:initial_id] = options[:initial_id] unless options[:initial_id].nil?
335
+ query_params[:direction] = options[:direction] unless options[:direction].nil?
336
+ query_params[:limit] = options[:limit] unless options[:limit].nil?
337
+
338
+ api_request({
339
+ method: "GET",
340
+ path: "/rooms/#{CGI::escape options[:room_id]}/messages",
341
+ query: query_params,
342
+ jwt: generate_su_token[:token]
343
+ })
344
+ end
345
+
346
+ def send_message(options)
347
+ if options[:room_id].nil?
348
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the room to send the message to")
349
+ end
350
+
351
+ if options[:sender_id].nil?
352
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the user sending the message")
353
+ end
354
+
355
+ if options[:text].nil?
356
+ raise Chatkit::MissingParameterError.new("You must provide some text for the message")
357
+ end
358
+
359
+ attachment = options[:attachment]
360
+
361
+ unless attachment.nil?
362
+ if attachment[:resource_link].nil?
363
+ raise Chatkit::MissingParameterError.new("You must provide a resource_link for the message attachment")
364
+ end
365
+
366
+ valid_file_types = ['image', 'video', 'audio', 'file']
367
+
368
+ if attachment[:type].nil? || !valid_file_types.include?(attachment[:type])
369
+ raise Chatkit::MissingParameterError.new(
370
+ "You must provide a valid type for the message attachment, i.e. one of: #{valid_file_types.join(', ')}"
371
+ )
372
+ end
373
+ end
374
+
375
+ payload = {
376
+ text: options[:text],
377
+ attachment: options[:attachment]
148
378
  }
149
379
 
150
- unless from_id == nil && from_id == ""
151
- request_options.merge!({ query: { from_id: from_id }})
380
+ api_request({
381
+ method: "POST",
382
+ path: "/rooms/#{CGI::escape options[:room_id]}/messages",
383
+ body: payload,
384
+ jwt: generate_su_token({ user_id: options[:sender_id] })[:token]
385
+ })
386
+ end
387
+
388
+ def delete_message(options)
389
+ if options[:id].nil?
390
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the message you want to delete")
152
391
  end
153
392
 
154
- @api_instance.request(request_options)
393
+ api_request({
394
+ method: "DELETE",
395
+ path: "/messages/#{options[:id]}",
396
+ jwt: generate_su_token[:token]
397
+ })
155
398
  end
156
399
 
157
- # Authorizer API
400
+ # Roles and permissions API
158
401
 
159
- def create_room_role(name, permissions)
160
- create_role(name, ROOM_SCOPE, permissions)
402
+ def create_global_role(options)
403
+ options[:scope] = GLOBAL_SCOPE
404
+ create_role(options)
161
405
  end
162
406
 
163
- def create_global_role(name, permissions)
164
- create_role(name, GLOBAL_SCOPE, permissions)
407
+ def create_room_role(options)
408
+ options[:scope] = ROOM_SCOPE
409
+ create_role(options)
165
410
  end
166
411
 
167
- def delete_room_role(role_name)
168
- delete_role(role_name, ROOM_SCOPE)
412
+ def delete_global_role(options)
413
+ options[:scope] = GLOBAL_SCOPE
414
+ delete_role(options)
169
415
  end
170
416
 
171
- def delete_global_role(role_name)
172
- delete_role(role_name, GLOBAL_SCOPE)
417
+ def delete_room_role(options)
418
+ options[:scope] = ROOM_SCOPE
419
+ delete_role(options)
173
420
  end
174
421
 
175
- def assign_global_role_to_user(user_id, role_name)
176
- assign_role_to_user(user_id, role_name, nil)
422
+ def assign_global_role_to_user(options)
423
+ assign_role_to_user(options)
177
424
  end
178
425
 
179
- def assign_room_role_to_user(user_id, role_name, room_id)
180
- assign_role_to_user(user_id, role_name, room_id)
426
+ def assign_room_role_to_user(options)
427
+ if options[:room_id].nil?
428
+ raise Chatkit::MissingParameterError.new("You must provide a room ID to assign a room role to a user")
429
+ end
430
+
431
+ assign_role_to_user(options)
181
432
  end
182
433
 
183
434
  def get_roles
184
- resp = @authorizer_instance.request(
435
+ authorizer_request({
185
436
  method: "GET",
186
437
  path: "/roles",
187
- jwt: generate_su_token
188
- )
189
-
190
- JSON.parse(resp.body)
438
+ jwt: generate_su_token[:token]
439
+ })
191
440
  end
192
441
 
193
- def get_user_roles(user_id)
194
- resp = @authorizer_instance.request(
442
+ def get_user_roles(options)
443
+ if options[:user_id].nil?
444
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the user whose roles you want to fetch")
445
+ end
446
+
447
+ authorizer_request({
195
448
  method: "GET",
196
- path: "/users/#{user_id}/roles",
197
- jwt: generate_su_token
198
- )
449
+ path: "/users/#{CGI::escape options[:user_id]}/roles",
450
+ jwt: generate_su_token[:token]
451
+ })
452
+ end
453
+
454
+ def remove_global_role_for_user(options)
455
+ remove_role_for_user(options)
456
+ end
457
+
458
+ def remove_room_role_for_user(options)
459
+ if options[:room_id].nil?
460
+ raise Chatkit::MissingParameterError.new("You must provide a room ID")
461
+ end
462
+
463
+ remove_role_for_user(options)
464
+ end
199
465
 
200
- JSON.parse(resp.body)
466
+ def get_permissions_for_global_role(options)
467
+ options[:scope] = GLOBAL_SCOPE
468
+ get_permissions_for_role(options)
201
469
  end
202
470
 
203
- def remove_global_role_for_user(user_id)
204
- remove_role_for_user(user_id, nil)
471
+ def get_permissions_for_room_role(options)
472
+ options[:scope] = ROOM_SCOPE
473
+ get_permissions_for_role(options)
205
474
  end
206
475
 
207
- def remove_room_role_for_user(user_id, room_id)
208
- remove_role_for_user(user_id, room_id)
476
+ def update_permissions_for_global_role(options)
477
+ options[:scope] = GLOBAL_SCOPE
478
+ update_permissions_for_role(options)
209
479
  end
210
480
 
211
- def get_permissions_for_global_role(role_name)
212
- get_permissions_for_role(role_name, GLOBAL_SCOPE)
481
+ def update_permissions_for_room_role(options)
482
+ options[:scope] = ROOM_SCOPE
483
+ update_permissions_for_role(options)
213
484
  end
214
485
 
215
- def get_permissions_for_room_role(role_name)
216
- get_permissions_for_role(role_name, ROOM_SCOPE)
486
+ # Cursors API
487
+
488
+ def get_read_cursor(options)
489
+ if options[:user_id].nil?
490
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the user whose read cursor you want to fetch")
491
+ end
492
+
493
+ if options[:room_id].nil?
494
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the room that you want the read cursor for")
495
+ end
496
+
497
+ cursors_request({
498
+ method: "GET",
499
+ path: "/cursors/0/rooms/#{CGI::escape options[:room_id]}/users/#{CGI::escape options[:user_id]}",
500
+ jwt: generate_su_token[:token]
501
+ })
217
502
  end
218
503
 
219
- def update_role_permissions(role_name, scope, permissions_to_add = [], permissions_to_remove = [])
220
- if permissions_to_add.empty? && permissions_to_remove.empty?
221
- raise Chatkit::Error.new("permissions_to_add and permissions_to_remove cannot both be empty")
504
+ def set_read_cursor(options)
505
+ if options[:user_id].nil?
506
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the user whose read cursor you want to set")
222
507
  end
223
508
 
224
- body = {}
225
- body[:add_permissions] = permissions_to_add unless permissions_to_add.empty?
226
- body[:remove_permissions] = permissions_to_remove unless permissions_to_remove.empty?
509
+ if options[:room_id].nil?
510
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the room you want to set the user's cursor in")
511
+ end
512
+
513
+ if options[:position].nil?
514
+ raise Chatkit::MissingParameterError.new("You must provide position of the read cursor")
515
+ end
227
516
 
228
- @authorizer_instance.request(
517
+ cursors_request({
229
518
  method: "PUT",
230
- path: "/roles/#{role_name}/scope/#{scope}/permissions",
231
- headers: {
232
- "Content-Type": "application/json"
233
- },
234
- body: body,
235
- jwt: generate_su_token
236
- )
519
+ path: "/cursors/0/rooms/#{CGI::escape options[:room_id]}/users/#{CGI::escape options[:user_id]}",
520
+ body: { position: options[:position] },
521
+ jwt: generate_su_token[:token]
522
+ })
523
+ end
524
+
525
+ def get_user_read_cursors(options)
526
+ if options[:user_id].nil?
527
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the user whose read cursors you want to fetch")
528
+ end
529
+
530
+ cursors_request({
531
+ method: "GET",
532
+ path: "/cursors/0/users/#{CGI::escape options[:user_id]}",
533
+ jwt: generate_su_token[:token]
534
+ })
535
+ end
536
+
537
+ def get_room_read_cursors(options)
538
+ if options[:room_id].nil?
539
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the room that you want the read cursors for")
540
+ end
541
+
542
+ cursors_request({
543
+ method: "GET",
544
+ path: "/cursors/0/rooms/#{CGI::escape options[:room_id]}",
545
+ jwt: generate_su_token[:token]
546
+ })
547
+ end
548
+
549
+ # Service-specific helpers
550
+
551
+ def api_request(options)
552
+ make_request(@api_instance, options)
553
+ end
554
+
555
+ def authorizer_request(options)
556
+ make_request(@authorizer_instance, options)
557
+ end
558
+
559
+ def cursors_request(options)
560
+ make_request(@cursors_instance, options)
237
561
  end
238
562
 
239
563
  private
240
564
 
241
- def create_role(name, scope, permissions)
242
- @authorizer_instance.request(
565
+ def make_request(instance, options)
566
+ options.merge!({ headers: { "Content-Type": "application/json" } })
567
+ begin
568
+ format_response(instance.request(options))
569
+ rescue PusherPlatform::ErrorResponse => e
570
+ raise Chatkit::ResponseError.new(e)
571
+ rescue PusherPlatform::Error => e
572
+ raise Chatkit::Error.new(e.message)
573
+ end
574
+ end
575
+
576
+ def format_response(res)
577
+ body = res.body.empty? ? nil : JSON.parse(res.body, { symbolize_names: true })
578
+
579
+ {
580
+ status: res.status,
581
+ headers: res.headers,
582
+ body: body
583
+ }
584
+ end
585
+
586
+ def get_rooms_for_user(options)
587
+ if options[:id].nil?
588
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the user whose rooms you want to fetch")
589
+ end
590
+
591
+ request_options = {
592
+ method: "GET",
593
+ path: "/users/#{CGI::escape options[:id]}/rooms",
594
+ jwt: generate_su_token[:token]
595
+ }
596
+
597
+ unless options[:joinable].nil?
598
+ request_options.merge!({ query: { joinable: options[:joinable] }})
599
+ end
600
+
601
+ api_request(request_options)
602
+ end
603
+
604
+ def create_role(options)
605
+ if options[:name].nil?
606
+ raise Chatkit::MissingParameterError.new("You must provide a name for the role")
607
+ end
608
+
609
+ if options[:permissions].nil?
610
+ raise Chatkit::MissingParameterError.new("You must provide permissions for the role, even if it's an empty list")
611
+ end
612
+
613
+ authorizer_request({
243
614
  method: "POST",
244
615
  path: "/roles",
245
- headers: {
246
- "Content-Type": "application/json"
247
- },
248
616
  body: {
249
- scope: scope,
250
- name: name,
251
- permissions: permissions,
617
+ scope: options[:scope],
618
+ name: options[:name],
619
+ permissions: options[:permissions],
252
620
  },
253
- jwt: generate_su_token
254
- )
621
+ jwt: generate_su_token[:token]
622
+ })
255
623
  end
256
624
 
257
- def delete_role(role_name, scope)
258
- @authorizer_instance.request(
625
+ def delete_role(options)
626
+ if options[:name].nil?
627
+ raise Chatkit::MissingParameterError.new("You must provide the role's name")
628
+ end
629
+
630
+ authorizer_request({
259
631
  method: "DELETE",
260
- path: "/roles/#{role_name}/scope/#{scope}",
261
- jwt: generate_su_token
262
- )
632
+ path: "/roles/#{CGI::escape options[:name]}/scope/#{options[:scope]}",
633
+ jwt: generate_su_token[:token]
634
+ })
263
635
  end
264
636
 
265
- def assign_role_to_user(user_id, role_name, room_id)
266
- body = { name: role_name }
637
+ def assign_role_to_user(options)
638
+ if options[:name].nil?
639
+ raise Chatkit::MissingParameterError.new("You must provide the role's name")
640
+ end
641
+
642
+ if options[:user_id].nil?
643
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the user you want to assign the role to")
644
+ end
645
+
646
+ body = { name: options[:name] }
267
647
 
268
- unless room_id.nil?
269
- body.merge!(room_id: room_id)
648
+ unless options[:room_id].nil?
649
+ body.merge!(room_id: options[:room_id])
270
650
  end
271
651
 
272
- @authorizer_instance.request(
652
+ authorizer_request({
273
653
  method: "PUT",
274
- path: "/users/#{user_id}/roles",
275
- headers: {
276
- "Content-Type": "application/json",
277
- },
654
+ path: "/users/#{CGI::escape options[:user_id]}/roles",
278
655
  body: body,
279
- jwt: generate_su_token
280
- )
656
+ jwt: generate_su_token[:token]
657
+ })
281
658
  end
282
659
 
283
- def remove_role_for_user(user_id, room_id)
284
- options = {
660
+ def remove_role_for_user(options)
661
+ if options[:user_id].nil?
662
+ raise Chatkit::MissingParameterError.new("You must provide the ID of the user you want to remove the role for")
663
+ end
664
+
665
+ request_options = {
285
666
  method: "DELETE",
286
- path: "/users/#{user_id}/roles",
287
- headers: {
288
- "Content-Type": "application/json",
289
- },
290
- query: { room_id: room_id },
291
- jwt: generate_su_token
667
+ path: "/users/#{CGI::escape options[:user_id]}/roles",
668
+ jwt: generate_su_token[:token]
292
669
  }
293
670
 
294
- unless room_id.nil?
295
- options.merge!(body: { room_id: room_id })
671
+ unless options[:room_id].nil?
672
+ request_options.merge!({ query: { room_id: options[:room_id] }})
296
673
  end
297
674
 
298
- @authorizer_instance.request(options)
675
+ authorizer_request(request_options)
299
676
  end
300
677
 
301
- def get_permissions_for_role(role_name, scope)
302
- resp = @authorizer_instance.request(
678
+ def get_permissions_for_role(options)
679
+ if options[:name].nil?
680
+ raise Chatkit::MissingParameterError.new("You must provide the name of the role you want to fetch the permissions of")
681
+ end
682
+
683
+ authorizer_request({
303
684
  method: "GET",
304
- path: "/roles/#{role_name}/scope/#{scope}/permissions",
305
- jwt: generate_su_token
306
- )
685
+ path: "/roles/#{CGI::escape options[:name]}/scope/#{options[:scope]}/permissions",
686
+ jwt: generate_su_token[:token]
687
+ })
688
+ end
689
+
690
+ def update_permissions_for_role(options)
691
+ if options[:name].nil?
692
+ raise Chatkit::MissingParameterError.new("You must provide the name of the role you want to update the permissions of")
693
+ end
694
+
695
+ permissions_to_add = options[:permissions_to_add]
696
+ permissions_to_remove = options[:permissions_to_remove]
307
697
 
308
- JSON.parse(resp.body)
698
+ if (permissions_to_add.nil? || permissions_to_add.empty?) && (permissions_to_remove.nil? || permissions_to_remove.empty?)
699
+ raise Chatkit::MissingParameterError.new("permissions_to_add and permissions_to_remove cannot both be empty")
700
+ end
701
+
702
+ body = {}
703
+ body[:add_permissions] = permissions_to_add unless permissions_to_add.nil? || permissions_to_add.empty?
704
+ body[:remove_permissions] = permissions_to_remove unless permissions_to_remove.nil? ||permissions_to_remove.empty?
705
+
706
+ authorizer_request({
707
+ method: "PUT",
708
+ path: "/roles/#{CGI::escape options[:name]}/scope/#{options[:scope]}/permissions",
709
+ body: body,
710
+ jwt: generate_su_token[:token]
711
+ })
309
712
  end
310
713
  end
311
714
  end
@@ -0,0 +1,4 @@
1
+ module Chatkit
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,16 @@
1
+ require_relative './error'
2
+
3
+ module Chatkit
4
+ class MissingParameterError < Chatkit::Error
5
+ attr_accessor :message
6
+
7
+ def initialize(message)
8
+ @message = message
9
+ end
10
+
11
+ def to_s
12
+ "Chatkit::MissingParameterError - #{@message}"
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,37 @@
1
+ require_relative './error'
2
+
3
+ module Chatkit
4
+ class ResponseError < Chatkit::Error
5
+ attr_reader :status, :headers, :error_description, :error, :error_uri
6
+
7
+ def initialize(platform_error)
8
+ @status = platform_error.status
9
+ @headers = platform_error.headers
10
+ @error = platform_error.error
11
+ @error_description = platform_error.error_description
12
+ @error_uri = platform_error.error_uri
13
+ end
14
+
15
+ def to_s
16
+ output = "Chatkit::ResponseError - status: #{@status} description: #{@error_description}."
17
+ output += " Find out more at #{@error_uri}" if @error_uri
18
+ output
19
+ end
20
+
21
+ def as_json(options = {})
22
+ json = {
23
+ status: @status,
24
+ headers: @headers,
25
+ error: @error,
26
+ error_description: @error_description,
27
+ }
28
+ json[:error_uri] = @error_uri unless @error_uri.nil?
29
+ json
30
+ end
31
+
32
+ def to_json(*options)
33
+ as_json(*options).to_json(*options)
34
+ end
35
+
36
+ end
37
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pusher-chatkit-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pusher
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-20 00:00:00.000000000 Z
11
+ date: 2018-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pusher-platform
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.8.2
19
+ version: 0.11.2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.8.2
26
+ version: 0.11.2
27
27
  description:
28
28
  email: support@pusher.com
29
29
  executables: []
@@ -32,6 +32,9 @@ extra_rdoc_files: []
32
32
  files:
33
33
  - lib/chatkit.rb
34
34
  - lib/chatkit/client.rb
35
+ - lib/chatkit/error.rb
36
+ - lib/chatkit/missing_parameter_error.rb
37
+ - lib/chatkit/response_error.rb
35
38
  homepage:
36
39
  licenses:
37
40
  - MIT
@@ -52,7 +55,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
55
  version: '0'
53
56
  requirements: []
54
57
  rubyforge_project:
55
- rubygems_version: 2.7.7
58
+ rubygems_version: 2.5.2.3
56
59
  signing_key:
57
60
  specification_version: 4
58
61
  summary: Pusher Chatkit Ruby SDK