1

i am trying to capture the twitter stream for a specific hashtag to pass it on as another http stream. the code i have written (i'm new to ruby) looks like this:

    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

when i run this program i get:

    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

the weird thing is that if i just use sinatra and remove the "TweetStream::Client.new.track" part, everything works fine. same goes for just using tweetstream to output in the console. however, if i use them both, i cant get a connection with a browser.

stephanlindauer
  • 1,910
  • 2
  • 14
  • 17

2 Answers2

2

tbh i can not reproduce this warning. i guess it could be because i am using ruby 2.1.1p76 or some updates gems or because of my os. i have no idea.

simple rewrite of your idea, without holding the connections. just wait for a reply from twitter.

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
1

As mentioned above the server should be set to thin.

Due to the event nature I'm not sure how the random_tweet_about function above would work.

I was able to get TweetStream working with Sinatra by wrapping TweetStream::Client.new in an EM.schedule block. Perhaps competing event loops require it being in another thread.

require 'sinatra'
require 'tweetstream'

set :server, 'thin'

twitter = YAML.load_file('twitter.yml')['twitter']

TweetStream.configure do |config|
  config.consumer_key       = twitter['CONSUMER_KEY']
  config.consumer_secret    = twitter['CONSUMER_SECRET']
  config.oauth_token        = twitter['ACCESS_TOKEN']
  config.oauth_token_secret = twitter['ACCESS_SECRET']
  config.auth_method        = :oauth
end

connections = []

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

EM.schedule do
  TweetStream::Client.new.track('#yolo') do |status|
    connections.each do |out|
      out << status.text
    end
  end
end
Dave
  • 479
  • 3
  • 13