Class: Chatkit::Client
- Inherits:
-
Object
- Object
- Chatkit::Client
- Defined in:
- lib/chatkit/client.rb
Instance Attribute Summary collapse
-
#api_instance ⇒ Object
Returns the value of attribute api_instance.
-
#api_v2_instance ⇒ Object
Returns the value of attribute api_v2_instance.
-
#authorizer_instance ⇒ Object
Returns the value of attribute authorizer_instance.
-
#cursors_instance ⇒ Object
Returns the value of attribute cursors_instance.
Instance Method Summary collapse
- #add_users_to_room(options) ⇒ Object
- #api_request(options) ⇒ Object
-
#api_v2_request(options) ⇒ Object
Service-specific helpers.
- #assign_global_role_to_user(options) ⇒ Object
- #assign_room_role_to_user(options) ⇒ Object
- #async_delete_room(options) ⇒ Object
- #async_delete_user(options) ⇒ Object
- #authenticate(options) ⇒ Object
- #authorizer_request(options) ⇒ Object
-
#create_global_role(options) ⇒ Object
Roles and permissions API.
-
#create_room(options) ⇒ Object
Room API.
- #create_room_role(options) ⇒ Object
-
#create_user(options) ⇒ Object
User API.
- #create_users(options) ⇒ Object
- #cursors_request(options) ⇒ Object
- #delete_global_role(options) ⇒ Object
- #delete_message(options) ⇒ Object
- #delete_room_role(options) ⇒ Object
- #edit_message(room_id, message_id, options) ⇒ Object
- #edit_multipart_message(room_id, message_id, options) ⇒ Object
- #edit_simple_message(room_id, message_id, options) ⇒ Object
-
#fetch_multipart_message(options) ⇒ Object
Messages API.
- #fetch_multipart_messages(options) ⇒ Object
- #generate_access_token(options) ⇒ Object
- #generate_su_token(options = {}) ⇒ Object
- #get_delete_status(options) ⇒ Object
- #get_permissions_for_global_role(options) ⇒ Object
- #get_permissions_for_room_role(options) ⇒ Object
-
#get_read_cursor(options) ⇒ Object
Cursors API.
- #get_roles ⇒ Object
- #get_room(options) ⇒ Object
- #get_room_messages(options) ⇒ Object
- #get_room_read_cursors(options) ⇒ Object
- #get_rooms(options = nil) ⇒ Object
- #get_user(options) ⇒ Object
- #get_user_joinable_rooms(options) ⇒ Object
- #get_user_read_cursors(options) ⇒ Object
- #get_user_roles(options) ⇒ Object
- #get_user_rooms(options) ⇒ Object
- #get_users(options = nil) ⇒ Object
- #get_users_by_id(options) ⇒ Object
-
#initialize(options) ⇒ Client
constructor
A new instance of Client.
- #remove_global_role_for_user(options) ⇒ Object
- #remove_room_role_for_user(options) ⇒ Object
- #remove_users_from_room(options) ⇒ Object
- #scheduler_request(options) ⇒ Object
- #send_message(options) ⇒ Object
- #send_multipart_message(options) ⇒ Object
- #send_simple_message(options) ⇒ Object
- #set_read_cursor(options) ⇒ Object
- #update_permissions_for_global_role(options) ⇒ Object
- #update_permissions_for_room_role(options) ⇒ Object
- #update_room(options) ⇒ Object
- #update_user(options) ⇒ Object
-
#url_encode_path_segment(s) ⇒ Object
This helper should be used to encode parameters that appear in path segments.
Constructor Details
#initialize(options) ⇒ Client
Returns a new instance of Client.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/chatkit/client.rb', line 22 def initialize() = { locator: [:instance_locator], key: [:key], port: [:port], host: [:host], client: [:client], sdk_info: PusherPlatform::SDKInfo.new({ product_name: 'chatkit', version: '0.7.2' }) } @api_v2_instance = PusherPlatform::Instance.new( .merge({ service_name: 'chatkit', service_version: 'v2' }) ) @api_instance = PusherPlatform::Instance.new( .merge({ service_name: 'chatkit', service_version: 'v6' }) ) @authorizer_instance = PusherPlatform::Instance.new( .merge({ service_name: 'chatkit_authorizer', service_version: 'v2' }) ) @cursors_instance = PusherPlatform::Instance.new( .merge({ service_name: 'chatkit_cursors', service_version: 'v2' }) ) @scheduler_instance = PusherPlatform::Instance.new( .merge({ service_name: 'chatkit_scheduler', service_version: 'v1' }) ) end |
Instance Attribute Details
#api_instance ⇒ Object
Returns the value of attribute api_instance.
17 18 19 |
# File 'lib/chatkit/client.rb', line 17 def api_instance @api_instance end |
#api_v2_instance ⇒ Object
Returns the value of attribute api_v2_instance.
17 18 19 |
# File 'lib/chatkit/client.rb', line 17 def api_v2_instance @api_v2_instance end |
#authorizer_instance ⇒ Object
Returns the value of attribute authorizer_instance.
17 18 19 |
# File 'lib/chatkit/client.rb', line 17 def @authorizer_instance end |
#cursors_instance ⇒ Object
Returns the value of attribute cursors_instance.
17 18 19 |
# File 'lib/chatkit/client.rb', line 17 def cursors_instance @cursors_instance end |
Instance Method Details
#add_users_to_room(options) ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/chatkit/client.rb', line 336 def add_users_to_room() if [:room_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the room you want to add users to") end if [:user_ids].nil? raise Chatkit::MissingParameterError.new("You must provide a list of IDs of the users you want to add to the room") end api_request({ method: "PUT", path: "/rooms/#{url_encode_path_segment [:room_id]}/users/add", body: { user_ids: [:user_ids] }, jwt: generate_su_token[:token] }) end |
#api_request(options) ⇒ Object
816 817 818 |
# File 'lib/chatkit/client.rb', line 816 def api_request() make_request(@api_instance, ) end |
#api_v2_request(options) ⇒ Object
Service-specific helpers
812 813 814 |
# File 'lib/chatkit/client.rb', line 812 def api_v2_request() make_request(@api_v2_instance, ) end |
#assign_global_role_to_user(options) ⇒ Object
683 684 685 |
# File 'lib/chatkit/client.rb', line 683 def assign_global_role_to_user() assign_role_to_user() end |
#assign_room_role_to_user(options) ⇒ Object
687 688 689 690 691 692 693 |
# File 'lib/chatkit/client.rb', line 687 def assign_room_role_to_user() if [:room_id].nil? raise Chatkit::MissingParameterError.new("You must provide a room ID to assign a room role to a user") end assign_role_to_user() end |
#async_delete_room(options) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/chatkit/client.rb', line 283 def async_delete_room() if [:id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the room to delete") end scheduler_request({ method: "PUT", path: "/rooms/#{url_encode_path_segment [:id]}", jwt: generate_su_token[:token] }) end |
#async_delete_user(options) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/chatkit/client.rb', line 161 def async_delete_user() if [:id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user you want to delete") end scheduler_request({ method: "PUT", path: "/users/#{url_encode_path_segment [:id]}", jwt: generate_su_token[:token] }) end |
#authenticate(options) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/chatkit/client.rb', line 71 def authenticate() user_id = ['user_id'] || [:user_id] auth_payload = ['auth_payload'] || [:auth_payload] || { grant_type: 'client_credentials' } @api_instance.authenticate(auth_payload, { user_id: user_id }) end |
#authorizer_request(options) ⇒ Object
820 821 822 |
# File 'lib/chatkit/client.rb', line 820 def () make_request(@authorizer_instance, ) end |
#create_global_role(options) ⇒ Object
Roles and permissions API
663 664 665 666 |
# File 'lib/chatkit/client.rb', line 663 def create_global_role() [:scope] = GLOBAL_SCOPE create_role() end |
#create_room(options) ⇒ Object
Room API
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/chatkit/client.rb', line 234 def create_room() if [:creator_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user creating the room") end if [:name].nil? raise Chatkit::MissingParameterError.new("You must provide a name for the room") end body = { name: [:name], private: [:private] || false } body[:id] = [:id] unless [:id].nil? body[:push_notification_title_override] = [:push_notification_title_override] unless [:push_notification_title_override].nil? body[:custom_data] = [:custom_data] unless [:custom_data].nil? unless [:user_ids].nil? body[:user_ids] = [:user_ids] end api_request({ method: "POST", path: "/rooms", body: body, jwt: generate_su_token({ user_id: [:creator_id] })[:token] }) end |
#create_room_role(options) ⇒ Object
668 669 670 671 |
# File 'lib/chatkit/client.rb', line 668 def create_room_role() [:scope] = ROOM_SCOPE create_role() end |
#create_user(options) ⇒ Object
User API
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/chatkit/client.rb', line 100 def create_user() if [:id].nil? raise Chatkit::MissingParameterError.new("You must provide an ID for the user you want to create") end if [:name].nil? raise Chatkit::MissingParameterError.new("You must provide a name for the user you want to create") end body = { id: [:id], name: [:name] } unless [:avatar_url].nil? body[:avatar_url] = [:avatar_url] end unless [:custom_data].nil? body[:custom_data] = [:custom_data] end api_request({ method: "POST", path: "/users", body: body, jwt: generate_su_token[:token] }) end |
#create_users(options) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/chatkit/client.rb', line 130 def create_users() if [:users].nil? raise Chatkit::MissingParameterError.new("You must provide a list of users that you want to create") end api_request({ method: "POST", path: "/batch_users", body: , jwt: generate_su_token[:token] }) end |
#cursors_request(options) ⇒ Object
824 825 826 |
# File 'lib/chatkit/client.rb', line 824 def cursors_request() make_request(@cursors_instance, ) end |
#delete_global_role(options) ⇒ Object
673 674 675 676 |
# File 'lib/chatkit/client.rb', line 673 def delete_global_role() [:scope] = GLOBAL_SCOPE delete_role() end |
#delete_message(options) ⇒ Object
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
# File 'lib/chatkit/client.rb', line 525 def () if [:message_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the message you want to delete") end if [:room_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the room to which the message belongs") end api_request({ method: "DELETE", path: "/rooms/#{[:room_id]}/messages/#{[:message_id]}", jwt: generate_su_token[:token] }) end |
#delete_room_role(options) ⇒ Object
678 679 680 681 |
# File 'lib/chatkit/client.rb', line 678 def delete_room_role() [:scope] = ROOM_SCOPE delete_role() end |
#edit_message(room_id, message_id, options) ⇒ Object
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
# File 'lib/chatkit/client.rb', line 607 def (room_id, , ) if room_id.nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the room in which the message to edit belongs") end if .nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the message to edit") end if [:sender_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user editing the message") end if [:text].nil? raise Chatkit::MissingParameterError.new("You must provide some text for the message") end if !['room_id'].nil? raise Chatkit::UnexpectedParameterError.new("a messages room id cannot be edited") end if !['message_id'].nil? raise Chatkit::UnexpectedParameterError.new("a messages id cannot be edited") end = [:attachment] unless .nil? if [:resource_link].nil? raise Chatkit::MissingParameterError.new("You must provide a resource_link for the message attachment") end valid_file_types = ['image', 'video', 'audio', 'file'] if [:type].nil? || !valid_file_types.include?([:type]) raise Chatkit::MissingParameterError.new( "You must provide a valid type for the message attachment, i.e. one of: #{valid_file_types.join(', ')}" ) end end payload = { text: [:text], attachment: [:attachment] } api_v2_request({ method: "PUT", path: "/rooms/#{url_encode_path_segment room_id}/messages/#{}", body: payload, jwt: generate_su_token({ user_id: [:sender_id] })[:token] }) end |
#edit_multipart_message(room_id, message_id, options) ⇒ Object
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 |
# File 'lib/chatkit/client.rb', line 552 def (room_id, , ) verify({ sender_id: "You must provide the ID of the user editing the message", parts: "You must provide a parts array", }, ) verify({ room_id: "You must provide the ID of the room in which the message to edit belongs", message_id: "You must provide the ID of the message to edit", }, {room_id: room_id, message_id: }) if !['room_id'].nil? raise Chatkit::UnexpectedParameterError.new("a messages room id cannot be edited") end if !['message_id'].nil? raise Chatkit::UnexpectedParameterError.new("a messages id cannot be edited") end if not [:parts].length > 0 raise Chatkit::MissingParameterError.new("parts array must have at least one item") end # this assumes the token lives long enough to finish all S3 uploads token = generate_su_token({ user_id: [:sender_id] })[:token] request_parts = [:parts].map { |part| verify({type: "Each part must define a type"}, part) if !part[:content].nil? { type: part[:type], content: part[:content] } elsif !part[:url].nil? { type: part[:type], url: part[:url] } elsif !part[:file].nil? = (token, room_id, part) { type: part[:type], attachment: {id: }, }.reject{ |_,v| v.nil? } else raise Chatkit::MissingParameterError.new("Each part must have one of :file, :content or :url") end } api_request({ method: "PUT", path: "/rooms/#{url_encode_path_segment room_id}/messages/#{}", body: {parts: request_parts}, jwt: token }) end |
#edit_simple_message(room_id, message_id, options) ⇒ Object
541 542 543 544 545 546 547 548 549 550 |
# File 'lib/chatkit/client.rb', line 541 def (room_id, , ) verify({text: "You must provide some text for the message", }, ) [:parts] = [{type: "text/plain", content: [:text] }] (room_id, , ) end |
#fetch_multipart_message(options) ⇒ Object
Messages API
372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/chatkit/client.rb', line 372 def () verify({ room_id: "You must provide the ID of the room to fetch the message from", message_id: "You must provide the message ID" }, ) api_request({ method: "GET", path: "/rooms/#{url_encode_path_segment [:room_id]}/messages/#{[:message_id]}", jwt: generate_su_token[:token] }) end |
#fetch_multipart_messages(options) ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/chatkit/client.rb', line 388 def () verify({ room_id: "You must provide the ID of the room to fetch the messages from", }, ) if ![:limit].nil? and [:limit] <= 0 raise Chatkit::MissingParameterError.new("Limit must be greater than 0") end optional_params = [:initial_id, :direction, :limit] query_params = .select { |key,_| optional_params.include? key } api_request({ method: "GET", path: "/rooms/#{url_encode_path_segment [:room_id]}/messages", query: query_params, jwt: generate_su_token[:token] }) end |
#generate_access_token(options) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/chatkit/client.rb', line 79 def generate_access_token() if .empty? raise Chatkit::Error.new("You must provide a either a user_id or `su: true`") end @api_instance.generate_access_token() end |
#generate_su_token(options = {}) ⇒ Object
87 88 89 |
# File 'lib/chatkit/client.rb', line 87 def generate_su_token( = {}) generate_access_token({ su: true }.merge()) end |
#get_delete_status(options) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/chatkit/client.rb', line 173 def get_delete_status() if [:id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the job you want to query status of") end scheduler_request({ method: "GET", path: "/status/#{url_encode_path_segment [:id]}", jwt: generate_su_token[:token] }) end |
#get_permissions_for_global_role(options) ⇒ Object
727 728 729 730 |
# File 'lib/chatkit/client.rb', line 727 def () [:scope] = GLOBAL_SCOPE () end |
#get_permissions_for_room_role(options) ⇒ Object
732 733 734 735 |
# File 'lib/chatkit/client.rb', line 732 def () [:scope] = ROOM_SCOPE () end |
#get_read_cursor(options) ⇒ Object
Cursors API
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 |
# File 'lib/chatkit/client.rb', line 749 def get_read_cursor() if [:user_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user whose read cursor you want to fetch") end if [:room_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the room that you want the read cursor for") end cursors_request({ method: "GET", path: "/cursors/0/rooms/#{url_encode_path_segment [:room_id]}/users/#{url_encode_path_segment [:user_id]}", jwt: generate_su_token[:token] }) end |
#get_roles ⇒ Object
695 696 697 698 699 700 701 |
# File 'lib/chatkit/client.rb', line 695 def get_roles ({ method: "GET", path: "/roles", jwt: generate_su_token[:token] }) end |
#get_room(options) ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/chatkit/client.rb', line 295 def get_room() if [:id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the room to fetch") end api_request({ method: "GET", path: "/rooms/#{url_encode_path_segment [:id]}", jwt: generate_su_token[:token] }) end |
#get_room_messages(options) ⇒ Object
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/chatkit/client.rb', line 408 def () if [:room_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the room to fetch messages from") end query_params = {} query_params[:initial_id] = [:initial_id] unless [:initial_id].nil? query_params[:direction] = [:direction] unless [:direction].nil? query_params[:limit] = [:limit] unless [:limit].nil? api_v2_request({ method: "GET", path: "/rooms/#{url_encode_path_segment [:room_id]}/messages", query: query_params, jwt: generate_su_token[:token] }) end |
#get_room_read_cursors(options) ⇒ Object
798 799 800 801 802 803 804 805 806 807 808 |
# File 'lib/chatkit/client.rb', line 798 def get_room_read_cursors() if [:room_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the room that you want the read cursors for") end cursors_request({ method: "GET", path: "/cursors/0/rooms/#{url_encode_path_segment [:room_id]}", jwt: generate_su_token[:token] }) end |
#get_rooms(options = nil) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/chatkit/client.rb', line 307 def get_rooms( = nil) = { method: "GET", path: "/rooms", jwt: generate_su_token[:token] } unless .nil? query = {} query[:include_private] = ![:include_private].nil? ? [:include_private] : false query[:from_id] = [:from_id] unless [:from_id].nil? .merge!({ query: query }) end api_request() end |
#get_user(options) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/chatkit/client.rb', line 185 def get_user() if [:id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user you want to fetch") end api_request({ method: "GET", path: "/users/#{url_encode_path_segment [:id]}", jwt: generate_su_token[:token] }) end |
#get_user_joinable_rooms(options) ⇒ Object
331 332 333 334 |
# File 'lib/chatkit/client.rb', line 331 def get_user_joinable_rooms() [:joinable] = true get_rooms_for_user() end |
#get_user_read_cursors(options) ⇒ Object
786 787 788 789 790 791 792 793 794 795 796 |
# File 'lib/chatkit/client.rb', line 786 def get_user_read_cursors() if [:user_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user whose read cursors you want to fetch") end cursors_request({ method: "GET", path: "/cursors/0/users/#{url_encode_path_segment [:user_id]}", jwt: generate_su_token[:token] }) end |
#get_user_roles(options) ⇒ Object
703 704 705 706 707 708 709 710 711 712 713 |
# File 'lib/chatkit/client.rb', line 703 def get_user_roles() if [:user_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user whose roles you want to fetch") end ({ method: "GET", path: "/users/#{url_encode_path_segment [:user_id]}/roles", jwt: generate_su_token[:token] }) end |
#get_user_rooms(options) ⇒ Object
327 328 329 |
# File 'lib/chatkit/client.rb', line 327 def get_user_rooms() get_rooms_for_user() end |
#get_users(options = nil) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/chatkit/client.rb', line 197 def get_users( = nil) = { method: "GET", path: "/users", jwt: generate_su_token[:token] } unless .nil? query = {} query[:from_ts] = [:from_timestamp] unless [:from_timestamp].nil? query[:limit] = [:limit] unless [:limit].nil? .merge!({ query: query }) end api_request() end |
#get_users_by_id(options) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/chatkit/client.rb', line 217 def get_users_by_id() if [:user_ids].nil? raise Chatkit::MissingParameterError.new("You must provide the IDs of the users you want to fetch") end api_request({ method: "GET", path: "/users_by_ids", query: { id: [:user_ids], }, jwt: generate_su_token[:token] }) end |
#remove_global_role_for_user(options) ⇒ Object
715 716 717 |
# File 'lib/chatkit/client.rb', line 715 def remove_global_role_for_user() remove_role_for_user() end |
#remove_room_role_for_user(options) ⇒ Object
719 720 721 722 723 724 725 |
# File 'lib/chatkit/client.rb', line 719 def remove_room_role_for_user() if [:room_id].nil? raise Chatkit::MissingParameterError.new("You must provide a room ID") end remove_role_for_user() end |
#remove_users_from_room(options) ⇒ Object
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/chatkit/client.rb', line 353 def remove_users_from_room() if [:room_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the room you want to remove users from") end if [:user_ids].nil? raise Chatkit::MissingParameterError.new("You must provide a list of IDs of the users you want to remove from the room") end api_request({ method: "PUT", path: "/rooms/#{url_encode_path_segment [:room_id]}/users/remove", body: { user_ids: [:user_ids] }, jwt: generate_su_token[:token] }) end |
#scheduler_request(options) ⇒ Object
828 829 830 |
# File 'lib/chatkit/client.rb', line 828 def scheduler_request() make_request(@scheduler_instance, ) end |
#send_message(options) ⇒ Object
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
# File 'lib/chatkit/client.rb', line 483 def () if [:room_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the room to send the message to") end if [:sender_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user sending the message") end if [:text].nil? raise Chatkit::MissingParameterError.new("You must provide some text for the message") end = [:attachment] unless .nil? if [:resource_link].nil? raise Chatkit::MissingParameterError.new("You must provide a resource_link for the message attachment") end valid_file_types = ['image', 'video', 'audio', 'file'] if [:type].nil? || !valid_file_types.include?([:type]) raise Chatkit::MissingParameterError.new( "You must provide a valid type for the message attachment, i.e. one of: #{valid_file_types.join(', ')}" ) end end payload = { text: [:text], attachment: [:attachment] } api_v2_request({ method: "POST", path: "/rooms/#{url_encode_path_segment [:room_id]}/messages", body: payload, jwt: generate_su_token({ user_id: [:sender_id] })[:token] }) end |
#send_multipart_message(options) ⇒ Object
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/chatkit/client.rb', line 437 def () verify({ room_id: "You must provide the ID of the room to send the message to", sender_id: "You must provide the ID of the user sending the message", parts: "You must provide a parts array", }, ) if not [:parts].length > 0 raise Chatkit::MissingParameterError.new("parts array must have at least one item") end # this assumes the token lives long enough to finish all S3 uploads token = generate_su_token({ user_id: [:sender_id] })[:token] request_parts = [:parts].map { |part| verify({type: "Each part must define a type"}, part) if !part[:content].nil? { type: part[:type], content: part[:content] } elsif !part[:url].nil? { type: part[:type], url: part[:url] } elsif !part[:file].nil? = (token, [:room_id], part) { type: part[:type], attachment: {id: } }.reject{ |_,v| v.nil? } else raise Chatkit::MissingParameterError.new("Each part must have one of :file, :content or :url") end } api_request({ method: "POST", path: "/rooms/#{url_encode_path_segment [:room_id]}/messages", body: {parts: request_parts}, jwt: token }) end |
#send_simple_message(options) ⇒ Object
426 427 428 429 430 431 432 433 434 435 |
# File 'lib/chatkit/client.rb', line 426 def () verify({text: "You must provide some text for the message", }, ) [:parts] = [{type: "text/plain", content: [:text] }] () end |
#set_read_cursor(options) ⇒ Object
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 |
# File 'lib/chatkit/client.rb', line 765 def set_read_cursor() if [:user_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user whose read cursor you want to set") end if [:room_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the room you want to set the user's cursor in") end if [:position].nil? raise Chatkit::MissingParameterError.new("You must provide position of the read cursor") end cursors_request({ method: "PUT", path: "/cursors/0/rooms/#{url_encode_path_segment [:room_id]}/users/#{url_encode_path_segment [:user_id]}", body: { position: [:position] }, jwt: generate_su_token[:token] }) end |
#update_permissions_for_global_role(options) ⇒ Object
737 738 739 740 |
# File 'lib/chatkit/client.rb', line 737 def () [:scope] = GLOBAL_SCOPE () end |
#update_permissions_for_room_role(options) ⇒ Object
742 743 744 745 |
# File 'lib/chatkit/client.rb', line 742 def () [:scope] = ROOM_SCOPE () end |
#update_room(options) ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/chatkit/client.rb', line 264 def update_room() if [:id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the room to update") end payload = {} payload[:name] = [:name] unless [:name].nil? payload[:private] = [:private] unless [:private].nil? payload[:push_notification_title_override] = [:push_notification_title_override] if .key?(:push_notification_title_override) # We want to accept nil payload[:custom_data] = [:custom_data] unless [:custom_data].nil? api_request({ method: "PUT", path: "/rooms/#{url_encode_path_segment [:id]}", body: payload, jwt: generate_su_token[:token] }) end |
#update_user(options) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/chatkit/client.rb', line 143 def update_user() if [:id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user you want to update") end payload = {} payload[:name] = [:name] unless [:name].nil? payload[:avatar_url] = [:avatar_url] unless [:avatar_url].nil? payload[:custom_data] = [:custom_data] unless [:custom_data].nil? api_request({ method: "PUT", path: "/users/#{url_encode_path_segment [:id]}", body: payload, jwt: generate_su_token({ user_id: [:id] })[:token] }) end |
#url_encode_path_segment(s) ⇒ Object
This helper should be used to encode parameters that appear in path segments. CGI::escape should NOT be used as it treats the string as if it appears in a query string. E.G. We want “user name” to be encoded as “user%20name” rather than “user+name”
94 95 96 |
# File 'lib/chatkit/client.rb', line 94 def url_encode_path_segment(s) ERB::Util.url_encode(s) end |