Using Curl to play with CouchDB

As a big big fan of all things HTTP, the new Erlang based database, CouchDB, piqued my interest. With the recent release of 0.7 it’s now intended for widespread use. Now I’m a fan of databases as long as I don’t have to go too near them. SQL, triggers and stored procedures are all a little too close to magic for me.

The reason CouchDB looks particularly good fun was it’s build around a REST API using JSON for data transport. I’m generally not an acronym guy, but REST, JSON, API and HTTP in one open source piece of code? Sounds like fun to me.

The CouchDB community have built up an already excellent wiki with all the information you need to get started and get the software installed. I just used the magic of MacPorts but the page covers all the various dependencies and setups (though nothing on their yet about the N800).

<code>sudo port install couchdb</code>

You might then need to set-up a user and some permissions. Again the wiki has more detailed installation instructions. All being well you should then be able to fire up CouchDB:

<code>sudo -u couchdb couchdb</code>

So first things first. Their are getting started code examples on the wiki in all the usual languages so you can just dive in. CouchDB also comes with a few in-build tools which are both pretty attractive and useful. You have a database browser, a full test suite and a command shell.

Curl, for those not familiar with it, is a fantastic command line utility for throwing around HTTP requests. I already use it for testing and generally prodding sites but it’s absolutely perfect for demonstrating CouchDB. Let’s start with creating a database called test:

<code>curl -X PUT http://localhost:8888/test/</code>

That should have created the database. Let’s get some information back about the database:

<code>curl -X GET http://localhost:8888/test/</code>

Everything should be returning JSON strings telling us something about what it finds. Let’s add a blank document to the database:

<code>curl -X POST http://localhost:8888/test/ \
-H "Content-Type: application/json" -d {}</code>

Note the blank JSON object {} passed as Post data. What about retrieving a list of document from the database?

<code>curl -X GET http://localhost:8888/test/_all_docs</code>

We’re now done with that database so let’s delete it.

<code>curl -X DELETE http://localhost:8888/test/</code>

I’ve just done the basics here, but the APIs are simple and nicely documented enough for you easily to find out more. This is the beauty of RESTfully designed APIs, they are just about self documenting. For the most part you just have a predefined HTTP method and a defined URL.

The Erlang base of CouchDB makes for some interesting horizontal scaling possibilities (as well as a good excuse to play with Erlang.) Even if CouchDB wasn’t cool enough already, it comes with probably the best start-up message I’ve seen in software for ages:

<code>CouchDB has started. Time to relax.</code>

Insert Dojo and YUI bookmarklets

I’ve talked about having fun with jQuery using Jash and Firebug before after seeing Simon throw the Google homepage around at barcamp. I’m no more a one javascript framework person than a one programming language person and recently I’ve seen cool things I want to play with in lots of the main contenders.

I’ve put together a couple of bookmarklets which load YUI or Dojo from their respective content delivery networks and insert them into your current browser context. You can then play around with them in Jash or Firebug or any other Javascript console.

Dojo is particularly interesting here due to it’s cunning loading mechanism for additional parts of the framework. Basically you can gain access to the entire (huge) framework simply by using the bookmarklet and then dynamically loading the rest via the command line.

Insert Dojo

<code>javascript:void(function(){
 var%20s=document.createElement('script');
 s.src='http://o.aolcdn.com/dojo/1.0.0/dojo/dojo.xd.js';
 document.getElementsByTagName('head')[0].appendChild(s);
}())</code>

Insert YUI

<code>javascript:void(function(){
 var%20s=document.createElement('script');
 s.src='http://yui.yahooapis.com/2.4.0/build/yahoo-dom-event/yahoo-dom-event.js';
 document.getElementsByTagName('head')[0].appendChild(s);
}())</code>

The code simply inserts a script element into the page which loads the relevant framework. I’ve shown the code on multiple lines for the sake of the example, but your bookmarklet should be on one line. Alternatively you can drag the Insert YUI or Insert Dojo links to your bookmark bar.

