Saturday, October 27, 2007

Posting updates to Twitter with Ruby

Step one in my new Premiership updater: post status to Twitter. It's pretty easy, so I'll just dive straight into code:


require 'net/http'
require 'uri'

url = URI.parse('http://twitter.com/statuses/update.xml')
url.user = "your username"
url.password = "your password"

res = Net::HTTP.post_form(url, {'status' => "hello world"})

puts res.body


Just two requires are needed before getting your hands dirty. Next is setting up Basic Authentication on the URI and posting with a single parameter of 'status'. Job done. View the result in XML or JSON through the update extension in the URI.

No comments: