Sha256: 3e0fb85af6f89756dd3e5ff77a44e9dbf9c4bd9ca002bf506f57d9138225eed9

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

module Tw
  class Client::Stream
    def initialize(user=nil)
      user = Tw::Auth.get_or_regist_user user
      @client = Twitter::Streaming::Client.new do |config|
        config.consumer_key = Conf['consumer_key']
        config.consumer_secret = Conf['consumer_secret']
        config.access_token = user['access_token']
        config.access_token_secret = user['access_secret']
      end
    end

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

    def filter(*track_words, &block)
      raise ArgumentError, 'block not given' unless block_given?
      @client.filter :track => track_words.join(',') do |chunk|
        if tweet = get_if_tweet(chunk)
          yield tweet
        end
      end
    end

    private
    def get_if_tweet(chunk)
      return nil unless chunk.kind_of? Twitter::Tweet
      Tw::Tweet.new(:id => chunk.id,
                    :user => chunk.user.screen_name,
                    :text => chunk.text,
                    :time => chunk.created_at)
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tw-1.3.1 lib/tw/client/stream.rb
tw-1.3.0 lib/tw/client/stream.rb
tw-1.2.1 lib/tw/client/stream.rb
tw-1.2.0 lib/tw/client/stream.rb