Sha256: 08a794ffb72515ecb7c3f824cddf5e43a304284e793c078b94f5aa68c771b1d8

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

module TwitterToCsv
  class TwitterWatcher
    attr_accessor :username, :password, :filter, :fetch_errors

    def initialize(options)
      @username = options[:username]
      @password = options[:password]
      @filter = options[:filter]
      @fetch_errors = 0
    end

    def progress(str)
      STDERR.print "#{str}..."
      STDERR.flush
      yield
      STDERR.puts "done."
    end

    def run(&block)
      EventMachine::run do
        stream = Twitter::JSONStream.connect(
          :path    => "/1/statuses/#{(filter && filter.length > 0) ? 'filter' : 'sample'}.json#{"?track=#{filter.join(",")}" if filter && filter.length > 0}",
          :auth    => "#{username}:#{password}",
          :ssl     => true
        )

        stream.each_item do |item|
          handle_status JSON.parse(item), block
        end

        stream.on_error do |message|
          STDERR.puts " --> Twitter error: #{message} <--"
        end

        stream.on_max_reconnects do |timeout, retries|
          STDERR.puts " --> Oops, tried too many times! <--"
          EventMachine::stop_event_loop
        end
      end
    end

    def handle_status(status, block)
      return unless status
      return if status.has_key?('delete')
      status['text'] = status['text'].gsub(/&lt;/, "<").gsub(/&gt;/, ">").gsub(/[\t\n\r]/, '  ')
      block.call(status)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
twitter_to_csv-0.0.3 lib/twitter_to_csv/twitter_watcher.rb
twitter_to_csv-0.0.2 lib/twitter_to_csv/twitter_watcher.rb
twitter_to_csv-0.0.1 lib/twitter_to_csv/twitter_watcher.rb