Class: Slanger::Channel
- Inherits:
-
Object
- Object
- Slanger::Channel
- Extended by:
- Forwardable
- Defined in:
- lib/slanger/channel.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#channel_id ⇒ Object
readonly
Returns the value of attribute channel_id.
Class Method Summary collapse
- .all ⇒ Object
- .create(params = {}) ⇒ Object
- .from(channel_id) ⇒ Object
- .lookup(channel_id) ⇒ Object
- .send_client_message(msg) ⇒ Object
- .unsubscribe(channel_id, subscription_id) ⇒ Object
Instance Method Summary collapse
- #authenticated? ⇒ Boolean
- #channel ⇒ Object
-
#dispatch(message, channel) ⇒ Object
Send an event received from Redis to the EventMachine channel which will send it to subscribed clients.
-
#initialize(attrs) ⇒ Channel
constructor
A new instance of Channel.
-
#send_client_message(message) ⇒ Object
Send a client event to the EventMachine channel.
- #subscribe(*a, &blk) ⇒ Object
- #unsubscribe(*a, &blk) ⇒ Object
Constructor Details
Instance Attribute Details
#channel_id ⇒ Object (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
.all ⇒ Object
37 38 39 |
# File 'lib/slanger/channel.rb', line 37 def all @all ||= Hash.new { |h, k| h[k] = [] } end |
.create(params = {}) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/slanger/channel.rb', line 30 def create(params = {}) new(params).tap do |channel| channel_id = channel.channel_id all[channel_id] << channel end 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[channel_id]&.first end |
.send_client_message(msg) ⇒ Object
45 46 47 |
# File 'lib/slanger/channel.rb', line 45 def (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
92 93 94 |
# File 'lib/slanger/channel.rb', line 92 def authenticated? channel_id =~ /\Aprivate-/ || channel_id =~ /\Apresence-/ end |
#channel ⇒ Object
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(, channel) push(Oj.dump(, mode: :compat)) unless channel =~ /\Aslanger:/ perform_client_webhook!() 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 () Slanger::Redis.publish(["channel"], Oj.dump(, 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.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
68 69 70 71 72 73 74 75 |
# File 'lib/slanger/channel.rb', line 68 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 |