Module: TwitterFriendly::REST::Collector

Included in:
API
Defined in:
lib/twitter_friendly/rest/collector.rb

Instance Method Summary collapse

Instance Method Details

#collect_with_cursor(collection, cursor, &block) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/twitter_friendly/rest/collector.rb', line 58

def collect_with_cursor(collection, cursor, &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.concat (response[:ids] || response[:users] || response[:lists])
  response[:next_cursor].zero? ? collection.flatten : collect_with_cursor(collection, response[:next_cursor], &block)
end

#collect_with_max_id(collection, max_id, collect_options, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/twitter_friendly/rest/collector.rb', line 41

def collect_with_max_id(collection, max_id, collect_options, &block)
  tweets = yield(max_id)
  return collection if tweets.nil?

  collection.concat tweets
  if tweets.empty? || (collect_options[:call_count] -= 1) < 1
    collection.flatten
  else
    collect_with_max_id(collection, tweets.last[:id] - 1, collect_options, &block)
  end
end

#fetch_resources_with_cursor(method_name, *args) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/twitter_friendly/rest/collector.rb', line 32

def fetch_resources_with_cursor(method_name, *args)
  options = args.dup.extract_options!

  collect_with_cursor([], -1) do |next_cursor|
    options[:cursor] = next_cursor unless next_cursor.nil?
    send(method_name, *args)
  end
end

#fetch_tweets_with_max_id(method_name, max_count, *args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/twitter_friendly/rest/collector.rb', line 4

def fetch_tweets_with_max_id(method_name, max_count, *args)
  options = args.dup.extract_options!

  total_count = options.delete(:count) || max_count
  call_count = total_count / max_count + (total_count % max_count == 0 ? 0 : 1)
  options[:count] = [max_count, total_count].min
  collect_options = {call_count: call_count, total_count: total_count}

  collect_with_max_id([], nil, collect_options) do |max_id|
    options[:max_id] = max_id unless max_id.nil?
    result = send(method_name, *args)

    if method_name == :search
      result.attrs[:statuses]
    else
      if result.is_a?(Array) && result[0].respond_to?(:attrs)
        result.map(&:attrs)
      else
        result
      end
    end
  end
end