class RocketChat::Messages::Room
Rocket.Chat Room
messages template (groups&channels)
Attributes
Public Class Methods
Full API path to call
# File lib/rocket_chat/messages/room.rb, line 29 def self.api_path(method) "/api/v1/#{collection}.#{method}" end
# File lib/rocket_chat/messages/room.rb, line 12 def self.inherited(subclass) field = subclass.name.split('::')[-1].downcase collection = "#{field}s" subclass.send(:define_singleton_method, :field) { field } subclass.send(:define_singleton_method, :collection) { collection } super end
@param [Session] session Session
# File lib/rocket_chat/messages/room.rb, line 24 def initialize(session) @session = session end
Public Instance Methods
*.addAll REST API @param [String] room_id Rocket.Chat room id @param [String] name Rocket.Chat room name (coming soon) @param [String] active_users_only Add active users only @return [Boolean] @raise [HTTPError, StatusError]
# File lib/rocket_chat/messages/room.rb, line 75 def add_all(room_id: nil, name: nil, active_users_only: false) session.request_json( self.class.api_path('addAll'), method: :post, body: room_params(room_id, name) .merge(activeUsersOnly: active_users_only) )['success'] end
*.add_moderator REST API @param [String] room_id Rocket.Chat room id @param [String] name Rocket.Chat room name (coming soon) @param [String] user_id Rocket.Chat user id @param [String] username Rocket.Chat username @return [Boolean] @raise [HTTPError, StatusError]
# File lib/rocket_chat/messages/room.rb, line 129 def add_moderator(room_id: nil, name: nil, user_id: nil, username: nil) session.request_json( self.class.api_path('addModerator'), method: :post, body: room_params(room_id, name) .merge(user_params(user_id, username)) )['success'] end
*.add_owner REST API @param [String] room_id Rocket.Chat room id @param [String] name Rocket.Chat room name (coming soon) @param [String] user_id Rocket.Chat user id @param [String] username Rocket.Chat username @return [Boolean] @raise [HTTPError, StatusError]
# File lib/rocket_chat/messages/room.rb, line 93 def add_owner(room_id: nil, name: nil, user_id: nil, username: nil) session.request_json( self.class.api_path('addOwner'), method: :post, body: room_params(room_id, name) .merge(user_params(user_id, username)) )['success'] end
*.archive REST API @param [String] room_id Rocket.Chat room id @param [String] name Rocket.Chat room name (coming soon) @return [Boolean] @raise [HTTPError, StatusError]
# File lib/rocket_chat/messages/room.rb, line 216 def archive(room_id: nil, name: nil) session.request_json( self.class.api_path('archive'), method: :post, body: room_params(room_id, name) )['success'] end
*.create REST API @param [String] name Room
name @param [Hash] options Additional options @return [Room] @raise [HTTPError, StatusError]
# File lib/rocket_chat/messages/room.rb, line 40 def create(name, options = {}) response = session.request_json( self.class.api_path('create'), method: :post, body: { name: name }.merge(room_option_hash(options)) ) RocketChat::Room.new response[self.class.field] end
*.delete REST API @param [String] room_id Rocket.Chat room id @param [String] name Rocket.Chat room name (coming soon) @return [Boolean] @raise [HTTPError, StatusError]
# File lib/rocket_chat/messages/room.rb, line 58 def delete(room_id: nil, name: nil) session.request_json( self.class.api_path('delete'), method: :post, body: room_params(room_id, name), upstreamed_errors: ['error-room-not-found'] )['success'] end
*.info REST API @param [String] room_id Rocket.Chat room id @param [String] name Room
name (channels since 0.56) @return [Room] @raise [HTTPError, StatusError]
# File lib/rocket_chat/messages/room.rb, line 163 def info(room_id: nil, name: nil) response = session.request_json( self.class.api_path('info'), body: room_params(room_id, name), upstreamed_errors: ['error-room-not-found'] ) RocketChat::Room.new response[self.class.field] if response['success'] end
*.invite REST API @param [String] room_id Rocket.Chat room id @param [String] name Rocket.Chat room name (coming soon) @param [String] user_id Rocket.Chat user id @param [String] username Rocket.Chat username @return [Boolean] @raise [HTTPError, StatusError]
# File lib/rocket_chat/messages/room.rb, line 182 def invite(room_id: nil, name: nil, user_id: nil, username: nil) session.request_json( self.class.api_path('invite'), method: :post, body: room_params(room_id, name) .merge(user_params(user_id, username)) )['success'] end
*.kick REST API @param [String] room_id Rocket.Chat room id @param [String] name Rocket.Chat room name (coming soon) @param [String] user_id Rocket.Chat user id @param [String] username Rocket.Chat username @return [Boolean] @raise [HTTPError, StatusError]
# File lib/rocket_chat/messages/room.rb, line 200 def kick(room_id: nil, name: nil, user_id: nil, username: nil) session.request_json( self.class.api_path('kick'), method: :post, body: room_params(room_id, name) .merge(user_params(user_id, username)) )['success'] end
*.leave REST API @param [String] room_id Rocket.Chat room id @param [String] name Rocket.Chat room name (coming soon) @return [Boolean] @raise [HTTPError, StatusError]
# File lib/rocket_chat/messages/room.rb, line 246 def leave(room_id: nil, name: nil) session.request_json( self.class.api_path('leave'), method: :post, body: room_params(room_id, name) )['success'] end
.members REST API @param [String] room_id Rocket.Chat room id @param [String] name Rocket.Chat room name @param [Integer] offset Query offset @param [Integer] count Query count/limit @param [Hash] sort Query field sort hash. eg `{ msgs: 1, name: -1 }` @return [Users @raise [HTTPError, StatusError]
# File lib/rocket_chat/messages/room.rb, line 298 def members(room_id: nil, name: nil, offset: nil, count: nil, sort: nil) response = session.request_json( self.class.api_path('members'), body: room_params(room_id, name).merge(build_list_body(offset, count, sort, nil, nil)) ) response['members'].map { |hash| RocketChat::User.new hash } if response['success'] end
*.remove_moderator REST API @param [String] room_id Rocket.Chat room id @param [String] name Rocket.Chat room name (coming soon) @param [String] user_id Rocket.Chat user id @param [String] username Rocket.Chat username @return [Boolean] @raise [HTTPError, StatusError]
# File lib/rocket_chat/messages/room.rb, line 147 def remove_moderator(room_id: nil, name: nil, user_id: nil, username: nil) session.request_json( self.class.api_path('removeModerator'), method: :post, body: room_params(room_id, name) .merge(user_params(user_id, username)) )['success'] end
*.remove_owner REST API @param [String] room_id Rocket.Chat room id @param [String] name Rocket.Chat room name (coming soon) @param [String] user_id Rocket.Chat user id @param [String] username Rocket.Chat username @return [Boolean] @raise [HTTPError, StatusError]
# File lib/rocket_chat/messages/room.rb, line 111 def remove_owner(room_id: nil, name: nil, user_id: nil, username: nil) session.request_json( self.class.api_path('removeOwner'), method: :post, body: room_params(room_id, name) .merge(user_params(user_id, username)) )['success'] end
*.rename REST API @param [String] room_id Rocket.Chat room id @param [String] new_name New room name @return [Boolean] @raise [HTTPError, StatusError]
# File lib/rocket_chat/messages/room.rb, line 261 def rename(room_id, new_name) session.request_json( self.class.api_path('rename'), method: :post, body: { roomId: room_id, name: new_name } )['success'] end
.set REST API @param [String] room_id Rocket.Chat room id @param [String] name Rocket.Chat room name (coming soon) @param [Hash] setting Single key-value @return [Boolean] @raise [ArgumentError, HTTPError
, StatusError]
# File lib/rocket_chat/messages/room.rb, line 277 def set_attr(room_id: nil, name: nil, **setting) attribute, value = setting.first validate_attribute(attribute) session.request_json( self.class.api_path(Util.camelize("set_#{attribute}")), method: :post, body: room_params(room_id, name) .merge(Util.camelize(attribute) => value) )['success'] end
*.unarchive REST API @param [String] room_id Rocket.Chat room id @param [String] name Rocket.Chat room name (coming soon) @return [Boolean] @raise [HTTPError, StatusError]
# File lib/rocket_chat/messages/room.rb, line 231 def unarchive(room_id: nil, name: nil) session.request_json( self.class.api_path('unarchive'), method: :post, body: room_params(room_id, name) )['success'] end
Private Instance Methods
# File lib/rocket_chat/messages/room.rb, line 311 def room_option_hash(options) args = [options, :members, :read_only, :custom_fields] options = Util.slice_hash(*args) return {} if options.empty? new_hash = {} options.each { |key, value| new_hash[Util.camelize(key)] = value } new_hash end
# File lib/rocket_chat/messages/room.rb, line 322 def validate_attribute(attribute) raise ArgumentError, "Unsettable attribute: #{attribute || 'nil'}" unless \ self.class.settable_attributes.include?(attribute) end