Skip to content

Instantly share code, notes, and snippets.

@tjh
Forked from timnovinger/Tweet Caching.rb
Created April 22, 2010 17:57
Show Gist options
  • Save tjh/375565 to your computer and use it in GitHub Desktop.
Save tjh/375565 to your computer and use it in GitHub Desktop.
class Tweet < ActiveRecord::Base
def self.latest_text
if Tweet.first.nil? || Tweet.first.created_at < ENV['CACHE_TIMEOUT'].to_i.minute.ago
begin
httpauth = Twitter::HTTPAuth.new(ENV['TWITTER_USR'], ENV['TWITTER_PWD'])
client = Twitter::Base.new(httpauth)
Tweet.delete_all
client.user_timeline.each do |tweet|
Tweet.create :text => tweet.text
end
end
end
Tweet.first.nil? ? 'Could not connect to Twitter' : Tweet.first.text
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment