Class: FayeTracking::FayeExtension

Inherits:
Object
  • Object
show all
Defined in:
lib/faye_tracking/faye_extension.rb

Constant Summary collapse

MONITORED_CHANNELS =
[
  '/meta/subscribe',
  '/meta/unsubscribe',
  '/meta/disconnect'
]

Instance Method Summary collapse

Constructor Details

#initialize(tracker) ⇒ FayeExtension

Returns a new instance of FayeExtension.



9
10
11
# File 'lib/faye_tracking/faye_extension.rb', line 9

def initialize(tracker)
  @tracker = tracker
end

Instance Method Details

#incoming(message, callback) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/faye_tracking/faye_extension.rb', line 13

def incoming(message, callback)
  return callback.call(message) \
    unless MONITORED_CHANNELS.include? message['channel']

  FayeTracking.logger.debug "received incoming message: #{message}"

  unless message['error']
    subs_channel = message['subscription']
    client_id    = message['clientId']
    run_subscribe_callbacks = false

    case message['channel']
    when '/meta/subscribe'
      ext = message['ext']
      if app_client_id = ext['faye_tracking_client_id']
        @tracker.add(subs_channel, client_id, app_client_id)
        run_subscribe_callbacks = true
      else
        error_message = "missing ext['faye_tracking_client_id']"
        FayeTracking.logger.error "error with message: #{error_message}"
        message['error'] = error_message
      end
    when '/meta/unsubscribe'
      # This is not reliable, more robust way to detect unsubscribe event.
      # see: http://faye.jcoglan.com/ruby/monitoring.html
    end
  end

  callback.call(message)

  if run_subscribe_callbacks
    FayeTracking.run_on_subscribe_callbacks(
      client_id, app_client_id, subs_channel
    )
  end
end