gpt4 book ai didi

ruby - sinatra 和 tweetstream 不能一起工作

转载 作者:太空宇宙 更新时间:2023-11-03 18:16:55 25 4
gpt4 key购买 nike

我正在 try catch 特定主题标签的 Twitter 流,以将其作为另一个 http 流传递。我写的代码(我是 ruby​​ 的新手)看起来像这样:

    require 'tweetstream'
require 'sinatra'

set :server, 'webrick'
connections = []

TweetStream.configure do |config|
config.consumer_key = 'XXX'
config.consumer_secret = 'XXX'
config.oauth_token = 'XXX'
config.oauth_token_secret = 'XXX'
config.auth_method = :oauth
end


get '/hi' do
"Hello World!"
end

get '/evented', provides: 'text/event-stream' do
stream :keep_open do |out|
connections.push out
out.callback { connections.delete( out ) }
end
end

TweetStream::Client.new.track('#worldcup') do |status|
connections.each do |out|
out << "#{status.text}"
end
end

当我运行这个程序时,我得到:

    C:/Ruby193/lib/ruby/gems/1.9.1/gems/em-twitter-0.3.3/lib/em-twitter/client.rb:25: warning: `*' interpreted as argument prefix
C:/Ruby193/lib/ruby/gems/1.9.1/gems/em-twitter-0.3.3/lib/em-twitter/connection.rb:268: warning: assigned but unused variable - e
C:/Ruby193/lib/ruby/gems/1.9.1/gems/buftok-0.2.0/lib/buftok.rb:15: warning: method redefined; discarding old initialize
C:/Ruby193/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.3-x86-mingw32/lib/em/buftok.rb:25: warning: previous definition of initialize was here
C:/Ruby193/lib/ruby/gems/1.9.1/gems/buftok-0.2.0/lib/buftok.rb:30: warning: method redefined; discarding old extract
C:/Ruby193/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.3-x86-mingw32/lib/em/buftok.rb:49: warning: previous definition of extract was here
C:/Ruby193/lib/ruby/gems/1.9.1/gems/buftok-0.2.0/lib/buftok.rb:52: warning: method redefined; discarding old flush
C:/Ruby193/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.3-x86-mingw32/lib/em/buftok.rb:100: warning: previous definition of flush was here
C:/Ruby193/lib/ruby/gems/1.9.1/gems/naught-1.0.0/lib/naught/null_class_builder/commands/predicates_return.rb:38: warning: method redefined; discarding old respond_to?
C:/Ruby193/lib/ruby/gems/1.9.1/gems/naught-1.0.0/lib/naught/null_class_builder.rb:105: warning: previous definition of respond_to? was here
C:/Ruby193/lib/ruby/gems/1.9.1/gems/daemons-1.1.9/lib/daemons/pidfile.rb:39: warning: (...) interpreted as grouped expression
C:/Ruby193/lib/ruby/gems/1.9.1/gems/daemons-1.1.9/lib/daemons/daemonize.rb:59: warning: assigned but unused variable - sess_id
C:/Ruby193/lib/ruby/gems/1.9.1/gems/daemons-1.1.9/lib/daemons/daemonize.rb:102: warning: assigned but unused variable - pid
C:/Ruby193/lib/ruby/gems/1.9.1/gems/daemons-1.1.9/lib/daemons/application.rb:337: warning: mismatched indentations at 'end' with 'def' at 326
C:/Ruby193/lib/ruby/gems/1.9.1/gems/daemons-1.1.9/lib/daemons/application_group.rb:80: warning: assigned but unused variable - pid
C:/Ruby193/lib/ruby/gems/1.9.1/gems/em-twitter-0.3.3/lib/em-twitter/request.rb:49: warning: instance variable @proxy not initialized
C:/Ruby193/lib/ruby/gems/1.9.1/gems/em-twitter-0.3.3/lib/em-twitter/request.rb:49: warning: instance variable @proxy not initialized
C:/Ruby193/lib/ruby/gems/1.9.1/gems/em-twitter-0.3.3/lib/em-twitter/request.rb:49: warning: instance variable @proxy not initialized
C:/Ruby193/lib/ruby/gems/1.9.1/gems/em-twitter-0.3.3/lib/em-twitter/request.rb:49: warning: instance variable @proxy not initialized

奇怪的是,如果我只使用 sinatra 并删除“TweetStream::Client.new.track”部分,一切正常。仅使用 tweetstream 在控制台中输出也是如此。但是,如果我同时使用它们,我将无法连接到浏览器。

最佳答案

老实说我无法重现此警告。我想这可能是因为我正在使用 ruby​​ 2.1.1p76 或一些更新 gems 或因为我的操作系统。我不知道。

简单重写您的想法,无需保持联系。只需等待推特的回复即可。

require 'tweetstream'
require 'sinatra'

set :server, 'webrick'

TweetStream.configure do |config|
config.consumer_key = 'xxxx'
config.consumer_secret = 'xxxx'
config.oauth_token = 'xxxx'
config.oauth_token_secret = 'xxxx'
config.auth_method = :oauth
end

get '/:hashtag', provides: 'text/event-stream' do
stream :keep_open do |out|
out << random_tweet_about(params[:hashtag])
end
end

def random_tweet_about(hashtag)
TweetStream::Client.new.track(hashtag) do |tweet|
return tweet.text
end
end

关于ruby - sinatra 和 tweetstream 不能一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24621407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com