Writing Elsewhere

As well as the eclectic ramblings on this blog I’ve taken to writing longer bits and pieces for some other nice folk, two of which went up over the last few days.

“Drew” and Brian are doing a great job of gathering together some really good articles in a silly time-scale over on 24ways.org at the moment. My little ditty on minification was day 6.

Chris Mills, in his new position as Opera mascot, has also rapidly started to build up a great collection of articles over on dev.opera. My contribution to what is looking like a great new resource is a piece on using JSON for configuration in your Javascript applications.

Writing for other people is different to just posting interesting stuff on your blog. And it wouldn’t work at all without some pretty dedicated people editing, compiling and chasing to get us all those high quality articles for sites such as alistapart, Digital Web and Sitepoint. So if you meet any of these people on your travels then pat them on the back and buy them a beer. Without the information sharing community we probably wouldn’t be doing all this for a living.

Debug web pages with Jquery and Jash

I have a new toy I’m quite enamoured with. After a double helping of jQuery first from Jon Resig at @media Ajax and then from Simon Willison at barcamp I just had to take a look. My favourite part was probably Simon showing off his Insert jQuery bookmarklet - hacking around with the Google home page design on the firebug command line. The idea of having a command line to play around with any web page I come across appeals to my geekier side.

I spend more time in Safari than Firefox these days mainly as I find it feels snappier and is generally more stable. The only real downside is the lag of Firebug. A quick look at Firebug Lite revealed it doesn’t work in Safari but a little searching later I found Jash. The Insert jQuery and Insert Jash bookmarklets now have pride of place in my bookmark bar.

"Jquery and Jash bookmarklets running on Google"

So what to do with my new toy? Let’s hide all the paragraphs! (for no other reasons than we can).

<code>$("p").hide("slow")</code>

So, redesign any web page you visit via a quick to access command line? What’s not to like?

10 Thinks you probably don&#39;t do

The resent barcamplondon3 was huge fun and particularly interesting. I might even go as far as saying it was the best yet in terms of the sessions I attended.

Particular highlights for me included discussions of (Yahoo! flavoured) web development practices and processes from Norm and Mike, Matt Biddulph on messaging, erlang a event driven programming techniques and Dan Dixon’s panel on What should we be teaching the next gen of web designers/devs?

I tried to kick off a debate regarding automation and software tools and practices in web projects. More from anecdotal evidence and experience rather than pure research I don’t think that many companies or individuals really set their projects up on a solid base. Source control, bug tracking, scripted builds, automated quality testing, etc. aren’t that common for smaller or medium web projects.

In the end I talked though everything in general terms and the audience nicely jumped in with various platform specific toolsets for unit testing, functional testing and performance measurement. All in all good fun and a subject I think will see some activity next year.

Overall a good smattering of my favourite topics at the moment (education, development process, distributed messaging), lots of fun people and the chance to ride round Google on a segway at about two in the morning. Roll on the next barcamp.

Archiving Twitter data with Python

Alex from Twitter just got round to adding the ability to export your entire archive of tweets via the API. A few people on the mailing list had been asking for this for a while so good to see it get released.

I couldn’t resist knocking together a very quick and simple Python script to go off and get all your tweets, presented here for anyone else to play around with. Note that simple, fast and works on my machine were watchwords here. Don’t expect fancy parameters, much error handling or artificial intelligence.

