Class: Restforce::Concerns::Streaming::ReplayExtension

Inherits:
Object
  • Object
show all
Defined in:
lib/restforce/concerns/streaming.rb

Instance Method Summary collapse

Constructor Details

#initialize(replay_handlers) ⇒ ReplayExtension

Returns a new instance of ReplayExtension.



61
62
63
# File 'lib/restforce/concerns/streaming.rb', line 61

def initialize(replay_handlers)
  @replay_handlers = replay_handlers
end

Instance Method Details

#incoming(message, callback) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/restforce/concerns/streaming.rb', line 65

def incoming(message, callback)
  callback.call(message).tap do
    channel = message.fetch('channel')
    replay_id = message.fetch('data', {}).fetch('event', {})['replayId']

    handler = @replay_handlers[channel]
    if !replay_id.nil? && !handler.nil? && handler.respond_to?(:[]=)
      # remember the last replay_id for this channel
      handler[channel] = replay_id
    end
  end
end

#outgoing(message, callback) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/restforce/concerns/streaming.rb', line 78

def outgoing(message, callback)
  # Leave non-subscribe messages alone
  return callback.call(message) unless message['channel'] == '/meta/subscribe'

  channel = message['subscription']

  # Set the replay value for the channel
  message['ext'] ||= {}
  message['ext']['replay'] = {
    channel => replay_id(channel)
  }

  # Carry on and send the message to the server
  callback.call message
end