Created
January 21, 2014 17:28
-
-
Save tombeynon/8544308 to your computer and use it in GitHub Desktop.
Rails twitter integration
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
module ApplicationHelper | |
include Twitter::Autolink | |
#auto_link("link @user, please #request") | |
end |
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
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 |
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
gem 'twitter' | |
gem 'twitter-text' |
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
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 |
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
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