Class: PusherFake::Channel::Public

Inherits:
Object
  • Object
show all
Defined in:
lib/pusher-fake/channel/public.rb

Direct Known Subclasses

Private

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Public

Create a new PusherFake::Channel::Public object.

Parameters:

  • name (String)

    The channel name.



13
14
15
16
# File 'lib/pusher-fake/channel/public.rb', line 13

def initialize(name)
  @name        = name
  @connections = []
end

Instance Attribute Details

#connectionsArray (readonly)

Returns Connections in this channel.

Returns:

  • (Array)

    Connections in this channel.



5
6
7
# File 'lib/pusher-fake/channel/public.rb', line 5

def connections
  @connections
end

#nameString (readonly)

Returns The channel name.

Returns:

  • (String)

    The channel name.



8
9
10
# File 'lib/pusher-fake/channel/public.rb', line 8

def name
  @name
end

Instance Method Details

#add(connection, options = {}) ⇒ Object

Add the connection to the channel.

Parameters:

  • connection (Connection)

    The connection to add.

  • options (Hash) (defaults to: {})

    The options for the channel.



22
23
24
# File 'lib/pusher-fake/channel/public.rb', line 22

def add(connection, options = {})
  subscription_succeeded(connection, options)
end

#emit(event, data, options = {}) ⇒ Object

Emit an event to the channel.

Parameters:

  • event (String)

    The event name.

  • data (Hash)

    The event data.



30
31
32
33
34
35
36
# File 'lib/pusher-fake/channel/public.rb', line 30

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) ⇒ Boolean

Determine if the connection is in the channel.

Parameters:

Returns:

  • (Boolean)

    true if the connection is in the channel, false otherwise.



42
43
44
# File 'lib/pusher-fake/channel/public.rb', line 42

def includes?(connection)
  connections.index(connection)
end

#remove(connection) ⇒ Object

Remove the connection from the channel.

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

Parameters:

  • connection (Connection)

    The connection to remove.



51
52
53
54
55
56
57
# File 'lib/pusher-fake/channel/public.rb', line 51

def remove(connection)
  connections.delete(connection)

  if connections.empty?
    trigger("channel_vacated", channel: name)
  end
end

#subscription_dataHash

This method is abstract.

Return subscription data for the channel.

Returns:

  • (Hash)

    Subscription data for the channel.



63
64
65
# File 'lib/pusher-fake/channel/public.rb', line 63

def subscription_data
  {}
end