Class: PusherClient::Subscription

Inherits:
Object
  • Object
show all
Defined in:
lib/pusher-client/subscription.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel_name, user_data) ⇒ Subscription

Returns a new instance of Subscription.



6
7
8
9
10
11
12
13
# File 'lib/pusher-client/subscription.rb', line 6

def initialize(channel_name, user_data)
  @channel = channel_name
  @user_data = user_data
  @global = false
  @callbacks = {}
  @global_callbacks = {}
  @subscribed = false
end

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



4
5
6
# File 'lib/pusher-client/subscription.rb', line 4

def callbacks
  @callbacks
end

#channelObject (readonly)

Returns the value of attribute channel.



4
5
6
# File 'lib/pusher-client/subscription.rb', line 4

def channel
  @channel
end

#globalObject

Returns the value of attribute global.



3
4
5
# File 'lib/pusher-client/subscription.rb', line 3

def global
  @global
end

#global_callbacksObject (readonly)

Returns the value of attribute global_callbacks.



4
5
6
# File 'lib/pusher-client/subscription.rb', line 4

def global_callbacks
  @global_callbacks
end

#subscribedObject

Returns the value of attribute subscribed.



3
4
5
# File 'lib/pusher-client/subscription.rb', line 3

def subscribed
  @subscribed
end

#user_dataObject (readonly)

Returns the value of attribute user_data.



4
5
6
# File 'lib/pusher-client/subscription.rb', line 4

def user_data
  @user_data
end

Instance Method Details

#acknowledge_subscription(data) ⇒ Object



48
49
50
# File 'lib/pusher-client/subscription.rb', line 48

def acknowledge_subscription(data)
  @subscribed = true
end

#bind(event_name, &callback) ⇒ Object



15
16
17
18
19
# File 'lib/pusher-client/subscription.rb', line 15

def bind(event_name, &callback)
  @callbacks[event_name] = callbacks[event_name] || []
  @callbacks[event_name] << callback
  return self
end

#dispatch(event_name, data) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/pusher-client/subscription.rb', line 26

def dispatch(event_name, data)
  PusherClient.logger.debug "Dispatching callbacks for #{event_name}"
  if @callbacks[event_name]
    @callbacks[event_name].each do |callback|
      callback.call(data)
    end
  else
    PusherClient.logger.debug "No callbacks to dispatch for #{event_name}"
  end
end

#dispatch_global_callbacks(event_name, data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/pusher-client/subscription.rb', line 37

def dispatch_global_callbacks(event_name, data)
  if @global_callbacks[event_name]
    PusherClient.logger.debug "Dispatching global callbacks for #{event_name}"
    @global_callbacks[event_name].each do |callback|
      callback.call(data)
    end
  else
    PusherClient.logger.debug "No global callbacks to dispatch for #{event_name}"
  end
end

#dispatch_with_all(event_name, data) ⇒ Object



21
22
23
24
# File 'lib/pusher-client/subscription.rb', line 21

def dispatch_with_all(event_name, data)
  dispatch(event_name, data)
  dispatch_global_callbacks(event_name, data)
end