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
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
module ApplicationHelper
include Twitter::Autolink
#auto_link("link @user, please #request")
end
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