Class: PusherFake::Channel::Public

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

Overview

A public channel.

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.



16
17
18
19
# File 'lib/pusher-fake/channel/public.rb', line 16

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

Instance Attribute Details

#connectionsArray (readonly)

Returns Connections in this channel.

Returns:

  • (Array)

    Connections in this channel.



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

def connections
  @connections
end

#nameString (readonly)

Returns The channel name.

Returns:

  • (String)

    The channel name.



11
12
13
# File 'lib/pusher-fake/channel/public.rb', line 11

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.



25
26
27
# File 'lib/pusher-fake/channel/public.rb', line 25

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.



33
34
35
36
37
38
39
# 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) ⇒ Boolean

Determine if the connection is in the channel.

Parameters:

Returns:

  • (Boolean)

    If the connection is in the channel or not.



45
46
47
# File 'lib/pusher-fake/channel/public.rb', line 45

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.



54
55
56
57
58
# 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_dataHash

This method is abstract.

Return subscription data for the channel.

Returns:

  • (Hash)

    Subscription data for the channel.



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

def subscription_data
  {}
end

#trigger(name, data = {}) ⇒ Object



68
69
70
# File 'lib/pusher-fake/channel/public.rb', line 68

def trigger(name, data = {})
  PusherFake::Webhook.trigger(name, data)
end