Skip to content

Commit c73ba54

Browse files
committed
Merge pull request #9 from JWMarchant/master
Added in support for dimensions
2 parents 20e0728 + 34bfed4 commit c73ba54

File tree

3 files changed

+215
-3
lines changed

3 files changed

+215
-3
lines changed

lib/OptimizelyClient.js

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @fileOverview Optimizely Client for Node
33
* @name Optimizely Client
44
* @author Arun
5-
* @version 0.3.0
5+
* @version 0.5.0
66
*/
77

88
/** @access private */
@@ -653,4 +653,113 @@ OptimizelyClient.prototype.getAudiences = function(options){
653653
headers: this.baseHeaders
654654
});
655655
}
656+
657+
////////////////
658+
//5. Dimensions
659+
////////////////
660+
/**
661+
* @pubilc
662+
* @name OptimizelyClient#getDimension
663+
* @since 0.5.0
664+
* @description Read a dimension in Optimizely
665+
* @param {object} options An object with the following properties:
666+
* {
667+
* @param {string|number} id The Dimension ID
668+
* }
669+
* @returns {promise} A promise fulfilled with the Dimension
670+
* @note the id may be passed as a string instead of a member of an object
671+
*/
672+
OptimizelyClient.prototype.getDimension = function(options) {
673+
if (typeof options === "string") options = {
674+
id: options
675+
};
676+
if (!options.id) throw new Error("Required: options.id");
677+
var theUrl = this.baseUrl + 'dimensions/' + options.id;
678+
return rest.getAsync(theUrl, {
679+
method: 'get',
680+
headers: this.baseHeaders
681+
});
682+
}
683+
/**
684+
* @pubilc
685+
* @name OptimizelyClient#createDimension
686+
* @since 0.5.0
687+
* @description Create an Dimension in Optimizely
688+
* @param {object} options An object with the following properties:
689+
* {
690+
* @param {String} id Project ID
691+
* @param {String} name
692+
* @param {String} [description]
693+
* @param {Boolean} [client_api_name] A unique name to refer to this dimension
694+
* }
695+
* @returns {promise} A promise fulfilled with the created project
696+
*/
697+
OptimizelyClient.prototype.createDimension = function(options) {
698+
var optionsToSend = {};
699+
options = options || {};
700+
if (!options.name) throw new Error("Required: options.name");
701+
if (!options.id) throw new Error("Required: options.id");
702+
703+
optionsToSend.name = options.name;
704+
optionsToSend.id = options.id;
705+
optionsToSend.description = options.description || "";
706+
optionsToSend.client_api_name = options.client_api_name || "";
707+
708+
var postUrl = this.baseUrl + 'projects/' + options.id + '/dimensions/';
709+
return rest.postAsync(postUrl, {
710+
method: 'post',
711+
headers: this.baseHeaders,
712+
data: JSON.stringify(optionsToSend)
713+
})
714+
}
715+
/**
716+
* @public
717+
* @name OptimizelyClient#updateDimension
718+
* @since 0.5.0
719+
* @description Update an Existing Dimension in Optimizely
720+
* @param {object} options object with the following properties:
721+
* {
722+
* @param {String} id
723+
* @param {String} [name]
724+
* @param {String} [description]
725+
* @param {Boolean} [client_api_name] A unique name to refer to this dimension
726+
* }
727+
* @return {promise} A promise fulfilled with the updated audience
728+
*/
729+
OptimizelyClient.prototype.updateDimension = function(options) {
730+
options = options || {};
731+
options.id = options.id || false;
732+
if(!options.id) throw new Error('required: options.id');
733+
var putUrl = this.baseUrl + 'dimensions/' + options.id;
734+
return rest.putAsync(putUrl, {
735+
method: 'put',
736+
headers: this.baseHeaders,
737+
data: JSON.stringify(options)
738+
});
739+
}
740+
/**
741+
* @public
742+
* @name OptimizelyClient#getDimensions
743+
* @since 0.5.0
744+
* @description Retrieves a list of Dimensions in a project from Optimizely
745+
* @param {object} options An object with the following properties:
746+
* {
747+
* @param {string|number} id The Project ID
748+
* }
749+
* @return {promise} A promise fulfilled with an array of all Audiences
750+
*
751+
*/
752+
OptimizelyClient.prototype.getDimensions = function(options){
753+
if (typeof options === "string" || typeof options === "number") options = {
754+
id: options
755+
};
756+
options = options || {};
757+
options.id = options.id || "";
758+
if (!options.id) throw new Error("required: options.id");
759+
var theUrl = this.baseUrl + 'projects/' + options.id + '/dimensions/';
760+
return rest.getAsync(theUrl, {
761+
method: 'get',
762+
headers: this.baseHeaders
763+
});
764+
}
656765
module.exports = OptimizelyClient;

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "optimizely-node-client",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "Optimizely Client for Node",
55
"main": "index.js",
66
"scripts": {
@@ -18,7 +18,8 @@
1818
},
1919
"contributors": [
2020
"Nathan Selvidge <nselvidge@gmail.com> (http://www.nathanselvidge.com)",
21-
"Nathan Heaps <nsheaps@gmail.com> (http://www.github.com/nsheaps)"
21+
"Nathan Heaps <nsheaps@gmail.com> (http://www.github.com/nsheaps)",
22+
"James Marchant <jwmarchant@gmail.com> (https://github.com/JWMarchant)"
2223
],
2324
"license": "MIT",
2425
"repository": {

test/test.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ var PROJECTID = hat();
1212
var EXPERIMENTID = hat();
1313
var VARIATIONID = hat();
1414
var AUDIENCEID = hat();
15+
var DIMENSIONID = hat();
1516
var PROJECTNAME = "PROJECTNAME";
1617
var AUDIENCENAME = "AUDIENCENAME";
18+
var DIMENSIONNAME = "DIMENSIONNAME";
1719
var EXPERIMENTDESCRIPTION = "DESCRIPTION OF EXPERIMENT";
1820
var VARIATIONDESCRIPTION = "DESCRIPTION OF VARIATION";
1921
var baseUrl = 'https://www.optimizelyapis.com/experiment/v1';
@@ -582,6 +584,106 @@ describe("Successful API Calls", function() {
582584
})
583585
});
584586
})
587+
//////////////////
588+
//Dimension Tests
589+
//////////////////
590+
describe("Dimensions", function() {
591+
/**
592+
* Set up the Audience Test Paths here
593+
*/
594+
before(function(){
595+
scope.get('/dimensions/' + DIMENSIONID) //get
596+
.reply(200, function(uri, requestBody) {
597+
return stripPathEnd(uri);
598+
});
599+
scope.post('/projects/' + PROJECTID + '/dimensions/') //create
600+
.reply(201, function(uri, requestBody) {
601+
requestBody = JSON.parse(requestBody);
602+
requestBody.id = DIMENSIONID;
603+
return requestBody;
604+
});
605+
scope.put('/dimensions/' + DIMENSIONID) //update
606+
.reply(202, function(uri, requestBody) {
607+
requestBody = JSON.parse(requestBody);
608+
requestBody.id = DIMENSIONID;
609+
return requestBody;
610+
});
611+
scope.get('/projects/' + PROJECTID + '/dimensions/') //get
612+
.reply(200, function(uri, requestBody) {
613+
return [ {
614+
"id": DIMENSIONID,
615+
"name": DIMENSIONNAME
616+
} ];
617+
});
618+
});
619+
/**
620+
* Describe the Dimension functions here
621+
*/
622+
it('should create a dimension', function(done) {
623+
var options = {
624+
"id": PROJECTID,
625+
"name": DIMENSIONNAME
626+
}
627+
client.createDimension(options)
628+
.then(
629+
function(dimension) {
630+
dimension = JSON.parse(dimension);
631+
assert.equal(dimension.name,
632+
DIMENSIONNAME);
633+
done();
634+
},
635+
function(error) {
636+
done(error);
637+
}
638+
)
639+
});
640+
it('should get a dimension', function(done) {
641+
var options = {
642+
"id": DIMENSIONID
643+
}
644+
client.getDimension(options)
645+
.then(
646+
function(id) {
647+
assert.equal(id, DIMENSIONID);
648+
done();
649+
},
650+
function(error) {
651+
done(error);
652+
}
653+
)
654+
});
655+
it('should update a dimension', function(done) {
656+
var options = {
657+
"id": DIMENSIONID,
658+
"name": "New " + DIMENSIONNAME
659+
}
660+
client.updateDimension(options)
661+
.then(
662+
function(dimension) {
663+
dimension = JSON.parse(dimension);
664+
assert.equal(dimension.name,
665+
"New " + DIMENSIONNAME);
666+
done();
667+
},
668+
function(error) {
669+
done(error);
670+
}
671+
)
672+
});
673+
it('should return a list of dimensions', function(done){
674+
var options = {
675+
"id": PROJECTID
676+
}
677+
client.getDimensions(options).then(function(reply){
678+
reply = JSON.parse(reply);
679+
assert.equal(reply[0].id, DIMENSIONID);
680+
assert.equal(reply[0].name, DIMENSIONNAME);
681+
done();
682+
}, function (error){
683+
done(error);
684+
})
685+
});
686+
})
585687
})
586688

587689
////////////////////////

0 commit comments

Comments
 (0)