From e52eb75ac8b399d43d854126d79f1ec44fb34b22 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Mon, 1 Dec 2014 11:52:09 -0400 Subject: [PATCH] Implement constructRemoteUrl helper function --- lib/helpers/helpers.coffee | 8 ++++++++ lib/helpers/helpers.spec.coffee | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/helpers/helpers.coffee b/lib/helpers/helpers.coffee index 066183ca..8616bca8 100644 --- a/lib/helpers/helpers.coffee +++ b/lib/helpers/helpers.coffee @@ -1,3 +1,4 @@ +url = require('url') _ = require('lodash') resin = require('../resin') @@ -12,3 +13,10 @@ exports.isDeviceUUIDValid = (uuid, callback) -> return callback?(error) if error? uuidExists = _.findWhere(devices, { uuid })? return callback(null, uuidExists) + +exports.constructRemoteUrl = (base, path, query) -> + if not _.isEmpty(query) + formattedQuery = url.format({ query }) + path = url.resolve(path, formattedQuery) + + return url.resolve(base, path) diff --git a/lib/helpers/helpers.spec.coffee b/lib/helpers/helpers.spec.coffee index 8daaf351..2f039df9 100644 --- a/lib/helpers/helpers.spec.coffee +++ b/lib/helpers/helpers.spec.coffee @@ -63,3 +63,19 @@ describe 'Helpers:', -> expect(error).to.not.exist expect(isValid).to.be.false done() + + describe '#constructRemoteUrl', -> + + URL = + base: 'http://google.com' + path: '/search' + query: + foo: 'bar' + + it 'should concatenate a url and a path', -> + result = helpers.constructRemoteUrl(URL.base, URL.path) + expect(result).to.equal(URL.base + URL.path) + + it 'should add url queries', -> + result = helpers.constructRemoteUrl(URL.base, URL.path, URL.query) + expect(result).to.equal("#{URL.base}#{URL.path}?foo=bar")