Sha256: 4a6aad6ea485b94f1bc9b3c23318a6f59f8d7d667204b49f799ca0dc0bc2cb2e

Contents?: true

Size: 771 Bytes

Versions: 1

Compression:

Stored size: 771 Bytes

Contents

require "twitter"

module AllTweets
  class Fetcher
    def initialize(arg)
      @client = case arg
      when Hash
        Twitter::REST::Client.new(arg)
      else
        arg
      end
    end

    def fetch_all_tweets(user, include_retweets: true)
      collect_with_max_id do |max_id|
        options = {count: 200, include_rts: include_retweets}
        options[:max_id] = max_id unless max_id.nil?
        @client.user_timeline(user, options)
      end
    end

    private

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alltweets-2.0.0 lib/alltweets/fetcher.rb