<% syntax_colorize :python, type=:coderay do %> import urllib2 username = ‘‘ password = ‘‘ format = ‘json’ # json or xml filename = ‘archive.json’ # filename of the archive tweets = 164 # number of tweets pages = (int(float(tweets)/float(80)))+1 auth = urllib2.HTTPPasswordMgrWithDefaultRealm() auth.add_password(None, ‘http://twitter.com/account/', username, password) authHandler = urllib2.HTTPBasicAuthHandler(auth) opener = urllib2.build_opener(authHandler) urllib2.install_opener(opener) i = 1 response = “ print ‘Downloading tweets. Note that this may take some time’ while i <= pages: request = urllib2.Request(‘http://twitter.com/statuses/user_timeline/account.'
+ format + ‘?page=’ + str(i)) response = response + urllib2.urlopen(request).read() i = i + 1 handle = open(filename,“w”) handle.write(response) handle.close() print ‘Archived ’ + str(tweets) + ‘ of ’ + username +
‘\’s tweets to ‘ + filename <% end %>

The main thing to note here though is the ideal of data portability. Let’s just say I wanted to move to Jaiku or Pownce instead of Twitter, but didn’t want to lose all that data. If I can knock up an export script in half an hour then all you need are import data API calls and a little more scripting.

As it is, I’m more interesting in seeing what I can do with my data now I can get at it. Brian just suggested a quick visualisation tool (which already eats JSON) and I’d already been thinking about language analysis and maybe having a look at APML some more. Open data is simply more fun.

Should we just learn Java?

As a wandering web designer cum developer and now occasional consultant I’m generally pretty technology agnostic. At some point or another I’ve written HTML, CSS, Javascript, PHP, C#, Python and Rails for a living - often on the same day. But something has me thinking and I thought I’d see what other people thought as well.

That thing is Java. I have books on Java. I’ve done the usual examples (hello world in 20 lines for instance). I didn’t really like it and left it at that. But a few presentations, both from Google, at both Future of Mobile and @media Ajax got me thinking. First you have Android in the mobile space which Dave Burke did a good job of making look pretty interesting. Then you had the guys from Ajaxian talking about using Java for everything.

A few other thinks to throw into the pot. Beyond Java is an interesting read - summing up with a call for more fragmentation and for small nimble languages to be used where it makes sense. Oh, and I think Tom is busy learning Java as well. Mmmmm.

Some good coverage around of both conferences and I know my first impression was basically this way lies madness. But it would have been more interesting had their been a few more Java people around to argue with.

Note that I’m not advocating a Google like disregard for the web here. Markup really matters, I happen to disagree with Douglas and think CSS rocks and I love writing Javascript. But should we just learn Java anyhow and try to subvert some of the existing tools to be more web like?

Joost Developer Day

I spent yesterday afternoon over with Joost in London as part of their UK developer day. The event was mainly for them to show off their new widget platform and to get some input from prospective developers about what they would like to see next. About 20 people or so came along from the BBC, Ebay and a couple of london agencies, with me and Tom loitering around the edges. The afternoon was split into a series of short talks from people on the team. What follows is some of the interesting bits.

Joost describe themselves as The Best of the Internet and best of TV, if you haven’t seen it yet it’s basically TV running on the Mozilla platform. All in all pretty impressive. Dan Brickley had a different slant, seeing Joost as Web 2.0 +/- 1 Built and extended with modern web technology, modern web attitude. The idea of taking an unashamedly desktop piece of software and learning lessons from the web is pretty interesting.

The Joost development site is now public, although not linked to from the main site as yet. Their is some documentation of the various APIs but we can expect more in the future, including some videos from the developer days in a development channel.

The main emphasis for developers at this moment was on overlays and widgets (little bundles of web technology) which integrate with the Joost client. We got a sneak peak (and a chat with some of the developers) of the first commercial Joost widget for Coca Cola - Coke Bubbles and Libby Miller and Jim Ley showed off a few of the default widgets.

The most interesting bits from my point of view were to do with the technology. Joost has an interesting advantage as a platform being a single, dedicated client. No more cross browser testing here. They also keep pretty close to trunk for Mozilla, which gives them quite a bit of CSS3 support too. You can also try out other new technologies not available in too many browsers yet: SVG, RDF, Canvas and E4X for example. This was the second time in a few days I’d seen SVG pushed as cool, the first being at Future of Mobile which I’ll jot down some thoughts on later.

