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
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 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
90 91 92 |
# File 'lib/slanger/channel.rb', line 90 def authenticated? channel_id =~ /\Aprivate-/ || channel_id =~ /\Apresence-/ end |
#channel ⇒ Object
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(, 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.
78 79 80 |
# File 'lib/slanger/channel.rb', line 78 def () Slanger::Redis.publish(['channel'], Oj.dump(, 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 |