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

Constant Summary collapse

CACHE_CHANNEL_PREFIX =
/^(private-|presence-){0,1}cache-/

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.



18
19
20
21
22
# File 'lib/pusher-fake/channel/public.rb', line 18

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

Instance Attribute Details

#connectionsArray (readonly)

Returns Connections in this channel.

Returns:

  • (Array)

    Connections in this channel.



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

def connections
  @connections
end

#nameString (readonly)

Returns The channel name.

Returns:

  • (String)

    The channel name.



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

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.



28
29
30
31
# File 'lib/pusher-fake/channel/public.rb', line 28

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

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

Emit an event to the channel.

Parameters:

  • event (String)

    The event name.

  • data (Hash)

    The event data.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pusher-fake/channel/public.rb', line 37

def emit(event, data, options = {})
  if cache_channel?
    @last_event = [event, data]
  end

  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.



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

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.



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

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.



72
73
74
# File 'lib/pusher-fake/channel/public.rb', line 72

def subscription_data
  {}
end

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



76
77
78
# File 'lib/pusher-fake/channel/public.rb', line 76

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