Class: PusherFake::Channel::Presence

Inherits:
Private show all
Defined in:
lib/pusher-fake/channel/presence.rb

Instance Attribute Summary collapse

Attributes inherited from Public

#connections, #name

Instance Method Summary collapse

Methods inherited from Private

#add, #authentication_for, #authorized?

Methods inherited from Public

#add, #emit, #includes?

Constructor Details

#initialize(name) ⇒ Presence

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

Parameters:

  • name (String)

    The channel name.



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

def initialize(name)
  super

  @members = {}
end

Instance Attribute Details

#membersHash (readonly)

Returns Channel members hash.

Returns:

  • (Hash)

    Channel members hash.



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

def members
  @members
end

Instance Method Details

#remove(connection) ⇒ Object

Remove the connection from the channel and notify the channel.

Also trigger the member_removed webhook.

Parameters:

  • connection (Connection)

    The connection to remove.



21
22
23
24
25
26
27
28
29
# File 'lib/pusher-fake/channel/presence.rb', line 21

def remove(connection)
  super

  if members.key?(connection)
    trigger("member_removed", channel: name, user_id: members[connection][:user_id])

    emit("pusher_internal:member_removed", members.delete(connection))
  end
end

#subscription_dataHash

Return a hash containing presence information for the channel.

Returns:

  • (Hash)

    Hash containing presence information.



34
35
36
37
38
39
40
41
42
# File 'lib/pusher-fake/channel/presence.rb', line 34

def subscription_data
  hash = Hash[
    members.map { |_, member|
      [member[:user_id], member[:user_info]]
    }
  ]

  { presence: { hash: hash, count: members.size } }
end