Your first approach was the best, you need to run "deamon" from the command line, but since you want custom rails and activerecord you need to load the rails environment into the script.
You need to do something like this:
#!/usr/bin/env ruby # encoding: utf-8 ENV["RAILS_ENV"] ||= "development" root = File.expand_path(File.join(File.dirname(__FILE__), '..')) require File.join(root, "config", "environment") require 'tweetstream' p "Initializing daemon..." TweetStream.configure do |config| config.consumer_key = 'your-consumer_key' config.consumer_secret = 'your-consumer_secret' config.oauth_token = 'your-oauth_token' config.oauth_token_secret = 'your-oauth_token_secret' config.auth_method = :oauth end terms = ['ladygaga'] daemon = TweetStream::Daemon.new('tracker', :log_output => true, :backtrace => true, ) daemon.on_inited do ActiveRecord::Base.connection.reconnect! p "Listening..." end daemon.on_error do |message| puts "on_error: #{message}" end daemon.on_reconnect do |timeout, retries| puts "on_reconnect: #{timeout}, #{retries}" end daemon.on_limit do |discarded_count| puts "on_limit: #{skip_count}" end daemon.track(terms) do |status| # put here your model.create code! # Tweet.create!( :uid => status.id, ... ) end
To run the script just type:
ruby scrip-name.rb run
source share