Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding `restify.realizeUrl` for replacing URL params with values from…

… a hash.

Example: `restify.realizeUrl('/foo/:bar/:baz', {bar: "BAR", baz: "BAZ"}) === '/foo/BAR/BAZ'`

In the process factored out `sanitizePath` method from request.js into a new utils.js module.
  • Loading branch information...
commit 9bb51aff1b305c3ce5bd38103f52ae2154125dfd 1 parent 68f22a7
@domenic domenic authored
Showing with 62 additions and 20 deletions.
  1. +16 −0 lib/index.js
  2. +1 −20 lib/request.js
  3. +22 −0 lib/utils.js
  4. +23 −0 test/index.test.js
View
16 lib/index.js
@@ -10,6 +10,7 @@ var clients = require('./clients');
var errors = require('./errors');
var plugins = require('./plugins');
+var sanitizePath = require('./utils').sanitizePath;
///--- Globals
@@ -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
View
21 lib/request.js
@@ -9,6 +9,7 @@ var mime = require('mime');
var qs = require('qs');
var uuid = require('node-uuid');
+var sanitizePath = require('./utils').sanitizePath;
///--- Globals
@@ -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.
//
View
22 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;
+};
View
23 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();
+});
Please sign in to comment.
Something went wrong with that request. Please try again.