From 88d0582c24e9f307c2bc74472d4282e45e971e7f Mon Sep 17 00:00:00 2001 From: Eric Hulburd Date: Wed, 8 Feb 2017 13:14:42 -0600 Subject: [PATCH] list experiment variations --- lib/OptimizelyClient.js | 60 ++++++++++++++++++++++++++++------------- test/test.js | 53 ++++++++++++++++++++++++++++-------- 2 files changed, 83 insertions(+), 30 deletions(-) diff --git a/lib/OptimizelyClient.js b/lib/OptimizelyClient.js index ce3dda0..c425559 100644 --- a/lib/OptimizelyClient.js +++ b/lib/OptimizelyClient.js @@ -85,7 +85,7 @@ var OptimizelyClient = function(apiToken, options) { this.baseHeaders = { 'Token': this.apiToken, 'Content-Type': 'application/json' - } + } } } @@ -163,18 +163,18 @@ OptimizelyClient.prototype.getProject = function(options) { * @name OptimizelyClient#updateProject * @since 0.2.0 * @description Update an Existing Project in Optimizely - * @param {object} options object with the following properties: + * @param {object} options object with the following properties: * { - * @param {String} id + * @param {String} id * @param {String} [project_name] - * @param {String} [project_status = "Active|Archived"] - * @param {Boolean} [include_jquery] - * @param {String} [project_javascript] - * @param {Boolean} [enable_force_variation] - * @param {Boolean} [exclude_disabled_experiments] + * @param {String} [project_status = "Active|Archived"] + * @param {Boolean} [include_jquery] + * @param {String} [project_javascript] + * @param {Boolean} [enable_force_variation] + * @param {Boolean} [exclude_disabled_experiments] * @param {Boolean} [exclude_names] - * @param {Boolean} [ip_anonymization] - * @param {String} [ip_filtering] + * @param {Boolean} [ip_anonymization] + * @param {String} [ip_filtering] * } * @return {promise} A promise fulfilled with the updated project */ @@ -392,7 +392,7 @@ OptimizelyClient.prototype.deleteExperiment = function(options) { * @name OptimizelyClient#getResults * @since 0.4.0 * @description get non-stats engine results - * @param {object} options An object with the following properties: + * @param {object} options An object with the following properties: * { * @param {String} id Experiment ID * @param {object} [dimension = {}] An object with the following properties: @@ -429,7 +429,7 @@ OptimizelyClient.prototype.getResults = function(options) { * @name OptimizelyClient#getStats * @since 0.4.0 * @description get stats engine results - * @param {object} options An object with the following properties: + * @param {object} options An object with the following properties: * { * @param {String} id Experiment ID * @param {object} [dimension = {}] An object with the following properties: @@ -464,6 +464,28 @@ OptimizelyClient.prototype.getStats = function(options) { //////////////// //3. Variations //////////////// +/** + *@pubilc + *@name OptimizelyClient#getVariations + *@since 0.6.3 + *@description list variations for an experiment + *@param {object} options An object with the following properties: + *{ + * @param {string|number} experiment_id + *} + */ +OptimizelyClient.prototype.getVariations = function(options) { + options = options || {}; + options.experiment_id = String(options.experiment_id || ""); + if (!options.experiment_id) throw new Error( + "Required: options.experiment_id"); + var url = this.baseUrl + 'experiments/' + options.experiment_id + + '/variations'; + return rest.getAsync(url, { + method: 'get', + headers: this.baseHeaders + }) + } /** *@pubilc *@name OptimizelyClient#createVariation @@ -612,7 +634,7 @@ OptimizelyClient.prototype.getAudience = function(options) { * { * @param {String} id Project ID * @param {String} name - * @param {String} [description] + * @param {String} [description] * @param {Boolean} [segmentation] Only available for platinum * @param {Array} [conditions] See http://developers.optimizely.com/rest/conditions/ * } @@ -642,9 +664,9 @@ OptimizelyClient.prototype.createAudience = function(options) { * @name OptimizelyClient#updateAudience * @since 0.4.0 * @description Update an Existing Project in Optimizely - * @param {object} options object with the following properties: + * @param {object} options object with the following properties: * { - * @param {String} id + * @param {String} id * @param {String} [name] * @param {Array} [conditions] See http://developers.optimizely.com/rest/conditions/ * @param {Boolean} [segmentation] Platinum Customers only @@ -723,7 +745,7 @@ OptimizelyClient.prototype.getDimension = function(options) { * { * @param {String} id Project ID * @param {String} name - * @param {String} [description] + * @param {String} [description] * @param {Boolean} [client_api_name] A unique name to refer to this dimension * } * @returns {promise} A promise fulfilled with the created project @@ -751,11 +773,11 @@ OptimizelyClient.prototype.createDimension = function(options) { * @name OptimizelyClient#updateDimension * @since 0.5.0 * @description Update an Existing Dimension in Optimizely - * @param {object} options object with the following properties: + * @param {object} options object with the following properties: * { - * @param {String} id + * @param {String} id * @param {String} [name] - * @param {String} [description] + * @param {String} [description] * @param {Boolean} [client_api_name] A unique name to refer to this dimension * } * @return {promise} A promise fulfilled with the updated audience diff --git a/test/test.js b/test/test.js index aa88946..85592d3 100644 --- a/test/test.js +++ b/test/test.js @@ -295,7 +295,7 @@ describe("Successful API Calls", function() { } ) }); - scope.intercept('/experiments/' + EXPERIMENTID, 'DELETE') + scope.intercept('/experiments/' + EXPERIMENTID, 'DELETE') .reply(204, function(uri, requestBody) { return requestBody; }); @@ -314,7 +314,7 @@ describe("Successful API Calls", function() { } ) }); - scope.intercept('/experiments/' + EXPERIMENTID + '/results', 'GET') + scope.intercept('/experiments/' + EXPERIMENTID + '/results', 'GET') .reply(200, function(uri, requestBody) { return requestBody; }); @@ -332,7 +332,7 @@ describe("Successful API Calls", function() { } ) }); - scope.intercept('/experiments/' + EXPERIMENTID + '/stats', 'GET') + scope.intercept('/experiments/' + EXPERIMENTID + '/stats', 'GET') .reply(200, function(uri, requestBody) { return requestBody; }); @@ -355,6 +355,37 @@ describe("Successful API Calls", function() { //Variation Tests ////////////////// describe("Variations", function() { + scope.get('/experiments/' + EXPERIMENTID + '/variations') //get multiple + .reply(201, function(uri) { + return [{ + "is_paused": false, + "description": "Original", + "weight": null, + "created": "2014-04-17T00:47:58.390560Z", + "section_id": null, + "js_component": "", + "experiment_id": 854484703, + "project_id": 859720118, + "id": 854613530 + }]; + }); + it('should list variations an experiment', function(done) { + var options = { + "experiment_id": EXPERIMENTID + } + client.getVariations(options) + .then( + function(variations) { + variations = JSON.parse(variations); + assert.equal(variations.length, 1); + assert.equal(variations[0].id, 854613530); + done(); + }, + function(error) { + done(error); + } + ) + }); scope.post('/experiments/' + EXPERIMENTID + '/variations/') //create .reply(201, function(uri, requestBody) { requestBody = JSON.parse(requestBody); @@ -467,7 +498,7 @@ describe("Successful API Calls", function() { } ) }); - scope.intercept('/variations/' + VARIATIONID, 'DELETE') + scope.intercept('/variations/' + VARIATIONID, 'DELETE') .reply(204, function(uri, requestBody) { return; }); @@ -510,7 +541,7 @@ describe("Successful API Calls", function() { requestBody.id = AUDIENCEID; return requestBody; }); - scope.get('/projects/' + PROJECTID + '/audiences/') //get + scope.get('/projects/' + PROJECTID + '/audiences/') //get .reply(200, function(uri, requestBody) { return [ { "id": AUDIENCEID, @@ -610,7 +641,7 @@ describe("Successful API Calls", function() { requestBody.id = DIMENSIONID; return requestBody; }); - scope.get('/projects/' + PROJECTID + '/dimensions/') //get + scope.get('/projects/' + PROJECTID + '/dimensions/') //get .reply(200, function(uri, requestBody) { return [ { "id": DIMENSIONID, @@ -695,7 +726,7 @@ describe("Successful API Calls", function() { * Set up the Goals Test Paths here */ before(function(){ - scope.get('/projects/' + PROJECTID + '/goals/') //get + scope.get('/projects/' + PROJECTID + '/goals/') //get .reply(200, function(uri, requestBody) { return [ { "id": GOALSID, @@ -789,7 +820,7 @@ describe("Unsuccessful API Calls", function() { status: 400, message: FUNNELENVYERROR, uuid: hat() - }; + }; }); it('should not update a project', function(done){ var newProjectName = PROJECTNAME + '2'; @@ -1007,7 +1038,7 @@ describe("Unsuccessful API Calls", function() { } ) }); - scope.intercept('/experiments/' + EXPERIMENTID, 'DELETE') + scope.intercept('/experiments/' + EXPERIMENTID, 'DELETE') .reply(400, function(uri, requestBody) { return { status: 400, @@ -1031,7 +1062,7 @@ describe("Unsuccessful API Calls", function() { } ) }); - scope.intercept('/experiments/' + EXPERIMENTID, 'DELETE') + scope.intercept('/experiments/' + EXPERIMENTID, 'DELETE') .reply(400, function(uri, requestBody) { return { status: 400, @@ -1165,7 +1196,7 @@ describe("Unsuccessful API Calls", function() { } ) }); - scope.intercept('/variations/' + VARIATIONID, 'DELETE') + scope.intercept('/variations/' + VARIATIONID, 'DELETE') .reply(400, function(uri, requestBody) { return { status: 400,