app-automate.js 2.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
var util = require("util");
var querystring = require("querystring");
var BaseClient = require("./client");
var extend = require("./extend");

function AppAutomateClient(settings) {
	this.server = {
		host: "api.browserstack.com"
	};
	BaseClient.call(this, settings);
}

util.inherits(AppAutomateClient, BaseClient);

// public API
extend(AppAutomateClient.prototype, {
	getPlan: function(fn) {
		this.request({
			path: this.path("/plan.json")
		}, fn);
	},

	getProjects: function(fn) {
		this.request({
			path: this.path("/projects.json")
		}, this.handleResponse(fn, this.stripChildKeys("automation_project")));
	},

	getProject: function(id, fn) {
		this.request({
			path: this.path("/projects/" + id + ".json")
		}, this.handleResponse(fn, function(project) {
			project = project.project;
			project.builds = this.stripChildKeys("automation_build")(project.builds);
			return project;
		}.bind(this)));
	},

	getBuilds: function(options, fn) {
		if (typeof options === "function") {
			fn = options;
			options = {};
		}

		this.request({
			path: this.path("/builds.json?" + querystring.stringify(options))
		}, this.handleResponse(fn, this.stripChildKeys("automation_build")));
	},

	getSessions: function(buildId, options, fn) {
		if (typeof fn === "undefined") {
			fn = options;
			options = {};
		}

		this.request({
			path: this.path("/builds/" + buildId + "/sessions.json?" +
				querystring.stringify(options))
		}, this.handleResponse(fn, this.stripChildKeys("automation_session")));
	},

	getSession: function(id, fn) {
		this.request({
			path: this.path("/sessions/" + id + ".json")
		}, this.handleResponse(fn, this.stripKey("automation_session")));
	},

	updateSession: function(id, options, fn) {
		var data = JSON.stringify(options);
		this.request({
			method: "PUT",
			path: this.path("/sessions/" + id + ".json")
		}, data, this.handleResponse(fn, this.stripKey("automation_session")));
	},

	deleteSession: function(id, fn) {
		this.request({
			method: "DELETE",
			path: this.path("/sessions/" + id + ".json")
		}, fn);
	}
});

// internal API
extend(AppAutomateClient.prototype, {
	path: function(path) {
		return "/app-automate" + path;
	},

	handleResponse: function(fn, modifier) {
		return function(error, data) {
			if (error) {
				return fn(error);
			}

			fn(null, modifier(data));
		};
	},

	stripKey: function(key) {
		return function(item) {
			return item[key];
		};
	},

	stripChildKeys: function(key) {
		return function(items) {
			return items.map(function(item) {
				return item[key];
			});
		};
	}
});

module.exports = {
	createClient: function(settings) {
		return new AppAutomateClient(settings);
	}
};