The terminal is next to the editor probably the most important tool for a developer. Being comfortable executing shell commands increases productivty significantly. And that’s what it’s about at the end. Being able to focus on solving the problem and not dealing with tools and workflows to get there. For a long time I am using iTerm 2 but that’s only the terminal and does not change the actuall shell.
I was browsing dotfiles and came across zsh, an alternative to the default bash shell on OSX.
I’ve been using zsh for a couple of weeks now and like especially the dot-files oh-my-zsh which provide a great default configuration and git prompts. Here the steps on how I’ve set things up:
Make zsh default shell:
chsh -s /bin/zsh
Open a new terminal window to see zsh!
Install oh-my-zsh (you gotta trust that script so make sure you want to do that):
curl -L https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh | sh
Now some configiration. Edit ~/zshrc.
I like the default theme but set ZSH_THEME="random" and open some new shells to explore some other themes you might like better.
Oh-my-zsh comes with a bunch of plugins and only a few are set by default.
Here is my plugin list:
I’ve been using MongoDB with Node.js on Heroku so here a quick walk through on how to set it up and use it:
Setup
Local
Install and Run:
$ brew install mongo
$ mongod
On lesson I’ve learned is to always try to use MongoDB from the shell first before you start with actual code. After having the command right in the shell translate it into code and test your actual implementation. Here some shell commands to get you started:
$ mongo
> use mydbname
For this example we store locations in a collection called locations
Make sure you have set the geo spacial index:
On the Heroku side it’s pretty easy to set up as well:
$ heroku addons:add mongohq:sandbox
To make sure the location index is also working on Heroku, login, select your app and select the MongoHQ add-on. This brings you to the MongoHQ admin interface. Then select your locations collection and make sure the Indexes are configured correctly. In my case it shows as { key: { loc: "2d" }, v: 1, ns: "appXXX.locations", name: "loc_2d" }
MongoDB Shell
Insert Location
When you insert a new location it’s essential to store the location in an array with [lon, lat] and not [lat, lon]. This was a mistake I made first and it took me a while to figure that out because all my queries were returning the wrong stuff.
Let’s insert some example locations (I’ve been using LATLONG.net):
Just as a sanity check: Everything west of Greenwich has a longitude < 0 and east > 0. Everything in the northern hemisphere has a latitude > 0 and in the southern hemisphere < 0 or as Wikipedia puts it:
In geography, latitude (φ) is a geographic coordinate that specifies the north-south position of a point on the Earth’s surface. Latitude is an angle (defined below) which ranges from 0° at the Equator to 90° (North or South) at the poles.
Longitude (/ˈlɒndʒɨtjuːd/ or /ˈlɒŋɡɨtjuːd/),[1] is a geographic coordinate that specifies the east-west position of a point on the Earth’s surface. […] The longitude of other places is measured as an angle east or west from the Prime Meridian, ranging from 0° at the Prime Meridian to +180° eastward and −180° westward.
Query Locations
After we have inserted some test locations let’s try to make a geospatial query. In my case I wanted to find locations close to the current location so let’s pretend we are at the Ferry Building in San Francisco.
The result is ordered by distance and the Ferry Building should be on top with a dis of 0. To have the distance in a human readable unit you can use the distanceMultiplier attribute and select for instance the earth radius in miles which is 3959. This will show all distances in miles:
If you want kilometers use 6371m as the radius of the earth.
Now that everything is working in the shell we can start with actual code.
Node.js Code
This example is written with CoffeeScript and has been extracted from an actual project and usually you would do more validation and parsing but you should get the idea on how the Express route is working:
# routes/api.coffeemongo = require'mongodb'assert = require'assert'ObjectID = mongo.BSONPure.ObjectIDdbConnection = process.env.MONGOHQ_URLor'mongodb://localhost/mydbname'locations = nulldb = nullmongo.Db.connectdbConnection,(err, database) ->db = databaseassert.equalnull,errdatabase.collection'locations',(err, collection) ->assert.equalnull,errlocations = collectionexports.location = (req, res) ->switchreq.method# Create locationwhen'POST'# In this example I am using upsert to update the record with the existing _id when present or create a new ObjectID if not:location = req.bodylocation.loc = [parseFloat(location.loc[0]),parseFloat(location.loc[1])]isUpdate = location._id?id = ifisUpdatethenlocation._idelsenewObjectID()locations.update{_id:id},location,{upsert:true,safe:true},(err, result) ->assert.equalnull,errres.sendJSON.stringify(location)# Query locationswhen'GET'lat = req.query["lat"]lon = req.query["lon"]iflatandlonlat = parseFloatlatlon = parseFloatlon# validate that lat/lon are numericreturnres.send400ifisNaN(lat)orisNaN(lon)# validate that lat/lon are in the right rangereturnres.send400iflon<-180orlat>180geoNear = geoNear: locationsnear: [lon,lat]spherical: truemaxDistance: 10/3959distanceMultiplier: 3959db.commandgeoNear,(err, locations) ->assert.equalnull,errres.sendlocations
Lately I needed some tests for an ExpressNode.js project I was working on and I was surprised how easy and minimalistic the code can look like with CoffeeScript. Check out this gist:
I am using should.js as the assertion library and mocha as the testing framework. In this example I am testing a login function. My Node.js route (api.coffee) is being imported and then I’ll first test if the login function actually exists. After that I am invoking the login function and make assertions against the result. The implemented function signature looks like this:
1
exports.login = (req, res) ->
The parameters are request and response. This route is using GET so for the request parameter we’ll specify the query parameters.
When the implementation is done with the login execution it’ll call res.send(result) so it’s perfect to hook into that as well and we can do assertions against the result type and value.
If you are new to CoffeeScript this might look a bit cryptic but let me show you how the JavaScript would look like:
I’ve been blogging using Workpress since February 2005 and I think Wordpress made the Internet a better place. Luckily I never got hacked but there are just too many stories that sites got hijacked that I finally decided to switch to a static blog. Github pages with Octopress seemed like a perfect fit for my needs so here we are.
The setup is dead simple so here my steps:
Created a new Github repo srohde.github.io and a bunch of command lines to initialize Octopress:
Bamgain (did I just invent that?)! I’ve got a bunch of topics I wanted to blog about and having a new blogging engine will motivate me to actually do so. That’s how devs tick I guess. Cheers!