I’ve often joked about putting together an app to track my coffee consumption, such is my reputation for consuming the black goodness. Like a lot of my other personal projects, the idea had a prolonged gestation period and was finally born through a welcome spark of motivation.
Crafting fine web APIs
Over the past 6 months the bulk of what I’ve been doing day to day with Vigill has involved building web APIs for consumption by mobile clients. This has involved lots of Sinatra, Redis and MongoDB.
In this time I’ve also put together a couple more Cocoa apps.
Thick client boogie
An unexpected resurgance in enthusiam for developing desktop apps combined with a fluency in cranking out webservices put me in a good position to put together a simple API and client.
I’m not a big user of mobile apps but I do spend the bulk of my waking hours sitting in front of a Mac so producing a client for OS X was the logical choice.
For the API I considered using something a bit more esoteric than my standard toolkit, but no matter what combination of technologies I investigated not much seems to come close to the power and flexibilty I find with Ruby and it’s frameworks for performing the bulk of tasks required by the web applications of today.
Areas of note
A valid API key must be sent in the request headers when sending a POST to the Sinatra app. Validity of the key is determined by checking set membership in Redis. A 4xx status code is returned if the key is missing or invalid.
The daily count is boosted with an atomic increment of a hash field.
The client is not much more than a GUI wrapper around some HTTP requests sent using AFNetworking. A previous native iOS client I developed used NSURLRequest directly and I found AFNetworking much simpler to use.
Get the code
The source for the API and the OS X client is available on GitHub as usual and some further technical details are available in the READMEs.
You can keep up to date with my coffee consumption at coffee-tracker.herokuapp.com.
Last year I started researching how CouchDB could be used for the BodyGuard web-app QA platform. Unfortunately I had to put my tinkerings to one side when some extra freelance work came in; contracting during the day and freelancing in the evenings quickly put an end to any spare time and my fitness efforts took a hit but I was only self-employed for a short while and it seemed the thing to do.
Since the urgency of the freelance work died down I’ve been looking for a small project to help me get back up to speed with Couch again and when the rumours of Delicious being shutdown surfaced I saw the opportunity.
The code is quite simple but it served to refresh my memory and has an actual practical aspect which is a bonus It’s available on GitHub as usual and the instructions below should get you up and running.
- a pre-Yahoo Delicious account (they use HTTP Basic Auth for authentication)
- CouchDB running on port 5984 on your local machine, I recommend the binary distributions from CouchOne
- nokogiri and couchrest gems must be installed:
sudo gem install nokogiri couchrest
Clone the code from GitHub
git clone https://github.com/stevenwilkin/delicious-couch
Run the import script to fetch your saved bookmarks
USER=username PASS=password ./import.rb
Substitute your Delicious username and password on the command line
Create the design document
Querying the data
Now that the database had been populated and the views created, the contents can be queried
View all urls
View all tags along with their count
View all urls with a specific tag
Views all bookmarks tagged with “git”
This set of scripts is only scraping the surface of what is possible with CouchDB but it demonstrates the immense power of MapReduce along with the basics of interacting with CouchDB using Ruby. Clone the code and enjoy!