Sha256: 438bd3d83ef04ace01cf3ffca1c24260e6cefe1200063777e7238e1d2e8b1910

Contents?: true

Size: 1.51 KB

Versions: 5

Compression:

Stored size: 1.51 KB

Contents

require 'twitter'
require 'retryable'

module T
  module Collectable

    MAX_NUM_RESULTS = 200

    def collect_with_cursor(collection=[], cursor=-1, &block)
      object = retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
        yield(cursor)
      end
      collection += object.collection
      object.last? ? collection.flatten : collect_with_cursor(collection, object.next_cursor, &block)
    end

    def collect_with_max_id(collection=[], max_id=nil, &block)
      tweets = retryable(:tries => 3, :on => Twitter::Error::ServerError, :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, &block)
      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=[], page=1, &block)
      tweets = retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
        yield page
      end
      return collection if tweets.nil?
      collection += tweets
      tweets.empty? ? collection.flatten.uniq : collect_with_page(collection, page + 1, &block)
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
t-1.7.2 lib/t/collectable.rb
t-1.7.1 lib/t/collectable.rb
t-1.7.0 lib/t/collectable.rb
t-1.6.0 lib/t/collectable.rb
t-1.5.1 lib/t/collectable.rb