class Citadel::Client

Public Class Methods

new(tenant_url, public_rooms_limit = DEFAULT_PUBLIC_ROOMS_LIMIT) click to toggle source
# 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

Public Instance Methods

all_joined_rooms_response() click to toggle source
# 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() click to toggle source
# 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() click to toggle source

AUTH #

# 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) click to toggle source

ROOM MANAGEMENT #

# 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) click to toggle source

ROOM CREATION #

# 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) click to toggle source
# 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) click to toggle source

INVITE #

# 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) click to toggle source

ROOM MEMBERSHIP #

# 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) click to toggle source
# 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() click to toggle source
# 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() click to toggle source

LIST ROOMS #

# 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) click to toggle source

SEND #

# File lib/citadel-ruby-client.rb, line 105
def send_message(room_id, message)
  matrix_paths = MatrixPaths.new
  randomizer = Random.new
  txn = randomizer.rand(100)
  url = matrix_paths.base_uri + matrix_paths.send_message_path(room_id) + txn.to_s

  data_hash = { msgtype: 'm.text', body: message }
  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) click to toggle source
# 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