Team Boblycat is supported by the world's leading pathologists,
mad scientists and psychiatrists. If public health care
tries to euthanise you, please let our team get a shot!

You are hereBlogs / karltk's blog / RESTing with Ruby after a long week

RESTing with Ruby after a long week


I decided to take a peek at Ruby again. There's something cute about the language. There's a also a bunch of tiny webapp libraries in their CTAN/CPAN-like gem collection. Doing something quick and really dirt was never easier:)

I then decided to add flickr and twitter feeds to my homepage, but I wanted the page to remain static. I picked down the twitter and flickr ruby libraries and go to work. The stability of these libraries leave something to be desired. It didn't take all that many minutes to debug most of the buggy parts, fire off a couple of suggestions for improvements/bug fixes, and start being productive. Using the flickr API to pick out four random photos from my photostream on flickr:

 flickr = Flickr.new(API_KEY)

  user = flickr.users('karltk')

  if user.photos.length < 4 then
    ps = user.photos
  else
    ln = user.photos.length
    ps = []
    ps.push(user.photos[rand(ln)])
    ps.push(user.photos[rand(ln)])
    ps.push(user.photos[rand(ln)])
    ps.push(user.photos[rand(ln)])
  end
  urls = ""
  ps.each do |p|
    urls += "<a href=\"" + p.url + "\"><img src=\"" + p.source('Square') + "\"/></a>"
  end
  urls

Using the twitter API to fetch my four latest status updates:

  
  feed = Twitter::Base.new('username', 'password').timeline(:user)

  msgs = ""
  feed[0,4].each do |s|
    tm = Time.parse(s.created_at)
    msgs += "<p><a href=\"http://twitter.com/karltk\"><b>(" + tm.strftime("%a %H:%M") + ")</b></a> " + s
  end
  msgs

The stings generated by each of these code fragments are inserted into a plain HTML template. The end result, with the twitter feed showing at the bottom right:

Yes, I know I could've done this more easily using the existing JavaScript snippets provided by Twitter and Flickr, but that is totally beside the point:) Now I have this wonderful cron job running every once in a while instead.