Skip to content

Instantly share code, notes, and snippets.

@naniwaKun
Last active August 20, 2017 03:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naniwaKun/2910d0315fecb91676ab797687171e9d to your computer and use it in GitHub Desktop.
Save naniwaKun/2910d0315fecb91676ab797687171e9d to your computer and use it in GitHub Desktop.
tw クライアントのソース変更の提案 ref.https://twitter.com/naniwa_stg/status/881834161757081601
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 |m|
case m
when Twitter::Tweet
data = tweet?(m)
yield data
end
end
end
def filter(*track_words, &block)
raise ArgumentError, 'block not given' unless block_given?
@client.filter :track => track_words.join(',') do |m|
case m
when Twitter::Tweet
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 chu
nk.created_at and chunk.id
tweet = Tw::Tweet.new(:id => chunk.id,
:user => chunk.user.screen_name,
:text => chunk.text,
:time => chunk.created_at
)
return tweet
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment