Skip to content

Instantly share code, notes, and snippets.

@tombeynon
Created January 21, 2014 17:28
Show Gist options
  • Save tombeynon/8544308 to your computer and use it in GitHub Desktop.
Save tombeynon/8544308 to your computer and use it in GitHub Desktop.
Rails twitter integration
module ApplicationHelper
include Twitter::Autolink
#auto_link("link @user, please #request")
end
class CreateTweets < ActiveRecord::Migration
def change
create_table :tweets do |t|
t.text :body
t.text :twitter_id
t.datetime :date
t.string :profile_image
t.timestamps
end
end
end
gem 'twitter'
gem 'twitter-text'
class Tweet < ActiveRecord::Base
validates :body, :twitter_id, :date, :profile_image, :presence => true
scope :ordered, order("tweeted_at desc")
def self.latest_tweet
ordered.first
end
end
namespace :twitter do
desc "Fetch the latest tweets since the last stored tweet"
task fetch_tweets: :environment do
puts "Fetching Tweets"
last_tweet_id = Tweet.latest_tweet.tweet_id
client = Twitter::REST::Client.new do |config|
config.consumer_key = "YOUR_CONSUMER_KEY"
config.consumer_secret = "YOUR_CONSUMER_SECRET"
end
client.user_timeline("username", {:since_id => last_tweet_id}).each do |tweet|
t = Tweet.new(:body => tweet.text, :twitter_id => tweet.id, :date => tweet.created_at, :profile_image => tweet.profile_image_url_https)
t.save!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment