Sha256: ab478b894315b51c1f226af118be9d160463fda7c13a475af28ba2fd9406c12b

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

module Tw
  class Client::Stream
    def initialize(user=nil)
      user = Tw::Auth.get_or_regist_user user
      UserStream.configure do |config|
        config.consumer_key = Conf['consumer_key']
        config.consumer_secret = Conf['consumer_secret']
        config.oauth_token = user['access_token']
        config.oauth_token_secret = user['access_secret']
      end

      puts '-- waiting stream..'
      @client = UserStream::Client.new
    end

    def user_stream(&block)
      raise ArgumentError, 'block not given' unless block_given?
      @client.user do |m|
        if data = tweet?(m)
          yield data
        end
      end
    end

    def filter(*track_words, &block)
      raise ArgumentError, 'block not given' unless block_given?
      track_words = track_words.join(',')
      puts "track #{track_words}"
      @client.filter :track => track_words do |m|
        if data = tweet?(m)
          yield data
        end
      end
    end

    private
    def tweet?(chunk)
      return false unless chunk.user and chunk.user.screen_name and chunk.text and chunk.created_at and chunk.id
      {
        :id => chunk.id,
        :user => chunk.user.screen_name,
        :text => chunk.text,
        :time => (Time.parse chunk.created_at)
      }
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tw-0.2.0 lib/tw/client/stream.rb