Class: Citadel::Client
- Inherits:
-
Object
- Object
- Citadel::Client
- Defined in:
- lib/citadel-ruby-client.rb
Instance Method Summary collapse
- #all_joined_rooms_response ⇒ Object
- #all_public_rooms_response ⇒ Object
-
#auth_token ⇒ Object
AUTH #.
-
#change_room_visibility(room_id, visibility) ⇒ Object
ROOM MANAGEMENT #.
-
#create_room(room_name, topic) ⇒ Object
ROOM CREATION #.
-
#initialize(tenant_url, public_rooms_limit = DEFAULT_PUBLIC_ROOMS_LIMIT) ⇒ Client
constructor
A new instance of Client.
- #invite_in_room(room_id, user_id) ⇒ Object
-
#invite_users_in_room(room_id, users) ⇒ Object
INVITE #.
-
#join_room(room_id) ⇒ Object
ROOM MEMBERSHIP #.
- #leave_room(room_id) ⇒ Object
- #list_all_joined_rooms ⇒ Object
-
#list_all_public_rooms ⇒ Object
LIST ROOMS #.
-
#send_message(room_id, message) ⇒ Object
SEND #.
- #sign_in(login, password) ⇒ Object
Constructor Details
#initialize(tenant_url, public_rooms_limit = DEFAULT_PUBLIC_ROOMS_LIMIT) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 |
# File 'lib/citadel-ruby-client.rb', line 17 def initialize(tenant_url, public_rooms_limit = DEFAULT_PUBLIC_ROOMS_LIMIT) Citadel.tenant_url = if tenant_url[-1, 1] == '/' tenant_url[0, tenant_url.length - 1] else tenant_url end Citadel.public_rooms_limit = public_rooms_limit end |
Instance Method Details
#all_joined_rooms_response ⇒ Object
69 70 71 72 73 |
# File 'lib/citadel-ruby-client.rb', line 69 def all_joined_rooms_response matrix_paths = MatrixPaths.new url = matrix_paths.base_uri + matrix_paths.list_joined_rooms_path HTTP.auth(auth_token).get(url) end |
#all_public_rooms_response ⇒ Object
63 64 65 66 67 |
# File 'lib/citadel-ruby-client.rb', line 63 def all_public_rooms_response matrix_paths = MatrixPaths.new url = matrix_paths.base_uri + matrix_paths.list_public_rooms_path HTTP.auth(auth_token).get(url) end |
#auth_token ⇒ Object
AUTH #
30 31 32 33 34 35 |
# File 'lib/citadel-ruby-client.rb', line 30 def auth_token return @auth_token if defined? @auth_token puts 'Citadel: You need to sign in' nil end |
#change_room_visibility(room_id, visibility) ⇒ Object
ROOM MANAGEMENT #
178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/citadel-ruby-client.rb', line 178 def change_room_visibility(room_id, visibility) matrix_paths = MatrixPaths.new url = matrix_paths.base_uri + matrix_paths.change_room_visibility_path(room_id) data_hash = { join_rule: visibility } data = JSON.generate data_hash response = HTTP.auth(auth_token).put(url, body: data) matrix_interceptor = MatrixInterceptor.new if matrix_interceptor.need_to_wait_and_retry(response) response = HTTP.auth(auth_token).put(url) end end |
#create_room(room_name, topic) ⇒ Object
ROOM CREATION #
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/citadel-ruby-client.rb', line 79 def create_room(room_name, topic) matrix_paths = MatrixPaths.new url = matrix_paths.base_uri + matrix_paths.create_room_path room_name_alias = room_name.tr!(' ', '_') data_hash = { creation_content: { 'm.federate': false }, name: room_name, preset: 'public_chat', visibility: 'public', room_alias_name: room_name_alias, topic: topic } data = JSON.generate data_hash response = HTTP.auth(auth_token).post(url, body: data) matrix_interceptor = MatrixInterceptor.new if matrix_interceptor.need_to_wait_and_retry(response) response = HTTP.auth(auth_token).post(url, body: data) end JSON.parse(response.body)['room_id'] end |
#invite_in_room(room_id, user_id) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/citadel-ruby-client.rb', line 132 def invite_in_room(room_id, user_id) matrix_paths = MatrixPaths.new url = matrix_paths.base_uri + matrix_paths.invite_in_room_path(room_id) data_hash = { user_id: user_id } data = JSON.generate data_hash response = HTTP.auth(auth_token).post(url, body: data) matrix_interceptor = MatrixInterceptor.new if matrix_interceptor.need_to_wait_and_retry(response) response = HTTP.auth(auth_token).post(url, body: data) end end |
#invite_users_in_room(room_id, users) ⇒ Object
INVITE #
126 127 128 129 130 |
# File 'lib/citadel-ruby-client.rb', line 126 def invite_users_in_room(room_id, users) users.each do |user| invite_in_room(room_id, user) end end |
#join_room(room_id) ⇒ Object
ROOM MEMBERSHIP #
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/citadel-ruby-client.rb', line 150 def join_room(room_id) matrix_paths = MatrixPaths.new url = matrix_paths.base_uri + matrix_paths.join_room_path(room_id) response = HTTP.auth(auth_token).post(url) matrix_interceptor = MatrixInterceptor.new if matrix_interceptor.need_to_wait_and_retry(response) response = HTTP.auth(auth_token).post(url) end end |
#leave_room(room_id) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/citadel-ruby-client.rb', line 162 def leave_room(room_id) matrix_paths = MatrixPaths.new url = matrix_paths.base_uri + matrix_paths.leave_room_path(room_id) response = HTTP.auth(auth_token).post(url) matrix_interceptor = MatrixInterceptor.new if matrix_interceptor.need_to_wait_and_retry(response) response = HTTP.auth(auth_token).post(url) end end |
#list_all_joined_rooms ⇒ Object
57 58 59 60 61 |
# File 'lib/citadel-ruby-client.rb', line 57 def list_all_joined_rooms response = all_joined_rooms_response rooms = JSON.parse(response.body)['joined_rooms'] rooms end |
#list_all_public_rooms ⇒ Object
LIST ROOMS #
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/citadel-ruby-client.rb', line 46 def list_all_public_rooms response = all_public_rooms_response array = JSON.parse(response.body)['chunk'] count = array.size - 1 result = [] (0..count).each do |i| result << array[i]['room_id'] end result end |
#send_message(room_id, message) ⇒ Object
SEND #
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/citadel-ruby-client.rb', line 105 def (room_id, ) matrix_paths = MatrixPaths.new randomizer = Random.new txn = randomizer.rand(100) url = matrix_paths.base_uri + matrix_paths.(room_id) + txn.to_s data_hash = { msgtype: 'm.text', body: } data = JSON.generate data_hash response = HTTP.auth(auth_token).put(url, body: data) matrix_interceptor = MatrixInterceptor.new if matrix_interceptor.need_to_wait_and_retry(response) response = HTTP.auth(auth_token).put(url, body: data) end end |
#sign_in(login, password) ⇒ Object
37 38 39 40 |
# File 'lib/citadel-ruby-client.rb', line 37 def sign_in(login, password) authenticator = Authenticator.new @auth_token = 'Bearer ' + authenticator.get_token(login, password) end |