Module: Pubnub::Client::PagedHistory

Included in:
Pubnub::Client
Defined in:
lib/pubnub/client/paged_history.rb

Overview

Module that holds generator for all events

Instance Method Summary (collapse)

Instance Method Details

- (Object) paged_history(options = {}, &block)



7
8
9
10
11
12
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
# File 'lib/pubnub/client/paged_history.rb', line 7

def paged_history(options = {}, &block)
  channel  = options[:channel]
  page     = options[:page]      || 1
  limit    = options[:limit]     || 100
  callback = options[:callback]  || block
  sync     = options[:http_sync] ? true : false
  start_tt = options[:start]     || nil
  end_tt   = options[:end]       || nil

  current_start_tt = start_tt

  if sync
    envelopes = []
    page.times do |i|
      Pubnub.logger.debug('Pubnub::Client'){"\n\nFetching page no. #{i}"}
      Pubnub.logger.debug('Pubnub::Client'){"Current start tt #{current_start_tt}\n"}
      envelopes << history(:channel => channel, :http_sync => true, :count => limit, :start => current_start_tt, :end => end_tt)
      envelopes.flatten!

      Pubnub.logger.debug('Pubnub::Client'){"\n\nHistroy start: #{envelopes.last.history_start}"}
      Pubnub.logger.debug('Pubnub::Client'){"History end: #{envelopes.last.history_end}\n"}
      current_start_tt = envelopes.last.history_start.to_i

      unless i == page - 1
        envelopes = []
      end
    end

    envelopes.flatten!
    envelopes.each do |envelope|
      callback.call envelope
    end if callback
    envelopes
  else
    Celluloid::Future.new do
      sync_options = options.dup
      sync_options[:http_sync] = true
      self.paged_history(sync_options, &block)
    end
  end
end