From 88ee7e05a7f0136136eaf6e353b4a0e107f4c4ec Mon Sep 17 00:00:00 2001 From: hij1nx Date: Tue, 17 May 2011 08:33:15 -0400 Subject: [PATCH] readme update --- README.md | 110 +++++++++++++++++++++++++++--------------------------- 1 file changed, 54 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 3d618c6..4f845c5 100644 --- a/README.md +++ b/README.md @@ -65,24 +65,24 @@ In most cases you will want to make assertions on the outgoing and incoming data ### Specifying settings that apply to all calls that get made... ```javascript - var porter = Porter({ + var porter = Porter({ - users: { - list: ['get', '/api/users/:partialname', { out: hasData, in: hasData }], - update: ['post', '/api/apps/:username', { in: hasData }] - }, + users: { + list: ['get', '/api/users/:partialname', { out: hasData, in: hasData }], + update: ['post', '/api/apps/:username', { in: hasData }] + }, - apps: { - list: ['get', '/api/apps/:username', { in: hasData }], - create: ['post', '/api/apps/:username/:appname', { in: hasData }] - } + apps: { + list: ['get', '/api/apps/:username', { in: hasData }], + create: ['post', '/api/apps/:username/:appname', { in: hasData }] + } - }).use({ - port: 8080, - in: fn1, - out: fn2, - headers: { 'Accept': 'application/json' } - }); + }).use({ + port: 8080, + in: fn1, + out: fn2, + headers: { 'Accept': 'application/json' } + }); ``` The `use` function sets the defaults for all calls that get made. It accepts an object literal containing the following members... @@ -98,17 +98,17 @@ The `use` function sets the defaults for all calls that get made. It accepts an And here is the above code in use... ```javascript - porter.headers['Authorization'] = 'Basic ' + encodeBase64('username:password'); - - porter.users.update( - - { partialname: 'bill' }, - { address: '555 Mockingbird Ln' }, - - function(error, response) { - console.log(error || response); - } - ); + porter.headers['Authorization'] = 'Basic ' + encodeBase64('username:password'); + + porter.users.update( + + { partialname: 'bill' }, + { address: '555 Mockingbird Ln' }, + + function(error, response) { + console.log(error || response); + } + ); ``` The `update` function was generated from its definition in the `users` group. We pass it a payload object, some data to replace the url tokens with and a callback function for when the request has finished processing. The app object will also expose the headers collection, this is simply an object literal that contains the headers to be used for the request. @@ -116,44 +116,42 @@ The `update` function was generated from its definition in the `users` group. We ### Specifying what to do with the response. ```javascript - var porter = Porter({ + var porter = Porter({ - users: { - list: ['get', '/api/users/:partialname'] - } + users: { + list: ['get', '/api/users/:partialname'] + } - }).use({ - port: 8080, - host: 'google.com' - }).on({ - '500': function(err, response) { - // do something... - }, - '404': function(err, response) { - // do something... - } - }); + }).use({ + port: 8080, + host: 'google.com' + }).on({ + '500': function(err, response) { + // do something... + }, + '404': function(err, response) { + // do something... + } + }); ``` In a lot of cases you'll want to handle http responses based on their response code. using the `on` method will allow you to associate methods with these response codes. In some cases you'll want to explicitly override these http response code handlers. you can do this by replacing the regular callback method with an object literal containing the items to overwrite. ```javascript - porter.headers['Authorization'] = 'Basic ' + encodeBase64('username:password'); - - porter.users.update( - - { partialname: 'bill' }, - { address: '555 Mockingbird Ln' }, - - { - '404': function(err, response) { - // do something... - }, - '500': function(err, response) { - // do something... - } + porter.users.update( + + { partialname: 'bill' }, + { address: '555 Mockingbird Ln' }, + + { + '404': function(err, response) { + // do something... + }, + '500': function(err, response) { + // do something... } - ); + } + ); ``` Porter provides a simple Node.js server to complement it's test suite. You may find this a useful starting point for your own test suite.