Class: Slanger::Channel

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

Direct Known Subclasses

PresenceChannel

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Channel

Returns a new instance of Channel.



47
48
49
50
# File 'lib/slanger/channel.rb', line 47

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.



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

def channel_id
  @channel_id
end

Class Method Details

.allObject



34
35
36
# File 'lib/slanger/channel.rb', line 34

def all
  @all ||= []
end

.create(params = {}) ⇒ Object



30
31
32
# File 'lib/slanger/channel.rb', line 30

def create(params = {})
  new(params).tap { |r| all << r }
end

.from(channel_id) ⇒ Object



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

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



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

def lookup(channel_id)
  all.detect { |o| o.channel_id == channel_id }
end

.send_client_message(msg) ⇒ Object



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

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

.unsubscribe(channel_id, subscription_id) ⇒ Object



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

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

Instance Method Details

#authenticated?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/slanger/channel.rb', line 90

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

#channelObject



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

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.



84
85
86
87
88
# File 'lib/slanger/channel.rb', line 84

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.



78
79
80
# File 'lib/slanger/channel.rb', line 78

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

#subscribe(*a, &blk) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/slanger/channel.rb', line 56

def subscribe *a, &blk
  Slanger::Redis.hincrby('channel_subscriber_count', channel_id, 1).
    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



65
66
67
68
69
70
71
72
# File 'lib/slanger/channel.rb', line 65

def unsubscribe *a, &blk
  Slanger::Redis.hincrby('channel_subscriber_count', channel_id, -1).
    callback do |value|
      Slanger::Webhook.post name: 'channel_vacated', channel: channel_id if value == 0
    end

  channel.unsubscribe *a, &blk
end