Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Route.prototype.realize. #134

Merged
merged 1 commit into from May 4, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/index.js
Expand Up @@ -10,6 +10,7 @@ var clients = require('./clients');
var errors = require('./errors');
var plugins = require('./plugins');

var sanitizePath = require('./utils').sanitizePath;


///--- Globals
Expand Down Expand Up @@ -177,6 +178,21 @@ module.exports = {
},


/**
* Returns a string representation of a URL pattern , with its parameters
* filled in by the passed hash.
*
* If a key is not found in the hash for a param, it is left alone.
*
* @param {Object} a hash of parameter names to values for substitution.
*/
realizeUrl: function realizeUrl(pattern, params) {
return sanitizePath(pattern.replace(/\/:([^/]+)/g, function (wholeMatch, key) {
return params.hasOwnProperty(key) ? '/' + params[key] : wholeMatch;
}));
},


HttpClient: HttpClient,
JsonClient: JsonClient,
StringClient: StringClient
Expand Down
21 changes: 1 addition & 20 deletions lib/request.js
Expand Up @@ -9,6 +9,7 @@ var mime = require('mime');
var qs = require('qs');
var uuid = require('node-uuid');

var sanitizePath = require('./utils').sanitizePath;


///--- Globals
Expand All @@ -19,26 +20,6 @@ var Request = http.IncomingMessage;

///--- Helpers

/**
* Cleans up sloppy URL paths, like /foo////bar/// to /foo/bar.
*
* @param {String} path the HTTP resource path.
* @return {String} Cleaned up form of path.
*/
function sanitizePath(path) {
assert.ok(path);

// Be nice like apache and strip out any //my//foo//bar///blah
path = path.replace(/\/\/+/g, '/');

// Kill a trailing '/'
if (path.lastIndexOf('/') === (path.length - 1) && path.length > 1)
path = path.substr(0, path.length - 1);

return path;
}


// The following three functions are courtesy of expressjs
// as is req.accepts(), and req.is() below.
//
Expand Down
22 changes: 22 additions & 0 deletions lib/utils.js
@@ -0,0 +1,22 @@
// Copyright 2012 Mark Cavage, Inc. All rights reserved.

var assert = require('assert');

/**
* Cleans up sloppy URL paths, like /foo////bar/// to /foo/bar.
*
* @param {String} path the HTTP resource path.
* @return {String} Cleaned up form of path.
*/
exports.sanitizePath = function sanitizePath(path) {
assert.ok(path);

// Be nice like apache and strip out any //my//foo//bar///blah
path = path.replace(/\/\/+/g, '/');

// Kill a trailing '/'
if (path.lastIndexOf('/') === (path.length - 1) && path.length > 1)
path = path.substr(0, path.length - 1);

return path;
};
23 changes: 23 additions & 0 deletions test/index.test.js
@@ -0,0 +1,23 @@
// Copyright 2012 Mark Cavage, Inc. All rights reserved.

var test = require('tap').test;


var restify = require('../lib/index');



///--- Tests

test('realize', function (t) {
var pattern = '/foo/:bar/:baz';

t.equal(restify.realizeUrl(pattern, {}), '/foo/:bar/:baz');
t.equal(restify.realizeUrl(pattern, {bar: 'BAR'}), '/foo/BAR/:baz');
t.equal(restify.realizeUrl(pattern, {bar: 'BAR', baz: 'BAZ'}), '/foo/BAR/BAZ');
t.equal(restify.realizeUrl(pattern, {bar: 'BAR', baz: 'BAZ', quux: 'QUUX'}), '/foo/BAR/BAZ');

t.equal(restify.realizeUrl('/foo////bar///:baz', {baz: 'BAZ'}), '/foo/bar/BAZ');

t.end();
});