Skip to content

Instantly share code, notes, and snippets.

@jpinnix
Created July 19, 2010 19:49
Show Gist options
  • Select an option

  • Save jpinnix/481896 to your computer and use it in GitHub Desktop.

Select an option

Save jpinnix/481896 to your computer and use it in GitHub Desktop.
Include recent tweets in a Rails app
#lib/tasks/download_twitter_feed.rake
desc "Download Twitter Feed"
task :download_twitter_feed do
require 'open-uri'
etag_cache = open( "#{RAILS_ROOT}/public/system/etag_cache.txt" ).read
begin
open( "http://twitter.com/statuses/user_timeline/callannlane.xml", "If-None-Match" => etag_cache ) do |feed|
open("#{RAILS_ROOT}/public/system/tweets.xml","w").write(feed.read)
open("#{RAILS_ROOT}/public/system/etag_cache.txt","w").write(feed.meta['etag'])
Rake::Task["remove_specific_cached_pages"].execute
puts "File has changed."
end
rescue OpenURI::HTTPError
puts "No file available or file has not changed."
end
end
#lib/tasks/remove_specific_cached_pages.rake
desc "Remove specific cached pages"
task :remove_specific_cached_pages do
home_path = "#{RAILS_ROOT}/public/pages/home.html"
File.delete(home_path) if File.exist?(home_path)
puts "Cached pages removed."
end
# app/models/twitterer.rb
class Twitterer
include HappyMapper
element :id, Integer
element :name, String
element :screen_name, String
element :location, String
element :description, String
element :profile_image_url, String
element :url, String
element :protected, Boolean
element :followers_count, Integer
end
@jpinnix

jpinnix commented Dec 7, 2012

Copy link
Copy Markdown
Author

Due to changes with Twitter API, this no longer works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment