class Chatkit::Client
Attributes
api_instance[RW]
api_v2_instance[RW]
cursors_instance[RW]
Public Class Methods
new(options)
click to toggle source
# File lib/chatkit/client.rb, line 22 def initialize(options) base_options = { locator: options[:instance_locator], key: options[:key], port: options[:port], host: options[:host], client: options[:client], sdk_info: PusherPlatform::SDKInfo.new({ product_name: 'chatkit', version: '0.7.2' }) } @api_v2_instance = PusherPlatform::Instance.new( base_options.merge({ service_name: 'chatkit', service_version: 'v2' }) ) @api_instance = PusherPlatform::Instance.new( base_options.merge({ service_name: 'chatkit', service_version: 'v6' }) ) @authorizer_instance = PusherPlatform::Instance.new( base_options.merge({ service_name: 'chatkit_authorizer', service_version: 'v2' }) ) @cursors_instance = PusherPlatform::Instance.new( base_options.merge({ service_name: 'chatkit_cursors', service_version: 'v2' }) ) @scheduler_instance = PusherPlatform::Instance.new( base_options.merge({ service_name: 'chatkit_scheduler', service_version: 'v1' }) ) end
Public Instance Methods
add_users_to_room(options)
click to toggle source
# File lib/chatkit/client.rb, line 336 def add_users_to_room(options) if options[:room_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the room you want to add users to") end if options[: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 options[:room_id]}/users/add", body: { user_ids: options[:user_ids] }, jwt: generate_su_token[:token] }) end
api_request(options)
click to toggle source
# File lib/chatkit/client.rb, line 816 def api_request(options) make_request(@api_instance, options) end
api_v2_request(options)
click to toggle source
Service-specific helpers
# File lib/chatkit/client.rb, line 812 def api_v2_request(options) make_request(@api_v2_instance, options) end
assign_global_role_to_user(options)
click to toggle source
# File lib/chatkit/client.rb, line 683 def assign_global_role_to_user(options) assign_role_to_user(options) end
assign_room_role_to_user(options)
click to toggle source
# File lib/chatkit/client.rb, line 687 def assign_room_role_to_user(options) if options[: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(options) end
async_delete_room(options)
click to toggle source
# File lib/chatkit/client.rb, line 283 def async_delete_room(options) if options[: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 options[:id]}", jwt: generate_su_token[:token] }) end
async_delete_user(options)
click to toggle source
# File lib/chatkit/client.rb, line 161 def async_delete_user(options) if options[: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 options[:id]}", jwt: generate_su_token[:token] }) end
authenticate(options)
click to toggle source
# File lib/chatkit/client.rb, line 71 def authenticate(options) user_id = options['user_id'] || options[:user_id] auth_payload = options['auth_payload'] || options[:auth_payload] || { grant_type: 'client_credentials' } @api_instance.authenticate(auth_payload, { user_id: user_id }) end
create_global_role(options)
click to toggle source
Roles and permissions API
# File lib/chatkit/client.rb, line 663 def create_global_role(options) options[:scope] = GLOBAL_SCOPE create_role(options) end
create_room(options)
click to toggle source
Room API
# File lib/chatkit/client.rb, line 234 def create_room(options) if options[:creator_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user creating the room") end if options[:name].nil? raise Chatkit::MissingParameterError.new("You must provide a name for the room") end body = { name: options[:name], private: options[:private] || false } body[:id] = options[:id] unless options[:id].nil? body[:push_notification_title_override] = options[:push_notification_title_override] unless options[:push_notification_title_override].nil? body[:custom_data] = options[:custom_data] unless options[:custom_data].nil? unless options[:user_ids].nil? body[:user_ids] = options[:user_ids] end api_request({ method: "POST", path: "/rooms", body: body, jwt: generate_su_token({ user_id: options[:creator_id] })[:token] }) end
create_room_role(options)
click to toggle source
# File lib/chatkit/client.rb, line 668 def create_room_role(options) options[:scope] = ROOM_SCOPE create_role(options) end
create_user(options)
click to toggle source
User API
# File lib/chatkit/client.rb, line 100 def create_user(options) if options[:id].nil? raise Chatkit::MissingParameterError.new("You must provide an ID for the user you want to create") end if options[:name].nil? raise Chatkit::MissingParameterError.new("You must provide a name for the user you want to create") end body = { id: options[:id], name: options[:name] } unless options[:avatar_url].nil? body[:avatar_url] = options[:avatar_url] end unless options[:custom_data].nil? body[:custom_data] = options[:custom_data] end api_request({ method: "POST", path: "/users", body: body, jwt: generate_su_token[:token] }) end
create_users(options)
click to toggle source
# File lib/chatkit/client.rb, line 130 def create_users(options) if options[: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: options, jwt: generate_su_token[:token] }) end
cursors_request(options)
click to toggle source
# File lib/chatkit/client.rb, line 824 def cursors_request(options) make_request(@cursors_instance, options) end
delete_global_role(options)
click to toggle source
# File lib/chatkit/client.rb, line 673 def delete_global_role(options) options[:scope] = GLOBAL_SCOPE delete_role(options) end
delete_message(options)
click to toggle source
# File lib/chatkit/client.rb, line 525 def delete_message(options) if options[:message_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the message you want to delete") end if options[: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/#{options[:room_id]}/messages/#{options[:message_id]}", jwt: generate_su_token[:token] }) end
delete_room_role(options)
click to toggle source
# File lib/chatkit/client.rb, line 678 def delete_room_role(options) options[:scope] = ROOM_SCOPE delete_role(options) end
edit_message(room_id, message_id, options)
click to toggle source
# File lib/chatkit/client.rb, line 607 def edit_message(room_id, message_id, options) 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 message_id.nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the message to edit") end if options[:sender_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user editing the message") end if options[:text].nil? raise Chatkit::MissingParameterError.new("You must provide some text for the message") end if !options['room_id'].nil? raise Chatkit::UnexpectedParameterError.new("a messages room id cannot be edited") end if !options['message_id'].nil? raise Chatkit::UnexpectedParameterError.new("a messages id cannot be edited") end attachment = options[:attachment] unless attachment.nil? if attachment[: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 attachment[:type].nil? || !valid_file_types.include?(attachment[: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: options[:text], attachment: options[:attachment] } api_v2_request({ method: "PUT", path: "/rooms/#{url_encode_path_segment room_id}/messages/#{message_id}", body: payload, jwt: generate_su_token({ user_id: options[:sender_id] })[:token] }) end
edit_multipart_message(room_id, message_id, options)
click to toggle source
# File lib/chatkit/client.rb, line 552 def edit_multipart_message(room_id, message_id, options) verify({ sender_id: "You must provide the ID of the user editing the message", parts: "You must provide a parts array", }, options) 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: message_id}) if !options['room_id'].nil? raise Chatkit::UnexpectedParameterError.new("a messages room id cannot be edited") end if !options['message_id'].nil? raise Chatkit::UnexpectedParameterError.new("a messages id cannot be edited") end if not options[: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: options[:sender_id] })[:token] request_parts = options[: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? attachment_id = upload_attachment(token, room_id, part) { type: part[:type], attachment: {id: 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/#{message_id}", body: {parts: request_parts}, jwt: token }) end
edit_simple_message(room_id, message_id, options)
click to toggle source
# File lib/chatkit/client.rb, line 541 def edit_simple_message(room_id, message_id, options) verify({text: "You must provide some text for the message", }, options) options[:parts] = [{type: "text/plain", content: options[:text] }] edit_multipart_message(room_id, message_id, options) end
fetch_multipart_message(options)
click to toggle source
Messages API
# File lib/chatkit/client.rb, line 372 def fetch_multipart_message(options) verify({ room_id: "You must provide the ID of the room to fetch the message from", message_id: "You must provide the message ID" }, options) api_request({ method: "GET", path: "/rooms/#{url_encode_path_segment options[:room_id]}/messages/#{options[:message_id]}", jwt: generate_su_token[:token] }) end
fetch_multipart_messages(options)
click to toggle source
# File lib/chatkit/client.rb, line 388 def fetch_multipart_messages(options) verify({ room_id: "You must provide the ID of the room to fetch the messages from", }, options) if !options[:limit].nil? and options[:limit] <= 0 raise Chatkit::MissingParameterError.new("Limit must be greater than 0") end optional_params = [:initial_id, :direction, :limit] query_params = options.select { |key,_| optional_params.include? key } api_request({ method: "GET", path: "/rooms/#{url_encode_path_segment options[:room_id]}/messages", query: query_params, jwt: generate_su_token[:token] }) end
generate_access_token(options)
click to toggle source
# File lib/chatkit/client.rb, line 79 def generate_access_token(options) if options.empty? raise Chatkit::Error.new("You must provide a either a user_id or `su: true`") end @api_instance.generate_access_token(options) end
generate_su_token(options = {})
click to toggle source
# File lib/chatkit/client.rb, line 87 def generate_su_token(options = {}) generate_access_token({ su: true }.merge(options)) end
get_delete_status(options)
click to toggle source
# File lib/chatkit/client.rb, line 173 def get_delete_status(options) if options[: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 options[:id]}", jwt: generate_su_token[:token] }) end
get_permissions_for_global_role(options)
click to toggle source
# File lib/chatkit/client.rb, line 727 def get_permissions_for_global_role(options) options[:scope] = GLOBAL_SCOPE get_permissions_for_role(options) end
get_permissions_for_room_role(options)
click to toggle source
# File lib/chatkit/client.rb, line 732 def get_permissions_for_room_role(options) options[:scope] = ROOM_SCOPE get_permissions_for_role(options) end
get_read_cursor(options)
click to toggle source
Cursors API
# File lib/chatkit/client.rb, line 749 def get_read_cursor(options) if options[:user_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user whose read cursor you want to fetch") end if options[: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 options[:room_id]}/users/#{url_encode_path_segment options[:user_id]}", jwt: generate_su_token[:token] }) end
get_roles()
click to toggle source
# File lib/chatkit/client.rb, line 695 def get_roles authorizer_request({ method: "GET", path: "/roles", jwt: generate_su_token[:token] }) end
get_room(options)
click to toggle source
# File lib/chatkit/client.rb, line 295 def get_room(options) if options[: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 options[:id]}", jwt: generate_su_token[:token] }) end
get_room_messages(options)
click to toggle source
# File lib/chatkit/client.rb, line 408 def get_room_messages(options) if options[: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] = options[:initial_id] unless options[:initial_id].nil? query_params[:direction] = options[:direction] unless options[:direction].nil? query_params[:limit] = options[:limit] unless options[:limit].nil? api_v2_request({ method: "GET", path: "/rooms/#{url_encode_path_segment options[:room_id]}/messages", query: query_params, jwt: generate_su_token[:token] }) end
get_room_read_cursors(options)
click to toggle source
# File lib/chatkit/client.rb, line 798 def get_room_read_cursors(options) if options[: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 options[:room_id]}", jwt: generate_su_token[:token] }) end
get_rooms(options = nil)
click to toggle source
# File lib/chatkit/client.rb, line 307 def get_rooms(options = nil) request_options = { method: "GET", path: "/rooms", jwt: generate_su_token[:token] } unless options.nil? query = {} query[:include_private] = !options[:include_private].nil? ? options[:include_private] : false query[:from_id] = options[:from_id] unless options[:from_id].nil? request_options.merge!({ query: query }) end api_request(request_options) end
get_user(options)
click to toggle source
# File lib/chatkit/client.rb, line 185 def get_user(options) if options[: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 options[:id]}", jwt: generate_su_token[:token] }) end
get_user_joinable_rooms(options)
click to toggle source
# File lib/chatkit/client.rb, line 331 def get_user_joinable_rooms(options) options[:joinable] = true get_rooms_for_user(options) end
get_user_read_cursors(options)
click to toggle source
# File lib/chatkit/client.rb, line 786 def get_user_read_cursors(options) if options[: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 options[:user_id]}", jwt: generate_su_token[:token] }) end
get_user_roles(options)
click to toggle source
# File lib/chatkit/client.rb, line 703 def get_user_roles(options) if options[:user_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user whose roles you want to fetch") end authorizer_request({ method: "GET", path: "/users/#{url_encode_path_segment options[:user_id]}/roles", jwt: generate_su_token[:token] }) end
get_user_rooms(options)
click to toggle source
# File lib/chatkit/client.rb, line 327 def get_user_rooms(options) get_rooms_for_user(options) end
get_users(options = nil)
click to toggle source
# File lib/chatkit/client.rb, line 197 def get_users(options = nil) request_options = { method: "GET", path: "/users", jwt: generate_su_token[:token] } unless options.nil? query = {} query[:from_ts] = options[:from_timestamp] unless options[:from_timestamp].nil? query[:limit] = options[:limit] unless options[:limit].nil? request_options.merge!({ query: query }) end api_request(request_options) end
get_users_by_id(options)
click to toggle source
# File lib/chatkit/client.rb, line 217 def get_users_by_id(options) if options[: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: options[:user_ids], }, jwt: generate_su_token[:token] }) end
remove_global_role_for_user(options)
click to toggle source
# File lib/chatkit/client.rb, line 715 def remove_global_role_for_user(options) remove_role_for_user(options) end
remove_room_role_for_user(options)
click to toggle source
# File lib/chatkit/client.rb, line 719 def remove_room_role_for_user(options) if options[:room_id].nil? raise Chatkit::MissingParameterError.new("You must provide a room ID") end remove_role_for_user(options) end
remove_users_from_room(options)
click to toggle source
# File lib/chatkit/client.rb, line 353 def remove_users_from_room(options) if options[:room_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the room you want to remove users from") end if options[: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 options[:room_id]}/users/remove", body: { user_ids: options[:user_ids] }, jwt: generate_su_token[:token] }) end
scheduler_request(options)
click to toggle source
# File lib/chatkit/client.rb, line 828 def scheduler_request(options) make_request(@scheduler_instance, options) end
send_message(options)
click to toggle source
# File lib/chatkit/client.rb, line 483 def send_message(options) if options[:room_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the room to send the message to") end if options[:sender_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user sending the message") end if options[:text].nil? raise Chatkit::MissingParameterError.new("You must provide some text for the message") end attachment = options[:attachment] unless attachment.nil? if attachment[: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 attachment[:type].nil? || !valid_file_types.include?(attachment[: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: options[:text], attachment: options[:attachment] } api_v2_request({ method: "POST", path: "/rooms/#{url_encode_path_segment options[:room_id]}/messages", body: payload, jwt: generate_su_token({ user_id: options[:sender_id] })[:token] }) end
send_multipart_message(options)
click to toggle source
# File lib/chatkit/client.rb, line 437 def send_multipart_message(options) 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", }, options) if not options[: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: options[:sender_id] })[:token] request_parts = options[: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? attachment_id = upload_attachment(token, options[:room_id], part) { type: part[:type], attachment: {id: 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 options[:room_id]}/messages", body: {parts: request_parts}, jwt: token }) end
send_simple_message(options)
click to toggle source
# File lib/chatkit/client.rb, line 426 def send_simple_message(options) verify({text: "You must provide some text for the message", }, options) options[:parts] = [{type: "text/plain", content: options[:text] }] send_multipart_message(options) end
set_read_cursor(options)
click to toggle source
# File lib/chatkit/client.rb, line 765 def set_read_cursor(options) if options[:user_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user whose read cursor you want to set") end if options[: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 options[: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 options[:room_id]}/users/#{url_encode_path_segment options[:user_id]}", body: { position: options[:position] }, jwt: generate_su_token[:token] }) end
update_permissions_for_global_role(options)
click to toggle source
# File lib/chatkit/client.rb, line 737 def update_permissions_for_global_role(options) options[:scope] = GLOBAL_SCOPE update_permissions_for_role(options) end
update_permissions_for_room_role(options)
click to toggle source
# File lib/chatkit/client.rb, line 742 def update_permissions_for_room_role(options) options[:scope] = ROOM_SCOPE update_permissions_for_role(options) end
update_room(options)
click to toggle source
# File lib/chatkit/client.rb, line 264 def update_room(options) if options[:id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the room to update") end payload = {} payload[:name] = options[:name] unless options[:name].nil? payload[:private] = options[:private] unless options[:private].nil? payload[:push_notification_title_override] = options[:push_notification_title_override] if options.key?(:push_notification_title_override) # We want to accept nil payload[:custom_data] = options[:custom_data] unless options[:custom_data].nil? api_request({ method: "PUT", path: "/rooms/#{url_encode_path_segment options[:id]}", body: payload, jwt: generate_su_token[:token] }) end
update_user(options)
click to toggle source
# File lib/chatkit/client.rb, line 143 def update_user(options) if options[:id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user you want to update") end payload = {} payload[:name] = options[:name] unless options[:name].nil? payload[:avatar_url] = options[:avatar_url] unless options[:avatar_url].nil? payload[:custom_data] = options[:custom_data] unless options[:custom_data].nil? api_request({ method: "PUT", path: "/users/#{url_encode_path_segment options[:id]}", body: payload, jwt: generate_su_token({ user_id: options[:id] })[:token] }) end
url_encode_path_segment(s)
click to toggle source
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”
# File lib/chatkit/client.rb, line 94 def url_encode_path_segment(s) ERB::Util.url_encode(s) end
Private Instance Methods
assign_role_to_user(options)
click to toggle source
# File lib/chatkit/client.rb, line 906 def assign_role_to_user(options) if options[:name].nil? raise Chatkit::MissingParameterError.new("You must provide the role's name") end if options[:user_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user you want to assign the role to") end body = { name: options[:name] } unless options[:room_id].nil? body.merge!(room_id: options[:room_id]) end authorizer_request({ method: "PUT", path: "/users/#{url_encode_path_segment options[:user_id]}/roles", body: body, jwt: generate_su_token[:token] }) end
create_role(options)
click to toggle source
# File lib/chatkit/client.rb, line 873 def create_role(options) if options[:name].nil? raise Chatkit::MissingParameterError.new("You must provide a name for the role") end if options[:permissions].nil? raise Chatkit::MissingParameterError.new("You must provide permissions for the role, even if it's an empty list") end authorizer_request({ method: "POST", path: "/roles", body: { scope: options[:scope], name: options[:name], permissions: options[:permissions], }, jwt: generate_su_token[:token] }) end
delete_role(options)
click to toggle source
# File lib/chatkit/client.rb, line 894 def delete_role(options) if options[:name].nil? raise Chatkit::MissingParameterError.new("You must provide the role's name") end authorizer_request({ method: "DELETE", path: "/roles/#{url_encode_path_segment options[:name]}/scope/#{options[:scope]}", jwt: generate_su_token[:token] }) end
format_response(res)
click to toggle source
# File lib/chatkit/client.rb, line 845 def format_response(res) body = res.body.empty? ? nil : JSON.parse(res.body, { symbolize_names: true }) { status: res.status, headers: res.headers, body: body } end
get_permissions_for_role(options)
click to toggle source
# File lib/chatkit/client.rb, line 947 def get_permissions_for_role(options) if options[:name].nil? raise Chatkit::MissingParameterError.new("You must provide the name of the role you want to fetch the permissions of") end authorizer_request({ method: "GET", path: "/roles/#{url_encode_path_segment options[:name]}/scope/#{options[:scope]}/permissions", jwt: generate_su_token[:token] }) end
get_rooms_for_user(options)
click to toggle source
# File lib/chatkit/client.rb, line 855 def get_rooms_for_user(options) if options[:id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user whose rooms you want to fetch") end request_options = { method: "GET", path: "/users/#{url_encode_path_segment options[:id]}/rooms", jwt: generate_su_token[:token] } unless options[:joinable].nil? request_options.merge!({ query: { joinable: options[:joinable] }}) end api_request(request_options) end
make_request(instance, options)
click to toggle source
# File lib/chatkit/client.rb, line 834 def make_request(instance, options) options.merge!({ headers: { :'Content-Type' => 'application/json' } }) begin format_response(instance.request(options)) rescue PusherPlatform::ErrorResponse => e raise Chatkit::ResponseError.new(e) rescue PusherPlatform::Error => e raise Chatkit::Error.new(e.message) end end
remove_role_for_user(options)
click to toggle source
# File lib/chatkit/client.rb, line 929 def remove_role_for_user(options) if options[:user_id].nil? raise Chatkit::MissingParameterError.new("You must provide the ID of the user you want to remove the role for") end request_options = { method: "DELETE", path: "/users/#{url_encode_path_segment options[:user_id]}/roles", jwt: generate_su_token[:token] } unless options[:room_id].nil? request_options.merge!({ query: { room_id: options[:room_id] }}) end authorizer_request(request_options) end
update_permissions_for_role(options)
click to toggle source
# File lib/chatkit/client.rb, line 959 def update_permissions_for_role(options) if options[:name].nil? raise Chatkit::MissingParameterError.new("You must provide the name of the role you want to update the permissions of") end permissions_to_add = options[:permissions_to_add] permissions_to_remove = options[:permissions_to_remove] if (permissions_to_add.nil? || permissions_to_add.empty?) && (permissions_to_remove.nil? || permissions_to_remove.empty?) raise Chatkit::MissingParameterError.new("permissions_to_add and permissions_to_remove cannot both be empty") end body = {} body[:add_permissions] = permissions_to_add unless permissions_to_add.nil? || permissions_to_add.empty? body[:remove_permissions] = permissions_to_remove unless permissions_to_remove.nil? ||permissions_to_remove.empty? authorizer_request({ method: "PUT", path: "/roles/#{url_encode_path_segment options[:name]}/scope/#{options[:scope]}/permissions", body: body, jwt: generate_su_token[:token] }) end
upload_attachment(token, room_id, part)
click to toggle source
# File lib/chatkit/client.rb, line 983 def upload_attachment(token, room_id, part) body = part[:file] content_length = body.length content_type = part[:type] if content_length <= 0 raise Chatkit::MissingParameterError.new("File contents size must be greater than 0") end attachment_req = { content_type: content_type, content_length: content_length, name: part[:name], origin: part[:origin], custom_data: part[:custom_data] } attachment_response = api_request({ method: "POST", path: "/rooms/#{url_encode_path_segment room_id}/attachments", body: attachment_req, jwt: token }) url = attachment_response[:body][:upload_url] connection = Excon.new(url, :omit_default_port => true) upload_response = connection.put( :body => body, :headers => { "Content-Type" => content_type, "Content-Length" => content_length } ) if upload_response.status != 200 error = {message: "Failed to upload attachment", response_object: upload_response } raise Chatkit::UploadError.new(error) end attachment_response[:body][:attachment_id] end
verify(required, options)
click to toggle source
# File lib/chatkit/client.rb, line 1027 def verify(required, options) required.each { |field_name, message| if options[field_name].nil? raise Chatkit::MissingParameterError.new(message) end } end