Source Block Executors

Isa Mert Gurbuz (isamert) has a very interesting post on using Org Babel to interactively deal with Web APIs. Starting with Zweifisch’s ob-http, he queries a site and gets some JSON returned. The ob-http code even allows you to pipe the results through the jq binary to slice and filter the JSON results.

That’s pretty neat but what’s better is he shows us how to make the JSON results “executable”. It’s probably not clear what it means to “execute” JSON but the idea is simple: use can specify source block header parameters to specify filtering or other manipulation. The code to do this is simple and isamert gives an example of such code.
There are several examples in his post so take a look to see how it all works.

But wait! There’s more. JSON is an existing Emacs major mode but it’s possible to extend the idea to custom modes as well. The example isamert uses is for the Couchbase database. There’s no mode for Couchbase, so isamert created one by simply extending sql-mode. Then he wrote a function to take an SQL statement and send it to the Couchbase server. Again, that’s simple because Couchbase provides a command line tool. Finally, he wrote an executor for the Couchbase source block thereby providing an interactive Org-mode based tool to manipulate the database. Here’s an example of the its use (n1ql is the name of the mode):

#+begin_src n1ql :host DB_HOST :username DB_USERNAME :password DB_PASSWORD
  SELECT * FROM SomeTable LIMIT 10;
#+end_src

When he executes this block, the database is queried with the SELECT statement and the results are returned in a #+RESULTS: block as usual.

This is good stuff and has application outside of Web APIs so it’s well worth spending some time to read and understand isamert’s post even if you don’t routinely deal with Web APIs.

This entry was posted in General and tagged , . Bookmark the permalink.