Class: FayeTracking::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/faye_tracking/tracker.rb

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ Tracker

Returns a new instance of Tracker.



3
4
5
6
7
8
# File 'lib/faye_tracking/tracker.rb', line 3

def initialize(redis)
  @channel_to_client_ids = namespaced_key_list('channel_to_client_ids', redis)
  @client_id_to_channels = namespaced_key_list('client_id_to_channels', redis)
  @user_to_client_ids    = namespaced_key_list('user_to_client_ids',    redis)
  @client_id_to_users    = namespaced_key_list('client_id_to_users',    redis)
end

Instance Method Details

#add(channel, client_id, user) ⇒ Object



10
11
12
13
14
15
# File 'lib/faye_tracking/tracker.rb', line 10

def add(channel, client_id, user)
  @channel_to_client_ids.add(channel, client_id)
  @client_id_to_channels.add(client_id, channel)
  @user_to_client_ids.add(user, client_id)
  @client_id_to_users.add(client_id, user)
end

#channel_has_user?(channel, user) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
# File 'lib/faye_tracking/tracker.rb', line 62

def channel_has_user?(channel, user)
  client_ids = @channel_to_client_ids.members(channel)
  client_ids.any? do |client_id|
    @user_to_client_ids.member? user, client_id
  end
end

#channels_for_user(user) ⇒ Object



41
42
43
44
45
46
# File 'lib/faye_tracking/tracker.rb', line 41

def channels_for_user(user)
  client_ids = @user_to_client_ids.members(user)
  client_ids.inject([]) do |acc, client_id|
    acc += @client_id_to_channels.members(client_id)
  end.uniq
end

#remove(channel, client_id) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/faye_tracking/tracker.rb', line 21

def remove(channel, client_id)
  @channel_to_client_ids.remove(channel, client_id)
  @client_id_to_channels.remove(client_id, channel)
  @client_id_to_users.members(client_id).each do |user|
    @user_to_client_ids.remove(user, client_id)
  end
  @client_id_to_users.remove_all(client_id)
end

#remove_all(client_id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/faye_tracking/tracker.rb', line 30

def remove_all(client_id)
  @client_id_to_channels.members(client_id).each do |channel|
    @channel_to_client_ids.remove(channel, client_id)
  end
  @client_id_to_users.members(client_id).each do |user|
    @user_to_client_ids.remove(user, client_id)
  end
  @client_id_to_users.remove_all(client_id)
  @client_id_to_channels.remove_all(client_id)
end

#user_in_channel?(user, channel) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
# File 'lib/faye_tracking/tracker.rb', line 55

def user_in_channel?(user, channel)
  client_ids = @user_to_client_ids.members(user)
  client_ids.any? do |client_id|
    @channel_to_client_ids.member? channel, client_id
  end
end

#user_with_client_id(client_id) ⇒ Object



17
18
19
# File 'lib/faye_tracking/tracker.rb', line 17

def user_with_client_id(client_id)
  @client_id_to_users.members(client_id).first
end

#users_in_channel(channel) ⇒ Object



48
49
50
51
52
53
# File 'lib/faye_tracking/tracker.rb', line 48

def users_in_channel(channel)
  client_ids = @channel_to_client_ids.members(channel)
  client_ids.inject([]) do |acc, client_id|
    acc += @client_id_to_users.members(client_id)
  end.uniq
end