Created
March 11, 2011 11:27
-
-
Save michealbenedict/865766 to your computer and use it in GitHub Desktop.
How to write rake tasks and set in crontab (rails 3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#via http://jasonseifer.com/2010/04/06/rake-tutorial | |
#via http://codequietly.com/2010/6/rake-tasks-101 - Basics | |
#via http://codequietly.com/2010/6/rake-tasks-102 - Advanced use of Rails ENV | |
# | |
# General Rake | |
# | |
namespace :twitter do | |
desc 'Search Twitter for the parameter you pass in' | |
task :search, :query do |cmd, args| | |
# some very impressive search code... | |
Rake::Task['twitter:search'].reenable | |
end | |
namespace :search do | |
desc 'Search Twitter for "@daneharrigan" and save it in the database' | |
task :daneharrigan => :search do | |
# save results from :search and be happy | |
end | |
end | |
end | |
### | |
## The following code Finds Tweet of user and store in DB only once [Code is pseudo] | |
### | |
# | |
# Using Class | |
# | |
class TwitterStore | |
def self.search(query) | |
@results = Twitter::Search.new(query) | |
end | |
def self.latest_result | |
@results.first | |
end | |
end | |
namespace :twitter do | |
desc 'Search Twitter for the parameter you pass in' | |
task :search, :query do |cmd, args| | |
Rake::Task[:environment].invoke | |
# Rake is now aware of our Rails environment! | |
TwitterStore.search args[:query] | |
end | |
# ... | |
end | |
# using Twitter Class | |
task :michealbenedict do | |
Rake::Task[:search].invoke('michealbenedict') | |
result = TwitterStore.latest_result | |
params = { | |
:username => result[:from_user], | |
:message => result[:text], | |
:tweeted_at => result[:created_at].to_datetime | |
} | |
#model | |
Tweet.find_or_create_by_username_and_message_and_tweeted_at(params) | |
end | |
# crontab | |
*/5 * * * * cd /home/dane/twitter_store && /usr/bin/rake twitter:search:michealbenedict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment