class PusherFake::Channel::Public

A public channel.

Attributes

connections[R]

@return [Array] Connections in this channel.

name[R]

@return [String] The channel name.

Public Class Methods

new(name) click to toggle source

Create a new {Public} object.

@param [String] name The channel name.

# File lib/pusher-fake/channel/public.rb, line 16
def initialize(name)
  @name        = name
  @connections = []
end

Public Instance Methods

add(connection, options = {}) click to toggle source

Add the connection to the channel.

@param [Connection] connection The connection to add. @param [Hash] options The options for the channel.

# File lib/pusher-fake/channel/public.rb, line 25
def add(connection, options = {})
  subscription_succeeded(connection, options)
end
emit(event, data, options = {}) click to toggle source

Emit an event to the channel.

@param [String] event The event name. @param [Hash] data The event data.

# File lib/pusher-fake/channel/public.rb, line 33
def emit(event, data, options = {})
  connections.each do |connection|
    unless connection.id == options[:socket_id]
      connection.emit(event, data, name)
    end
  end
end
includes?(connection) click to toggle source

Determine if the connection is in the channel.

@param [Connection] connection The connection. @return [Boolean] If the connection is in the channel or not.

# File lib/pusher-fake/channel/public.rb, line 45
def includes?(connection)
  connections.index(connection)
end
remove(connection) click to toggle source

Remove the connection from the channel.

If it is the last connection, trigger the channel_vacated webhook.

@param [Connection] connection The connection to remove.

# File lib/pusher-fake/channel/public.rb, line 54
def remove(connection)
  connections.delete(connection)

  trigger("channel_vacated", channel: name) if connections.empty?
end
subscription_data() click to toggle source

Return subscription data for the channel.

@abstract @return [Hash] Subscription data for the channel.

# File lib/pusher-fake/channel/public.rb, line 64
def subscription_data
  {}
end
trigger(name, data = {}) click to toggle source
# File lib/pusher-fake/channel/public.rb, line 68
def trigger(name, data = {})
  PusherFake::Webhook.trigger(name, data)
end

Private Instance Methods

subscription_succeeded(connection, _options = {}) click to toggle source

Notify the connection of the successful subscription and add the connection to the channel.

If it is the first connection, trigger the channel_occupied webhook.

@param [Connection] connection Connection a subscription succeeded for. @param [Hash] options The options for the channel.

# File lib/pusher-fake/channel/public.rb, line 81
def subscription_succeeded(connection, _options = {})
  connection.emit("pusher_internal:subscription_succeeded",
                  subscription_data, name)

  connections.push(connection)

  trigger("channel_occupied", channel: name) if connections.length == 1
end