I used his original blog post as a guideline but his method was a bit dated and here’s the process I used to get this functionality working.
The basic idea works this way, when you create a post it prints the tweet message you give it and a link to the post in a
tweet_queue file. When you deploy, it simply reads this file and tweets what’s in there line-by-line. This also means you can create multiple posts at a time, and they will all be tweeted at the same time when you deploy you blog.
Create an application on Twitter
Head over to https://dev.twitter.com/apps/new, sign in and create a Twitter application for your blog.
In Settings: ensure read and write access is enabled.
In Details: take note of the following details:
- Consumer Key
- Consumer Secret
- Access Token
- Access Token Secret
Install the gem
We’ll be using the Twitter gem to update statues. So first things first, so go ahead and put this in your
Gemfile in the development group::
Then from a terminal run
bundle install to install the gem.
Open up your
Rakefile and add the following up near the top with the other requires:
While you have the
Rakefile open add the following lines after the
YOUR_* with the details from your Twitter app.
Make sure there is a trailing slash (slash at the end of the URL) for the
blog_url. It needs this because it appends the date-name combo of the post to form the URL of the post.
Modify the ‘new_post’ rake task
Look for the
:new_post rake task in the
Rakefile. this should be around lines 106 or so. Replace the task with the following:
If you already have a modified task, the only changes are the task declaration
(task :new_post, :title, :tweet do |t, args|) and the last few lines starting with
tweet = args.tweet.
Setup tweet on deploy
Look for the
:deploy task in the
Rakefile this should be around lines 232 or so. Replace the task with the following:
Or indeed just add the last couple of lines starting with
The only change here is you’ll need to specify a separate message for the tweet when you create the post. This is the best way to give you the option of choosing whether you want to tweet the post or not. It also allows you to add @’s and #’s to the tweet message separately from the post title.
Here’s an example:
The first section is the title of the post while the second section is the tweet that will go out along with the URL to the blog post.
And here’s the message generated after the
rake new_post task is run.
Now all that’s left to do is create your post and then run
rake generate && rake deploy. Your post will be deployed and your tweet will be sent.
And that’s it. Pretty simple eh? Have fun tweeting and blogging from Octopress.