Class: Colossus

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/colossus.rb,
lib/colossus/version.rb,
lib/colossus/verifier.rb,
lib/colossus/configuration.rb,
lib/colossus/faye/extension.rb,
lib/colossus/http_writer_client.rb,
lib/colossus/engines/redis/redis.rb,
lib/colossus/simple_writer_server.rb,
lib/colossus/engines/memory/memory.rb,
lib/colossus/engines/memory/client_session.rb,
lib/colossus/engines/memory_monitor/memory.rb,
lib/colossus/engines/memory_thread_safe/memory.rb,
lib/colossus/engines/memory/client_session_store.rb,
lib/colossus/engines/memory_thread_safe/client_session.rb,
lib/colossus/engines/memory_thread_safe/client_session_store.rb

Overview

Main Colossus module

Defined Under Namespace

Modules: Engine, Faye Classes: Configuration, HTTPWriterClient, SimpleWriterServer, Verifier

Constant Summary collapse

ACTIVE =
'active'.freeze
AWAY =
'away'.freeze
DISCONNECTED =
'disconnected'.freeze
VERSION =
'0.11.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ttl = Colossus.config.ttl, engine = Colossus.config.engine, secret = Colossus.config.secret_key, writer_token = Colossus.config.writer_token) ⇒ Colossus

Initialize Colossus

Parameters:

  • ttl (Integer) (defaults to: Colossus.config.ttl)

    the seconds before a user without emitting heartbeat is considered disconnected

  • engine (Engine) (defaults to: Colossus.config.engine)

    the engine which implements the needed methods to work with Colossus



49
50
51
52
53
54
55
56
# 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

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



38
39
40
# File 'lib/colossus.rb', line 38

def engine
  @engine
end

#verifierObject (readonly)

Returns the value of attribute verifier.



38
39
40
# File 'lib/colossus.rb', line 38

def verifier
  @verifier
end

Class Method Details

.configObject



27
28
29
# File 'lib/colossus/configuration.rb', line 27

def self.config
  configuration
end

.config=(configuration) ⇒ Object

rubocop:enable



38
39
40
# File 'lib/colossus/configuration.rb', line 38

def self.config=(configuration)
  self.configuration = configuration
end

.configurationObject



23
24
25
# File 'lib/colossus/configuration.rb', line 23

def self.configuration
  @configuration ||= Configuration.new
end

.configuration=(configuration) ⇒ Object

Help ? rubocop:disable TrivialAccessors



33
34
35
# File 'lib/colossus/configuration.rb', line 33

def self.configuration=(configuration)
  @configuration = configuration
end

.configure {|configuration| ... } ⇒ Object

class Configuration

Yields:



18
19
20
21
# File 'lib/colossus/configuration.rb', line 18

def self.configure
  yield(configuration) if block_given?
  configuration
end

.reset_configurationObject



42
43
44
# File 'lib/colossus/configuration.rb', line 42

def self.reset_configuration
  @configuration = Configuration.new
end

Instance Method Details

#generate_user_token(user_id) ⇒ Object

Generate a token for the given user_id



103
104
105
# File 'lib/colossus.rb', line 103

def generate_user_token(user_id)
  verifier.generate_user_token(user_id)
end

#get(user_id) ⇒ Object

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.

Parameters:

  • user_id (#to_s)

    The unique identifier of a user



82
83
84
# File 'lib/colossus.rb', line 82

def get(user_id)
  engine.get(user_id.to_s)
end

#get_allObject

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.

Parameters:

  • user_id (#to_s)

    The unique identifier of a user



93
94
95
# File 'lib/colossus.rb', line 93

def get_all
  engine.get_all
end

#get_multi(*user_ids) ⇒ Object

(see #get)

Parameters:

  • user_ids (Array<#to_s>)

    An array of user ids



88
89
90
# File 'lib/colossus.rb', line 88

def get_multi(*user_ids)
  engine.get_multi(*user_ids.map(&:to_s))
end

#reset!Object

Reset all the data (useful for specs)



98
99
100
# File 'lib/colossus.rb', line 98

def reset!
  engine.reset!
end

#set(user_id, client_id, status) ⇒ Boolean

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.

Parameters:

  • user_id (#to_s)

    The unique identifier of a user

  • client_id (#to_s)

    The unique identifier of a client

  • status (#to_s)

    The status of a the user, it can be active, away or disconnected.

Returns:

  • (Boolean)

    Return true if the status has changed if not false.



67
68
69
# 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