What was interesting was that the people along from the development team came from a W3C background rather than say an product development or design companies. Joost makes extensive use of Semantic Web technologies under the hood and exposes quite a bit of RDFa if you go looking for it. It also follows along with various other web-like ideas, unique URLs for channels and programmes, including the ability to pass these parameters for instance to deep link into a video at a current time. The widget platform as well is starting to align with standards work going on at the W3C - we saw a quick demo of a widget build for the Opera browser which worked seamlessly with Joost too. The development team are now working on dealing with signed widgets, local storage APIs, interwidget communication, as well as usable, clear privacy models. All pretty interesting.

Some of the event was hampered by a shoddy internet connection but the general mood was relaxed and friendly and no one seemed to mind too much. The beer probably helped. I did chuckle when a senior alternative marketing guy from Coke described as Insane the typical agency setup though (rich guy in Notting Hill -> account manager -> project manager -> developers).

Their will be a Coke sponsored widget competition announced after the rest of the developer days (in Amsterdam and New York) with the title of International widgets champion of Joost up for grabs along with some cash and a t-shirt.

My only real complaint was probably the low level mutterings about Javascript! Audience members and Joost developers alike made a couple of digs during the course of the day. Javascript rocks, people. Oh well, I’m back in London for @media Ajax where I don’t think I’ll have that problem.

If any of this sounds interesting (or you just want to watch more TV) I’d recommend having a look through the development site, jumping on the joost-dev freenode IRC channel, signing up to the mailing list or reading the development teams blog.

Activity

Sorry, shameless what’s going on post for historical posterity.

Before that starts thought here are a couple of useful microformatted schedules for Future of Mobile and @media Ajax created by the really rather handy Conference Schedule Creator. Hat tip to Jeremy and Brian for the lovely styles.

I’m spending the next few weeks circling London, first up attending Future of Mobile, on to Pubstandards and then popping in to see Joost. After a short break I’ll be back for @media Ajax and BarCamp London. Throw in a few business meetings and we’re all set. I even have new Moo cards.

Web Geek Moo cards

If you’re around at any of these events, or just happen to be in London, and are one of the two people who read such self serving ramblings as this let me know and we can drink beer. Actually scratch that. Due to the wonder of Dopplr I already know where you’re going to be.

Some useful mobile web design links

Earlier in the year I set aside some time to get my head into the mobile web. Most of this was reading and tinkering. Since then I’ve pointed a number of people (including clients and people in the pub) towards some or all of the following links. A good starting point for anyone starting to get interested in the mobile web.

A good starting place is Mobile Web Design by Cameron Moll which is available as a PDF, and now as a hard copy from lulu. Definitely worth your while. If you’re not convinced then you can read the original series of articles to get started.

The Mobile Web Best Practices 1.0 from the W3C Mobile Web Initiative and Global Authoring Practices for the Mobile Web are more good starting points, this time with more of an implementation focus. Blue Flavor also have an intersting presentation you can download on the subject of Designing for Mobile

Once you have a basic understanding, or if you just want the facts then check out The Wireless FAQ. Everything from What is WAP? up to Nokia S60 WAP browser cursor skip some links on my WAP site! What is wrong? is in there somewhere.

If you’re too busy to be reading everything written about mobile, or just prefer listening to the voice of Paul Boag, then have a listen to Boagworld episode 96. Heidi Pollock at the recent Future of Web Apps was also excellent, especially if you let your numbers. You can get the presentation and audio of the talk now.

If you have still have questions after reading a few good articles or books then their are a couple of high quality mailing lists, namely wmlprogramming and mobiledesign. It’s worth being subscribed to both, thought their is quite a bit of audience cross over.

The .mobi site also has lots of useful information regarding mobile. Whether you buy into the need for a new top level domain or not it’s worth reading.

Lots of reading to get you started and not one mentioned of the iPhone…

Have any other good links for getting started in the mobile web? Leave a comment with further suggestions.