Hi Guys, So well on a lazy saturday afternoon, when you don’t have anything to do, you get this feeling to make something. So well what you do, you just make something simple, Burgers, fast food anything that you like. But well people like us who think, they are so called developers, have to feel the burden of creating something which which solve more a problem more important than the “What to have for lunch?” problem.
Anyways, so well, I have two twitter accounts, 1) @sudipto05 <– which I use and do follow me here and 2) @delhianimeclub which is my club’s twitter accout, which well, I never use, but well, its gets updated with the posts that are put on our facebook page. (If you are into anime, do like us on facebook). So the problem at hand, I had to manually retweet the tweets of @delhianimeclub from my other account, you know cross promotion, stuff like that. But this wasn’t really cool. right?
So what do Rails developers do?
This is what they do
rails new retweeter
Lets add a few gemfiles, of late I have had to work on HAML and Bootstrap so lets add those gems. Checkout my gemfile from link. We wont be really needing the twitter bootstrap gem for this iteration of the project, but we will be certainly using it for the making a statistics application for our twitter account. I don’t really like postgresql, but I wanted to host it on heroku,which only has support for pg. Oh yes, just forgot, two of the most important gems that we are going to use
1. Twitter gem – will let us post fetch and post tweets only with what 2 lines of code! I could have tried to use Twitter’s rest api too, but then, who is going to make lunch? huh?
2. Rufus Scheduler – I love this, its an awesome gem for scheduling your jobs, since we are going to check if new tweets are posted after regular intervals, we need some kind of cron job running behind our app, this is going to make it super easy.
After adding the gem files, just
So here we go, now some actual coding starts, well yeah! twitter bootstrap gem doesnt like windows at all, so its better if you add the css and js files manually from link. You know right? they go into the assets folder 😛
Well, we are going to save the tweets in our database too, so lets generate a Tweet model with three fields.
rails g model Tweet tweet_id:string tweet_text:string tweeted_at:datetime
The advantage of specifying the attributes while generating a model is that they get automatically added to both the migration! duh! and also in the attr_accessible in the model file. Saves you 10 seconds of typing 😛
and afterwards to run the migration and setup our database
lets start with our Scheduler first. Create a new file in the config -> initializers directory, well you can name it anything, I just named it task_scheduler.rb and put in the snippet of code. A very simple explanation of the code within the file.
1) Requires the rufus scheduler gem, so we add the require line.
2) Create a new instance of the scheduler with
scheduler = Rufus::Scheduler.start_new
3) And lastly we simply call a class method within a block, which will call the method every 1 minute, now you can choose the duration of the interval according to your needs, 1 minute seems fair enough, its not like we post tweets faster. What? It requires aesthetics for tweeting? huh?
Okay so we are done with 33% percent of the work, Lets move onto our model file, Remember Tweet model that we created. Check out the file from link. Lets see what gibberish we have to put in the code now.
1. First we get the recent tweets of the user from whose account, we are going to retweet. latest_tweets = Twitter.user_timeline(“account_name”)
2. Then we run a simple loop through the recent tweets that we get from twitter and check if the tweet is present in the database, if not, the tweet will be retweeted and saved in the database.
4. Oh yes! I have put this whole block of code within begin rescue end block, this twitter gem seems to throw in some weird exceptions sometimes, so just to log the errors, you can use the logger of your choice for this.
Okay, Guess we are done with 70% of the work 😀
So the next step is to create a new twitter application which is going to be used to post your tweets, don’t worry, we just need it for the config keys, nothing fancy.
1. Head over to link, to create a new twitter application. Oh yeah! sign in with the twitter account, using which you want to retweet the tweets from your other account.
2. Fill in the name and all description, just anything who cares 😛
3. After you are done with creating the app, go to settings and change the application type to “Read and Write” after this change save it and head back to the Details tab and create new access tokens. What? can’t you see, there is a button on the bottom to create new tokens.
These tokens will be used to authorize our rails app to post the tweets, oh yeah! the name you put for this app is going to show below every tweet 😛
You need to add these keys to your environments.rb file. Just check out the files here and add the lines for twitter config to the respective environment file in your app.
And we are done, just start the server and see the magic. Our initializer will run after 1 minute and fetch the latest tweets, save them in database and post them to our other twitter account. Thats it. What we have made a retweeter in less than what 30 lines of code, i guess, that is why I love ruby on rails. Oh! wait, you are going to tell me, that I made a rails app, just for this, when I could have done this using just a single ruby file, well yeah! the reason being, we are going to extend this web application in many many ways, so don’t forget to check the blog for updates. The code of this application is hosted on my github.