Permalink
Browse files

Started with saving API..

  • Loading branch information...
1 parent 3257828 commit de8be4ba91a604fab3f13aa41aa35c1f1f974b9b @toots committed May 13, 2012
Showing with 165 additions and 43 deletions.
  1. +3 −3 Cakefile
  2. +22 −6 lib/browser/asana.js
  3. +1 −1 lib/browser/asana.min.js
  4. +22 −6 lib/node/asana.js
  5. +21 −9 src/node/asana.coffee
  6. +38 −7 test/api.coffee
  7. +58 −11 test/browser/files/bundle.js
View
@@ -22,10 +22,10 @@ task 'build', 'Compile coffee scripts into plain Javascript files', ->
console.log "Done!"
task 'test', 'Run the tests', (args) ->
- key = args.key || process.env["ASANA_KEY"]
- app = args.app || process.env["ASANA_WORKSPACE"]
+ key = args.key || process.env["ASANA_KEY"]
+ workspace = args.workspace || process.env["ASANA_WORKSPACE"]
build ->
exec "rm -rf tmp && mkdir tmp && cp src/node/*.coffee test/*.coffee tmp", ->
- require("./tmp/api")(key, app)
+ require("./tmp/api")(key, workspace)
View
@@ -346,9 +346,9 @@ exports.extname = function(path) {
require.define("/asana.coffee", function (require, module, exports, __dirname, __filename) {
(function() {
- var Asana, Backbone, addObjects, b64, defaults, isEmpty, querystringify, _ref;
+ var Asana, Backbone, addObjects, b64, clone, defaults, isEmpty, querystringify, _ref;
- _ref = require("./utils"), b64 = _ref.b64, defaults = _ref.defaults, querystringify = _ref.querystringify, isEmpty = _ref.isEmpty;
+ _ref = require("./utils"), b64 = _ref.b64, defaults = _ref.defaults, clone = _ref.clone, querystringify = _ref.querystringify, isEmpty = _ref.isEmpty;
Backbone = require("backbone");
@@ -371,6 +371,16 @@ require.define("/asana.coffee", function (require, module, exports, __dirname, _
method: "GET",
expects: 200
};
+ },
+ update: function(model) {
+ var query;
+ query = clone(model.attributes);
+ delete query.id;
+ return {
+ method: "PUT",
+ expects: 200,
+ query: query
+ };
}
};
if (this.asana.params.scheme === "https") {
@@ -388,12 +398,18 @@ require.define("/asana.coffee", function (require, module, exports, __dirname, _
Asana.prototype.sync = function(method, model, opts) {
var error, expects, headers, http_opts, options, params, query, req, success, url;
if (opts == null) opts = {};
- params = model.asana[method]();
+ params = model.asana[method](model);
url = typeof this.url === "function" ? this.url() : this.url;
expects = params.expects || 200;
- query = params.query;
error = opts.error || function() {};
success = opts.success || function() {};
+ if (method === "GET") {
+ query = void 0;
+ } else {
+ query = {
+ data: params.query || {}
+ };
+ }
options = defaults(this.asana.params.options, opts.asana);
headers = {
"Accept": "application/json",
@@ -417,8 +433,8 @@ require.define("/asana.coffee", function (require, module, exports, __dirname, _
}
if (query != null) {
query = JSON.stringify(query);
- opts.headers["Content-Type"] = "application/json";
- opts.headers["Content-Length"] = query.length;
+ http_opts.headers["Content-Type"] = "application/json";
+ http_opts.headers["Content-Length"] = query.length;
}
req = this.asana.http.request(http_opts, function(res) {
var data;
View

Large diffs are not rendered by default.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,4 +1,4 @@
-{b64,defaults,
+{b64,defaults,clone,
querystringify,
isEmpty} = require "./utils"
Backbone = require "backbone"
@@ -15,13 +15,21 @@ class Asana
options : opts.options || {}
# Backbone
- Backbone : opts.Backbone || Backbone
+ Backbone: opts.Backbone || Backbone
# Default methods
- read : ->
+ read: ->
method : "GET"
expects : 200
+ update: (model) ->
+ query = clone model.attributes
+ delete query.id
+
+ method : "PUT"
+ expects : 200
+ query : query
+
# For browserify..
if @asana.params.scheme == "https"
@asana.http = require "https"
@@ -38,13 +46,17 @@ class Asana
@user.url = "/users/me"
sync: (method, model, opts = {}) ->
- params = model.asana[method]()
+ params = model.asana[method] model
url = if typeof @url == "function" then @url() else @url
expects = params.expects || 200
- query = params.query
- error = opts.error || ->
- success = opts.success || ->
+ error = opts.error || ->
+ success = opts.success || ->
+
+ if method == "GET"
+ query = undefined
+ else
+ query = data: params.query || {}
# Get options but remove error and success..
options = defaults @asana.params.options, opts.asana
@@ -71,8 +83,8 @@ class Asana
if query?
query = JSON.stringify query
- opts.headers["Content-Type"] = "application/json"
- opts.headers["Content-Length"] = query.length
+ http_opts.headers["Content-Type"] = "application/json"
+ http_opts.headers["Content-Length"] = query.length
req = @asana.http.request http_opts, (res) ->
data = ""
View
@@ -13,19 +13,41 @@ testObject = (name, object, opts, fn) ->
opts = {}
opts.success = ->
- console.log "#{name}:"
- console.dir object.toJSON()
+ console.log "#{name}:"
+ console.dir object.toJSON()
- fn()
+ fn()
opts.error = (model, err) ->
- console.log "Error while fetching #{name}:"
- console.dir err
+ console.log "Error while fetching #{name}:"
+ console.dir err
object.fetch opts
-module.exports = (key, workspace) ->
- {user, Users, Workspaces}= new Asana
+saveObject = (name, object, opts, fn) ->
+ if typeof opts == "object"
+ fn = fn || ->
+ else
+ unless fn?
+ fn = opts || ->
+ opts = {}
+
+ opts.success = ->
+ console.log "#{name} Saved:"
+ console.dir object.toJSON()
+
+ fn()
+
+ opts.error = (model, err) ->
+ console.log "Error while save #{name}:"
+ console.dir err
+
+ object.save null, opts
+
+module.exports = (key, workspaceID) ->
+ {user, Users,
+ Workspaces,
+ Workspace} = new Asana
key : key
testObject "my user", user
@@ -59,3 +81,12 @@ module.exports = (key, workspace) ->
story = task.stories.models[0]
testObject "one story", story
+
+
+ workspace = new Workspace
+ id : workspaceID
+
+ testObject "Test workspace", workspace, ->
+ workspace.set name: "Updated test workspace"
+
+ saveObject "Test workspace", workspace
@@ -837,7 +837,7 @@ EventEmitter.prototype.listeners = function(type) {
require.define("/api.coffee", function (require, module, exports, __dirname, __filename) {
(function() {
- var Asana, inspect, testObject;
+ var Asana, inspect, saveObject, testObject;
Asana = require("./asana");
@@ -877,18 +877,40 @@ require.define("/api.coffee", function (require, module, exports, __dirname, __f
return object.fetch(opts);
};
- module.exports = function(key, workspace) {
- var Users, Workspaces, user, users, workspaces, _ref;
+ saveObject = function(name, object, opts, fn) {
+ if (typeof opts === "object") {
+ fn = fn || function() {};
+ } else {
+ if (fn == null) {
+ fn = opts || function() {};
+ opts = {};
+ }
+ }
+ opts.success = function() {
+ console.log("" + name + " Saved:");
+ console.dir(object.toJSON());
+ return fn();
+ };
+ opts.error = function(model, err) {
+ console.log("Error while save " + name + ":");
+ return console.dir(err);
+ };
+ return object.save(null, opts);
+ };
+
+ module.exports = function(key, workspaceID) {
+ var Users, Workspace, Workspaces, user, users, workspace, workspaces, _ref;
_ref = new Asana({
key: key
- }), user = _ref.user, Users = _ref.Users, Workspaces = _ref.Workspaces;
+ }), user = _ref.user, Users = _ref.Users, Workspaces = _ref.Workspaces, Workspace = _ref.Workspace;
testObject("my user", user);
users = new Users;
testObject("users", users, function() {
return testObject("one user", users.models[0]);
});
workspaces = new Workspaces;
- return testObject("workspaces", workspaces, function() {
+ testObject("workspaces", workspaces, function() {
+ var workspace;
workspace = workspaces.models[0];
return testObject("one workspace", workspace, function() {
testObject("workspace users", workspace.users);
@@ -916,6 +938,15 @@ require.define("/api.coffee", function (require, module, exports, __dirname, __f
});
});
});
+ workspace = new Workspace({
+ id: workspaceID
+ });
+ return testObject("Test workspace", workspace, function() {
+ workspace.set({
+ name: "Updated test workspace"
+ });
+ return saveObject("Test workspace", workspace);
+ });
};
}).call(this);
@@ -924,9 +955,9 @@ require.define("/api.coffee", function (require, module, exports, __dirname, __f
require.define("/asana.coffee", function (require, module, exports, __dirname, __filename) {
(function() {
- var Asana, Backbone, addObjects, b64, defaults, isEmpty, querystringify, _ref;
+ var Asana, Backbone, addObjects, b64, clone, defaults, isEmpty, querystringify, _ref;
- _ref = require("./utils"), b64 = _ref.b64, defaults = _ref.defaults, querystringify = _ref.querystringify, isEmpty = _ref.isEmpty;
+ _ref = require("./utils"), b64 = _ref.b64, defaults = _ref.defaults, clone = _ref.clone, querystringify = _ref.querystringify, isEmpty = _ref.isEmpty;
Backbone = require("backbone");
@@ -949,6 +980,16 @@ require.define("/asana.coffee", function (require, module, exports, __dirname, _
method: "GET",
expects: 200
};
+ },
+ update: function(model) {
+ var query;
+ query = clone(model.attributes);
+ delete query.id;
+ return {
+ method: "PUT",
+ expects: 200,
+ query: query
+ };
}
};
if (this.asana.params.scheme === "https") {
@@ -966,12 +1007,18 @@ require.define("/asana.coffee", function (require, module, exports, __dirname, _
Asana.prototype.sync = function(method, model, opts) {
var error, expects, headers, http_opts, options, params, query, req, success, url;
if (opts == null) opts = {};
- params = model.asana[method]();
+ params = model.asana[method](model);
url = typeof this.url === "function" ? this.url() : this.url;
expects = params.expects || 200;
- query = params.query;
error = opts.error || function() {};
success = opts.success || function() {};
+ if (method === "GET") {
+ query = void 0;
+ } else {
+ query = {
+ data: params.query || {}
+ };
+ }
options = defaults(this.asana.params.options, opts.asana);
headers = {
"Accept": "application/json",
@@ -995,8 +1042,8 @@ require.define("/asana.coffee", function (require, module, exports, __dirname, _
}
if (query != null) {
query = JSON.stringify(query);
- opts.headers["Content-Type"] = "application/json";
- opts.headers["Content-Length"] = query.length;
+ http_opts.headers["Content-Type"] = "application/json";
+ http_opts.headers["Content-Length"] = query.length;
}
req = this.asana.http.request(http_opts, function(res) {
var data;

0 comments on commit de8be4b

Please sign in to comment.