pusher-chatkit-server 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/chatkit/client.rb +149 -5
- data/lib/chatkit/upload_error.rb +17 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f132c6890c4c1b9c7ca4230ba21170ec33279c8dddc1d8a8536bf7210e5f4bcb
|
4
|
+
data.tar.gz: 4fa2791c7b84d65494cd71554dfda3bff2d2eec030cda828bc6dd1647d235a16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60220ad10c2e728fc8e2cf7fb902c79226707a992c5ccabcc1ade0387af24accdaa9787ae3b020116f762d8d82d33ffc6dfb3b21f21716c493d78345d2a1e185
|
7
|
+
data.tar.gz: c3c01e8ed88377b3b0fc494bc3ddc12f13b63479b043675906b61b46a495e1b2307f61c3d54e09e314b9c781e5187fb77dc8cfabc7b70caf3d367e37d580f268
|
data/lib/chatkit/client.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
require 'pusher-platform'
|
2
2
|
require 'json'
|
3
3
|
require 'cgi'
|
4
|
+
require 'excon'
|
4
5
|
|
5
6
|
require_relative './error'
|
6
7
|
require_relative './missing_parameter_error'
|
7
8
|
require_relative './response_error'
|
9
|
+
require_relative './upload_error'
|
8
10
|
|
9
11
|
module Chatkit
|
10
12
|
|
@@ -12,7 +14,10 @@ module Chatkit
|
|
12
14
|
GLOBAL_SCOPE = "global"
|
13
15
|
|
14
16
|
class Client
|
15
|
-
attr_accessor :api_instance,
|
17
|
+
attr_accessor :api_instance,
|
18
|
+
:api_v2_instance,
|
19
|
+
:authorizer_instance,
|
20
|
+
:cursors_instance
|
16
21
|
|
17
22
|
def initialize(options)
|
18
23
|
base_options = {
|
@@ -27,13 +32,20 @@ module Chatkit
|
|
27
32
|
})
|
28
33
|
}
|
29
34
|
|
30
|
-
@
|
35
|
+
@api_v2_instance = PusherPlatform::Instance.new(
|
31
36
|
base_options.merge({
|
32
37
|
service_name: 'chatkit',
|
33
38
|
service_version: 'v2'
|
34
39
|
})
|
35
40
|
)
|
36
41
|
|
42
|
+
@api_instance = PusherPlatform::Instance.new(
|
43
|
+
base_options.merge({
|
44
|
+
service_name: 'chatkit',
|
45
|
+
service_version: 'v3'
|
46
|
+
})
|
47
|
+
)
|
48
|
+
|
37
49
|
@authorizer_instance = PusherPlatform::Instance.new(
|
38
50
|
base_options.merge({
|
39
51
|
service_name: 'chatkit_authorizer',
|
@@ -206,7 +218,7 @@ module Chatkit
|
|
206
218
|
name: options[:name],
|
207
219
|
private: options[:private] || false
|
208
220
|
}
|
209
|
-
|
221
|
+
|
210
222
|
body[:custom_data] = options[:custom_data] unless options[:custom_data].nil?
|
211
223
|
|
212
224
|
unless options[:user_ids].nil?
|
@@ -328,6 +340,26 @@ module Chatkit
|
|
328
340
|
|
329
341
|
# Messages API
|
330
342
|
|
343
|
+
def fetch_multipart_messages(options)
|
344
|
+
verify({
|
345
|
+
room_id: "You must provide the ID of the room to send the message to",
|
346
|
+
}, options)
|
347
|
+
|
348
|
+
if !options[:limit].nil? and options[:limit] <= 0
|
349
|
+
raise Chatkit::MissingParameterError.new("Limit must be greater than 0")
|
350
|
+
end
|
351
|
+
|
352
|
+
optional_params = [:initial_id, :direction, :limit]
|
353
|
+
query_params = options.select { |key,_| optional_params.include? key }
|
354
|
+
|
355
|
+
api_request({
|
356
|
+
method: "GET",
|
357
|
+
path: "/rooms/#{CGI::escape options[:room_id]}/messages",
|
358
|
+
query: query_params,
|
359
|
+
jwt: generate_su_token[:token]
|
360
|
+
})
|
361
|
+
end
|
362
|
+
|
331
363
|
def get_room_messages(options)
|
332
364
|
if options[:room_id].nil?
|
333
365
|
raise Chatkit::MissingParameterError.new("You must provide the ID of the room to fetch messages from")
|
@@ -338,7 +370,7 @@ module Chatkit
|
|
338
370
|
query_params[:direction] = options[:direction] unless options[:direction].nil?
|
339
371
|
query_params[:limit] = options[:limit] unless options[:limit].nil?
|
340
372
|
|
341
|
-
|
373
|
+
api_v2_request({
|
342
374
|
method: "GET",
|
343
375
|
path: "/rooms/#{CGI::escape options[:room_id]}/messages",
|
344
376
|
query: query_params,
|
@@ -346,6 +378,65 @@ module Chatkit
|
|
346
378
|
})
|
347
379
|
end
|
348
380
|
|
381
|
+
def send_simple_message(options)
|
382
|
+
verify({text: "You must provide some text for the message",
|
383
|
+
}, options)
|
384
|
+
|
385
|
+
options[:parts] = [{type: "text/plain",
|
386
|
+
content: options[:text]
|
387
|
+
}]
|
388
|
+
|
389
|
+
send_multipart_message(options)
|
390
|
+
end
|
391
|
+
|
392
|
+
def send_multipart_message(options)
|
393
|
+
verify({
|
394
|
+
room_id: "You must provide the ID of the room to send the message to",
|
395
|
+
sender_id: "You must provide the ID of the user sending the message",
|
396
|
+
parts: "You must provide a parts array",
|
397
|
+
}, options)
|
398
|
+
|
399
|
+
if not options[:parts].length > 0
|
400
|
+
raise Chatkit::MissingParameterError.new("parts array must have at least one item")
|
401
|
+
end
|
402
|
+
|
403
|
+
# this assumes the token lives long enough to finish all S3 uploads
|
404
|
+
token = generate_su_token({ user_id: options[:sender_id] })[:token]
|
405
|
+
|
406
|
+
request_parts = options[:parts].map { |part|
|
407
|
+
verify({type: "Each part must define a type"}, part)
|
408
|
+
|
409
|
+
if !part[:content].nil?
|
410
|
+
{
|
411
|
+
type: part[:type],
|
412
|
+
content: part[:content]
|
413
|
+
}
|
414
|
+
elsif !part[:url].nil?
|
415
|
+
{
|
416
|
+
type: part[:type],
|
417
|
+
url: part[:url]
|
418
|
+
}
|
419
|
+
elsif !part[:file].nil?
|
420
|
+
attachment_id = upload_attachment(token, options[:room_id], part)
|
421
|
+
{
|
422
|
+
type: part[:type],
|
423
|
+
attachment: {id: attachment_id},
|
424
|
+
name: part[:name],
|
425
|
+
customData: part[:customData]
|
426
|
+
}.reject{ |_,v| v.nil? }
|
427
|
+
else
|
428
|
+
raise Chatkit::MissingParameterError.new("Each part must have one of :file, :content or :url")
|
429
|
+
end
|
430
|
+
}
|
431
|
+
|
432
|
+
api_request({
|
433
|
+
method: "POST",
|
434
|
+
path: "/rooms/#{CGI::escape options[:room_id]}/messages",
|
435
|
+
body: {parts: request_parts},
|
436
|
+
jwt: token
|
437
|
+
})
|
438
|
+
end
|
439
|
+
|
349
440
|
def send_message(options)
|
350
441
|
if options[:room_id].nil?
|
351
442
|
raise Chatkit::MissingParameterError.new("You must provide the ID of the room to send the message to")
|
@@ -380,7 +471,7 @@ module Chatkit
|
|
380
471
|
attachment: options[:attachment]
|
381
472
|
}
|
382
473
|
|
383
|
-
|
474
|
+
api_v2_request({
|
384
475
|
method: "POST",
|
385
476
|
path: "/rooms/#{CGI::escape options[:room_id]}/messages",
|
386
477
|
body: payload,
|
@@ -551,6 +642,10 @@ module Chatkit
|
|
551
642
|
|
552
643
|
# Service-specific helpers
|
553
644
|
|
645
|
+
def api_v2_request(options)
|
646
|
+
make_request(@api_v2_instance, options)
|
647
|
+
end
|
648
|
+
|
554
649
|
def api_request(options)
|
555
650
|
make_request(@api_instance, options)
|
556
651
|
end
|
@@ -713,5 +808,54 @@ module Chatkit
|
|
713
808
|
jwt: generate_su_token[:token]
|
714
809
|
})
|
715
810
|
end
|
811
|
+
|
812
|
+
def upload_attachment(token, room_id, part)
|
813
|
+
body = part[:file]
|
814
|
+
content_length = body.length
|
815
|
+
content_type = part[:type]
|
816
|
+
|
817
|
+
if content_length <= 0
|
818
|
+
raise Chatkit::MissingParameterError.new("File contents size must be greater than 0")
|
819
|
+
end
|
820
|
+
|
821
|
+
attachment_req = {
|
822
|
+
content_type: content_type,
|
823
|
+
content_length: content_length
|
824
|
+
}
|
825
|
+
|
826
|
+
attachment_response = api_request({
|
827
|
+
method: "POST",
|
828
|
+
path: "/rooms/#{CGI::escape room_id}/attachments",
|
829
|
+
body: attachment_req,
|
830
|
+
jwt: token
|
831
|
+
})
|
832
|
+
|
833
|
+
url = attachment_response[:body][:upload_url]
|
834
|
+
connection = Excon.new(url, :omit_default_port => true)
|
835
|
+
upload_response = connection.put(
|
836
|
+
:body => body,
|
837
|
+
:headers => {
|
838
|
+
"Content-Type" => content_type,
|
839
|
+
"Content-Length" => content_length
|
840
|
+
}
|
841
|
+
)
|
842
|
+
|
843
|
+
if upload_response.status != 200
|
844
|
+
error = {message: "Failed to upload attachment",
|
845
|
+
response_object: upload_response
|
846
|
+
}
|
847
|
+
raise Chatkit::UploadError.new(error)
|
848
|
+
end
|
849
|
+
|
850
|
+
attachment_response[:body][:attachment_id]
|
851
|
+
end
|
852
|
+
|
853
|
+
def verify(required, options)
|
854
|
+
required.each { |field_name, message|
|
855
|
+
if options[field_name].nil?
|
856
|
+
raise Chatkit::MissingParameterError.new(message)
|
857
|
+
end
|
858
|
+
}
|
859
|
+
end
|
716
860
|
end
|
717
861
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative './error'
|
2
|
+
|
3
|
+
module Chatkit
|
4
|
+
class UploadError < Chatkit::Error
|
5
|
+
attr_accessor :message, :response_object
|
6
|
+
|
7
|
+
def initialize(error)
|
8
|
+
@message = error[:message]
|
9
|
+
@response_object = error[:response_object]
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_s
|
13
|
+
"Chatkit::MissingParameterError - #{@message}"
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
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: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pusher
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-03-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pusher-platform
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- lib/chatkit/error.rb
|
36
36
|
- lib/chatkit/missing_parameter_error.rb
|
37
37
|
- lib/chatkit/response_error.rb
|
38
|
+
- lib/chatkit/upload_error.rb
|
38
39
|
homepage:
|
39
40
|
licenses:
|
40
41
|
- MIT
|
@@ -55,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
56
|
version: '0'
|
56
57
|
requirements: []
|
57
58
|
rubyforge_project:
|
58
|
-
rubygems_version: 2.6
|
59
|
+
rubygems_version: 2.7.6
|
59
60
|
signing_key:
|
60
61
|
specification_version: 4
|
61
62
|
summary: Pusher Chatkit Ruby SDK
|