Class: Slanger::Channel

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/slanger/channel.rb

Direct Known Subclasses

PresenceChannel

Constant Summary collapse

CHANNEL_SUBSCRIBER_COUNT_KEY =
"channel_subscriber_count"
CHANNEL_VACATED_DICT =
"channel_vacated_dict"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Channel

Returns a new instance of Channel.



50
51
52
53
# File 'lib/slanger/channel.rb', line 50

def initialize(attrs)
  @channel_id = attrs.with_indifferent_access[:channel_id]
  Slanger::Redis.subscribe channel_id
end

Instance Attribute Details

#channel_idObject (readonly)

Returns the value of attribute channel_id.



18
19
20
# File 'lib/slanger/channel.rb', line 18

def channel_id
  @channel_id
end

Class Method Details

.allObject



37
38
39
# File 'lib/slanger/channel.rb', line 37

def all
  @all ||= {}
end

.create(params = {}) ⇒ Object



31
32
33
34
35
# File 'lib/slanger/channel.rb', line 31

def create(params = {})
  new(params).tap do |channel|
    all[channel.channel_id] = channel
  end
end

.from(channel_id) ⇒ Object



21
22
23
24
25
# File 'lib/slanger/channel.rb', line 21

def from(channel_id)
  klass = channel_id[/\Apresence-/] ? PresenceChannel : Channel

  klass.lookup(channel_id) || klass.create(channel_id: channel_id)
end

.lookup(channel_id) ⇒ Object



27
28
29
# File 'lib/slanger/channel.rb', line 27

def lookup(channel_id)
  all[channel_id]
end

.send_client_message(msg) ⇒ Object



45
46
47
# File 'lib/slanger/channel.rb', line 45

def send_client_message(msg)
  from(msg["channel"]).try :send_client_message, msg
end

.unsubscribe(channel_id, subscription_id) ⇒ Object



41
42
43
# File 'lib/slanger/channel.rb', line 41

def unsubscribe(channel_id, subscription_id)
  from(channel_id).try :unsubscribe, subscription_id
end

Instance Method Details

#authenticated?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/slanger/channel.rb', line 92

def authenticated?
  channel_id =~ /\Aprivate-/ || channel_id =~ /\Apresence-/
end

#channelObject



55
56
57
# File 'lib/slanger/channel.rb', line 55

def channel
  @channel ||= EM::Channel.new
end

#dispatch(message, channel) ⇒ Object

Send an event received from Redis to the EventMachine channel which will send it to subscribed clients.



86
87
88
89
90
# File 'lib/slanger/channel.rb', line 86

def dispatch(message, channel)
  push(Oj.dump(message, mode: :compat)) unless channel =~ /\Aslanger:/

  perform_client_webhook!(message)
end

#send_client_message(message) ⇒ Object

Send a client event to the EventMachine channel. Only events to channels requiring authentication (private or presence) are accepted. Public channels only get events from the API.



80
81
82
# File 'lib/slanger/channel.rb', line 80

def send_client_message(message)
  Slanger::Redis.publish(message["channel"], Oj.dump(message, mode: :compat)) if authenticated?
end

#subscribe(*a, &blk) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/slanger/channel.rb', line 59

def subscribe(*a, &blk)
  Slanger::Redis.channel_attend([CHANNEL_SUBSCRIBER_COUNT_KEY], [channel_id]).
    callback do |value|
    Slanger::Webhook.post name: "channel_occupied", channel: channel_id if value == 1
  end

  channel.subscribe *a, &blk
end

#unsubscribe(*a, &blk) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/slanger/channel.rb', line 68

def unsubscribe(*a, &blk)
  Slanger::Redis.channel_leave([CHANNEL_SUBSCRIBER_COUNT_KEY], [channel_id]).
    callback do |value|
    Slanger::Webhook.post name: "channel_vacated", channel: channel_id if value == 1
  end

  channel.unsubscribe *a, &blk
end