Module: TwitterWithAutoPagination::Collector

Included in:
REST::Utils
Defined in:
lib/twitter_with_auto_pagination/collector.rb

Instance Method Summary collapse

Instance Method Details

#collect_with_cursor(collection = [], cursor = nil, &block) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/twitter_with_auto_pagination/collector.rb', line 11

def collect_with_cursor(collection = [], cursor = nil, &block)
  response = yield(cursor)
  return collection if response.nil?

  # Notice: If you call response.to_a, it automatically fetch all results and the results are not cached.
  collection += (response.attrs[:ids] || response.attrs[:users] || response.attrs[:lists])
  response.attrs[:next_cursor].zero? ? collection.flatten : collect_with_cursor(collection, response.attrs[:next_cursor], &block)
end

#collect_with_max_id(collection = [], max_id = nil, &block) ⇒ Object



4
5
6
7
8
9
# File 'lib/twitter_with_auto_pagination/collector.rb', line 4

def collect_with_max_id(collection = [], max_id = nil, &block)
  tweets = yield(max_id)
  return collection if tweets.nil?
  collection += tweets
  tweets.empty? ? collection.flatten : collect_with_max_id(collection, tweets.last.id - 1, &block)
end