class Colossus

Top Level Class. The public API of the Gem.

Main Colossus module

The Version

Constants

ACTIVE
AWAY
DISCONNECTED
VERSION

Attributes

engine[R]
verifier[R]

Public Class Methods

config() click to toggle source
# File lib/colossus/configuration.rb, line 27
def self.config
  configuration
end
config=(configuration) click to toggle source

rubocop:enable

# File lib/colossus/configuration.rb, line 38
def self.config=(configuration)
  self.configuration = configuration
end
configuration() click to toggle source
# File lib/colossus/configuration.rb, line 23
def self.configuration
  @configuration ||= Configuration.new
end
configuration=(configuration) click to toggle source

Help ? rubocop:disable TrivialAccessors

# File lib/colossus/configuration.rb, line 33
def self.configuration=(configuration)
  @configuration = configuration
end
configure() { |configuration| ... } click to toggle source
# File lib/colossus/configuration.rb, line 18
def self.configure
  yield(configuration) if block_given?
  configuration
end
new(ttl = Colossus.config.ttl, engine = Colossus.config.engine, secret = Colossus.config.secret_key, writer_token = Colossus.config.writer_token) click to toggle source

Initialize Colossus

@param ttl [Integer] the seconds before a user without emitting heartbeat

is considered disconnected

@param engine [Engine] the engine which implements the needed methods

to work with Colossus

@return [Colossus]

# File lib/colossus.rb, line 49
def initialize(ttl          = Colossus.config.ttl,
               engine       = Colossus.config.engine,
               secret       = Colossus.config.secret_key,
               writer_token = Colossus.config.writer_token)
  @engine = engine.new(ttl.to_i)
  @engine.add_observer(self)
  @verifier = Colossus::Verifier.new(secret, writer_token)
end
reset_configuration() click to toggle source
# File lib/colossus/configuration.rb, line 42
def self.reset_configuration
  @configuration = Configuration.new
end

Public Instance Methods

generate_user_token(user_id) click to toggle source

Generate a token for the given user_id

# File lib/colossus.rb, line 103
def generate_user_token(user_id)
  verifier.generate_user_token(user_id)
end
get(user_id) click to toggle source

Get the status of a specified user, it analyzes the status

of all the sessions.
It returns :

- active if one or more clients are active.
- away if one or more clients are away.
- disconnected.

@param user_id [#to_s] The unique identifier of a user

@return Hash{String => String} User_id key, status value

# File lib/colossus.rb, line 82
def get(user_id)
  engine.get(user_id.to_s)
end
get_all() click to toggle source

(see get)

# File lib/colossus.rb, line 93
def get_all
  engine.get_all
end
get_multi(*user_ids) click to toggle source

@param user_ids [Array<#to_s>] An array of user ids (see get)

# File lib/colossus.rb, line 88
def get_multi(*user_ids)
  engine.get_multi(*user_ids.map(&:to_s))
end
reset!() click to toggle source

Reset all the data (useful for specs)

# File lib/colossus.rb, line 98
def reset!
  engine.reset!
end
set(user_id, client_id, status) click to toggle source

Set the status of a user on a specificed client. A client could be a Websocket session (if the user has 2 tabs opened) or anything else.

@param user_id [#to_s] The unique identifier of a user @param client_id [#to_s] The unique identifier of a client @param status [#to_s] The status of a the user, it can be active,

away or disconnected.

@return [Boolean] Return true if the status has changed if not false.

# File lib/colossus.rb, line 67
def set(user_id, client_id, status)
  engine.set(user_id.to_s, client_id.to_s, status.to_s)
end
update(user_id, status) click to toggle source

Method used when the engine notify a change

@!visibility private

# File lib/colossus.rb, line 110
def update(user_id, status)
  changed
  notify_observers(user_id, status)
end