Sha256: d91101d99e682b9895cfbb0346ddbe3089e498553d14f6bdb49b3e9fc7c556f1

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

require 'twitter'
require 'retryable'
require 'set'

module T
  module Collectable
    MAX_NUM_RESULTS = 200
    MAX_PAGE = 51

    def collect_with_max_id(collection = [], max_id = nil, &block)
      tweets = Retryable.retryable(tries: 3, on: Twitter::Error, sleep: 0) do
        yield(max_id)
      end
      return collection if tweets.nil?
      collection += tweets
      tweets.empty? ? collection.flatten : collect_with_max_id(collection, tweets.last.id - 1, &block)
    end

    def collect_with_count(count)
      opts = {}
      opts[:count] = MAX_NUM_RESULTS
      collect_with_max_id do |max_id|
        opts[:max_id] = max_id unless max_id.nil?
        opts[:count] = count unless count >= MAX_NUM_RESULTS
        if count > 0
          tweets = yield opts
          count -= tweets.length
          tweets
        end
      end.flatten.compact
    end

    def collect_with_page(collection = ::Set.new, page = 1, previous = nil, &block)
      tweets = Retryable.retryable(tries: 3, on: Twitter::Error, sleep: 0) do
        yield page
      end
      return collection if tweets.nil? || tweets == previous || page >= MAX_PAGE
      collection += tweets
      tweets.empty? ? collection.flatten : collect_with_page(collection, page + 1, tweets, &block)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
t-3.1.0 lib/t/collectable.rb
t-3.0.0 lib/t/collectable.rb
t-2.10.0 lib/t/collectable.rb