Creating a Facebook API Documentation Browser and Learning Some Ruby

I’ve been working on the iminlikewithyou Facebook app over the past few days, and I found myself wasting a lot of time on the FBML Documentation Page. It’s kind of hard to navigate and there is no concise list of all the tags.

I decided to make a better way to browse the documentation and practice my ruby one liners at the same time. This is the place I always use as a guide when I need to write a quick ruby one liner:

First I downloaded the html source for the FBML Documentation page. After a quick look, I could see that all the FBML tags were enclosed in <h4> tags, so I began by first trying to mimic this grep search:

grep -noe ‘<h4>.*</h4>’ fbml_doc.html

This prints something like

164:<h4>fb:action</h4>  165:<h4>fb:create-button</h4> 166:<h4>fb:dashboard</h4>

Here’s a longwinded version of what I first came up with:

cat prints out fbml_doc.html and the | feeds this into ruby. In the ruby script, I stored the STDIN (standard input) coming through the pipe in a temporary array named r because I thought I might want to take a slice of r if there were other <h4> tags elsewhere in the document. Luckily there weren’t, so I was able to do away with that.

Once I had the pieces of text I wanted, I used some string substitutions to format it into an html list of links. (I noticed that each <h4> was preceded by an anchor, which I could link directly to.) Here’s the final script I ended up using:

This outputs:

<ul><li><a href="README.html" target="docwin">README</a></li>
<li><a target="docwin" href="">FQL documentation</a></li>
<li><a target="docwin" href="">fb:action</a></li>
<li><a target="docwin" href="">fb:create-button</a></li>
<li><a target="docwin" href="">fb:dashboard</a></li> ...

It clearly grew to a size that was not appropriate for a oneliner, but it was a fun little bit to write in any case. Notice that I had to use n=$1 to save the contents of the magic variable $1 that contains the matching portion of each line containing the FBML tag name. I did this because then I use the sub method to change the : to a - when making a link, the $1 variable takes a new value. I also added some links to the beginning of the list which made this script quite a bit longer.

Aside: I considered trying to use wget to pipe the Facebook hosted documentation page into the ruby script, but I got an error because Facebook doesn’t allow the wget browser to access their site. Here’s how that would have looked:

wget -q -O - | ruby …

And here’s a hosted version of the documentation browser I ended up with:

Categories: ,

Copyright © | family andrew kortina Top ↑

Design by Anders Noren | Blogger Theme by |