Wednesday, March 25, 2009

Posting a Status Update to Twitter

I recently had a need to allow a user to send the a url to twitter as a part of the status update. It seemed like it would be a trivial thing to do (and it was simple), but it was tough to find any information on it online. Most information is about the Twitter API and there are many products that were more heavy-weight than I needed.

This is what I wanted.

1) User clicks on a link which pops up a new window
2) My application would build the update text and include a tiny_url
3) Redirect to Twitter, pre-filling the update field.

In Ruby on Rails, this is what I ended up with. It's not very complicated, so it's probably easier just to see the code. I changed it to a Post model object for this example.

def tweet
@post = Post.find(params[:id])

tiny_url = open("{url_for(@post)}") {|f| }
message = "#{@post.title} #{tiny_url}"

redirect_to "{CGI.escape(message)}"

There are a lot of good ways to update status on Twitter using the Twitter API, but for this project they were more than I wanted. Here are a few that I plan to check out. I haven't used TwitterAuth yet, and a friend recently brought my attention to the site MessagePub.

I plan to try out TwitterAuth for another feature soon.

1 comment:

  1. If you use the Twitter API, it will automagically use tiny URL for you . . .