Class: Sinapse::Channels

Inherits:
Struct
  • Object
show all
Defined in:
lib/sinapse/channels.rb

Overview

TODO: #access_token to return the current user token (or generate one if missing)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#recordObject

Returns the value of attribute record

Returns:

  • (Object)

    the current value of record



3
4
5
# File 'lib/sinapse/channels.rb', line 3

def record
  @record
end

Instance Method Details

#add_channel(channel) ⇒ Object



16
17
18
19
20
21
# File 'lib/sinapse/channels.rb', line 16

def add_channel(channel)
  Sinapse.redis do |redis|
    redis.sadd(key, channel_for(channel))
    redis.publish(key(:add), channel_for(channel))
  end
end

#authObject



4
5
6
# File 'lib/sinapse/channels.rb', line 4

def auth
  @auth ||= Authentication.new(record)
end

#channel_for(record) ⇒ Object



41
42
43
# File 'lib/sinapse/channels.rb', line 41

def channel_for(record)
  record.is_a?(String) ? record : record.sinapse_channel
end

#channelsObject



8
9
10
# File 'lib/sinapse/channels.rb', line 8

def channels
  Sinapse.redis { |redis| redis.smembers(key) }
end

#clearObject

Removes all channels at once.



31
32
33
# File 'lib/sinapse/channels.rb', line 31

def clear
  channels.each { |channel| remove_channel(channel) }
end

#destroyObject

Removes all channels and clears authentication.



36
37
38
39
# File 'lib/sinapse/channels.rb', line 36

def destroy
  channels.each { |channel| remove_channel(channel) }
  auth.clear
end

#has_channel?(channel) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/sinapse/channels.rb', line 12

def has_channel?(channel)
  Sinapse.redis { |redis| redis.sismember(key, channel_for(channel)) }
end

#key(extra = nil) ⇒ Object



45
46
47
48
49
# File 'lib/sinapse/channels.rb', line 45

def key(extra = nil)
  key = "sinapse:channels:#{record.to_param}"
  key += ":#{extra}" if extra
  key
end

#remove_channel(channel) ⇒ Object



23
24
25
26
27
28
# File 'lib/sinapse/channels.rb', line 23

def remove_channel(channel)
  Sinapse.redis do |redis|
    redis.srem(key, channel_for(channel))
    redis.publish(key(:remove), channel_for(channel))
  end
end