rejin
rejin

Reputation: 179

I want find the tweets of all my followers in my sample twitter rails app

I want to find the tweets of followed_users

User model

class User < ActiveRecord::Base
    has_many :tweets, dependent: :destroy  
    has_many :relationships, foreign_key: "follower_id", dependent: :destroy  
    has_many :followed_users, through: :relationships, source: :followed
    has_many :reverse_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy
    has_many :followers, through: :reverse_relationships, source: :follower  

Tweet model

class Tweet < ActiveRecord::Base  
    belongs_to :user

Relationship model

class Relationship < ActiveRecord::Base  
    belongs_to :follower, class_name: "User"
    belongs_to :followed, class_name: "User

Please help me find

EDIT:
I don't want the actual tweets of The Twitter. I want tweets of my app.

Upvotes: 0

Views: 1018

Answers (3)

Richard Peck
Richard Peck

Reputation: 76774

Twitter's new v1.1 API allows you to do this somewhat, but you're not going to get a list of your followers' tweets from one call

Here's how I'd approach it:


Integration

It's no longer a case of oAuth to connect with Twitter, you've got to go through the v1.1 authentication process

You'll need to use the Twitter Gem to enable your Rails app:

#config/initializers/twitter.rb
#creates a constant
TWITTER = Twitter::REST::Client.new do |config|
  config.consumer_key        = "YOUR_CONSUMER_KEY"
  config.consumer_secret     = "YOUR_CONSUMER_SECRET"
  config.access_token        = "YOUR_ACCESS_TOKEN"
  config.access_token_secret = "YOUR_ACCESS_SECRET"
end

Then you can call the Twitter API directly:

#app/views/shared/footer.html.erb
<%= TWITTER.followers(213747670) %>

You must remember Twitter's new API is throttled


Backend

Because you can only get your followers, and then tweets, you'll have to make this a two-step process. I would approach it by storing the followers in their own table, and using a rake task to get their latest tweets, daily or hourly:

#app/models/tweet.rb
Class Tweet < ActiveRecord::Base
end

tweets
id | username | latest | created_at | updated_at

This will allow you to add twitter followers to the table, and use the rake task to update their latest tweet:

#app/controllers/tweets_controller.rb
def new
    @tweet = Tweet.new
end

def create
    @tweet = Tweet.new(tweet_params)
    @tweet.save
end

private

def tweet_params
    params.require(:tweet).permit(:username)
end

#lib/tasks/tweet.rake
namespace :tweets do 
    desc "Update Latest Tweets"
    task :latest => :environment do
        followers = Tweet.all.map(&:username).to_a
        followers.each do |follower|
             tweets = TWITTER.user_timeline(follower)
             follower.update_attributes({latest: tweets.first})
        end
    end
end

You could run the rake task from the console like this: rake tweets:latest

Upvotes: 1

Ziggy
Ziggy

Reputation: 22375

This is a fun thing to try to do! I recently made a small browser-based game that used twitter for authentication. In so doing, I found the following resources extremely useful:

Sferik, on github, provides the project Sign in With Twitter as an example of how to integrate your rails app with twitter's API. Lots of great code in there, and very simple. I used that project as a base for my own.

Sferik also provides the twitter gem, and t, a twitter CLI. These will be helpful to you on your journey.

In addition to the resource suggested in @royalGhost's answer, I would refer to this SO question.

Upvotes: 0

royalghost
royalghost

Reputation: 2808

First you need to understand how to integrate your rails app with Twitter. In order to do that you have to consume Twitter APIs.

  1. To integrate your rails app with Twitter, read this blog post - http://www.manaslutech.com/blogs/3-Ruby-on-Rails-integration-with-Facebook-and-Twitter . You can skip the Facebook part and just focus on Twitter integration.

  2. Once you have the Twitter authentication, you can get followers Twitter id or username

  3. Now, you can read all the tweets of followers from step #2

Upvotes: 1

Related Questions