Permalink
Browse files

Added ability to add a task

  • Loading branch information...
benadida committed Aug 16, 2012
1 parent a6c25f9 commit 9d420d52289a188c6db8afd1245f39eab6c70568
Showing with 62 additions and 14 deletions.
  1. +17 −14 lib/asana-api/client.js
  2. +33 −0 lib/asana-api/tasks.js
  3. +12 −0 test/tasks-test.js
View
@@ -4,13 +4,13 @@
* (C) 2010, Nodejitsu Inc.
*
*/
-
+
var events = require('events'),
qs = require('querystring'),
errs = require('errs'),
utile = require('utile'),
request = require('request'),
- base64 = utile.base64;
+ base64 = utile.base64;
var resources = {
Projects: require('./projects').Projects,
@@ -27,15 +27,15 @@ var resources = {
//
var Client = exports.Client = function (options) {
var self = this;
-
+
events.EventEmitter.call(this);
this.url = 'https://app.asana.com/api/1.0';
this.apiKey = options.apiKey;
this.auth = 'Basic ' + base64.encode([options.apiKey, ''].join(':'));
-
+
Object.keys(resources).forEach(function (name) {
- self[name.toLowerCase()] = new (resources[name])(self);
+ self[name.toLowerCase()] = new (resources[name])(self);
});
};
@@ -69,25 +69,28 @@ Client.prototype.successCodes = {
//
Client.prototype.request = function (options, callback) {
var self = this;
-
+
if (typeof options === 'string') {
options = { path: options };
}
-
+
options.method = options.method || 'GET';
options.uri = this.url + options.path;
options.headers = options.headers || {};
options.headers['content-type'] = options.headers['content-type'] || 'application/json';
options.headers['authorization'] = options.headers['authorization'] || this.auth;
-
+
if (options.query) {
options.uri += '?' + qs.stringify(options.query);
delete options.query;
}
-
- if (options.headers['content-type'] === 'application/json'
- && options.body) {
- options.body = JSON.stringify(options.body);
+
+ if (options.body) {
+ if (options.headers['content-type'] === 'application/json') {
+ options.body = JSON.stringify(options.body);
+ } else {
+ options.body = qs.stringify(options.body);
+ }
}
return request(options, function (err, response, body) {
@@ -98,14 +101,14 @@ Client.prototype.request = function (options, callback) {
var statusCode = response.statusCode.toString(),
result,
error;
-
+
try {
result = JSON.parse(body);
}
catch (ex) {
// Ignore Errors
}
-
+
if (Object.keys(self.failCodes).indexOf(statusCode) !== -1) {
return callback(errs.create({
message: 'Asana Error (' + statusCode + '): ' + self.failCodes[statusCode],
View
@@ -13,4 +13,37 @@ Tasks.prototype.get = function (task, callback) {
return this.client.request('/tasks/' + task, callback, function (res, result) {
callback(null, result.data);
});
+};
+
+// expects taskData to contain
+// name, notes, due_on, assignee, assignee_status
+// as per
+// http://developer.asana.com/documentation/#tasks
+Tasks.prototype.create = function(workspaceId, projectId, taskData, callback) {
+ taskData.workspace = workspaceId;
+
+ var options = {
+ method: "POST",
+ path: "/tasks",
+ body: {data: taskData},
+ };
+
+ var self = this;
+ return this.client.request(options, function(res, result) {
+ if (!result)
+ return callback(res.result.errors);
+
+ var task = result;
+
+ self.client.request({
+ method: "POST",
+ path: "/tasks/" + result.id + "/addProject",
+ body: {data: {project: projectId}}
+ }, function(res, result) {
+ if (!result)
+ return callback(res.result.errors);
+
+ callback(null, task);
+ });
+ });
};
View
@@ -25,6 +25,18 @@ vows.describe('asana-api/tasks').addBatch({
assert.isNull(err);
assert.isTask(task);
}
+ },
+ "the tasks.create() method": {
+ topic: function(client) {
+ client.tasks.create(config.workspaces[0], config.projects[0], {
+ name: 'testing name',
+ notes: 'testing notes'
+ }, this.callback);
+ },
+ "should respond with a valid task": function(err, task) {
+ assert.isNull(err);
+ assert.isTask(task);
+ }
}
}
}).export(module);

0 comments on commit 9d420d5

Please sign in to comment.