From fd68bab4d85819d8e84bcefe5a5df89b1bda0817 Mon Sep 17 00:00:00 2001 From: Andreas Koeberle Date: Wed, 31 Jul 2013 21:45:09 +0200 Subject: [PATCH] support promises --- lib/svpply.js | 89 ++++++++++++++++++++++++++++----------------------- package.json | 3 +- 2 files changed, 51 insertions(+), 41 deletions(-) diff --git a/lib/svpply.js b/lib/svpply.js index 4f86a4e..966e197 100644 --- a/lib/svpply.js +++ b/lib/svpply.js @@ -5,10 +5,11 @@ var request = require('request'), querystring = require('querystring'), + when = require('when'), Svpply = {}; Svpply.API = function (clientKey, clientSecret) { - + this.host = 'https://api.svpply.com'; this.path = '/v1'; @@ -31,7 +32,10 @@ Svpply.API.prototype.get = function(path, params, callback) { params = {}; } + params || (params = {}); + var requestUri; + var deferred = when.defer(); if (typeof params === "object") { if (this.client_id && this.client_secret) { @@ -46,38 +50,43 @@ Svpply.API.prototype.get = function(path, params, callback) { var options = { uri: requestUri }; - + request(options, function (error, response, result) { if (!error) { - callback(JSON.parse(result)); + var parsedResult = JSON.parse(result); + callback && callback(parsedResult); + deferred.resolve(parsedResult); } else { - callback(error); + callback && callback(error); + deferred.reject(error); } }); + + return deferred.promise; }; Svpply.API.prototype.post = function (path, params, callback) { params.client_id = this.client_id; params.client_secret = this.client_secret; - + var requestData = querystring.stringify(params); var requestHeaders = { 'Content-Length': requestData.length }; - - var options = { + + var options = { 'uri': this.host + this.path + path, 'headers': requestHeaders, 'method': 'POST', 'body': requestData }; - - request(options, function (error, response, result) { + + request(options, function (error, response, result) { if (!error) { callback(JSON.parse(result)); - } else { + } else { callback(error); } }); @@ -94,22 +103,22 @@ Svpply.API.Products = function (api) { Svpply.API.Products.prototype = { show: function (id, callback) { - this.api.get('/products/' + id + '.json', callback); + return this.api.get('/products/' + id + '.json', callback); }, find: function (query, callback) { - this.api.get('/products/search.json', query, callback); + return this.api.get('/products/search.json', query, callback); }, collections: function (id, callback) { - this.api.get('/products/' + id + '/collections.json', callback); + return this.api.get('/products/' + id + '/collections.json', callback); }, users: function (id, callback) { - this.api.get('/products/' + id + '/users.json', callback); + return this.api.get('/products/' + id + '/users.json', callback); }, collections: function (id, callback) { - this.api.get('/products/' + id + '/collections.json', callback); + return this.api.get('/products/' + id + '/collections.json', callback); }, comments: function (id, callback) { - this.api.get('/products/' + id + '/comments.json', callback); + return this.api.get('/products/' + id + '/comments.json', callback); } }; @@ -124,46 +133,46 @@ Svpply.API.Users = function (api) { Svpply.API.Users.prototype = { show: function (id, callback) { - this.api.get('/users/' + id + '.json', callback); + return this.api.get('/users/' + id + '.json', callback); }, find: function (query, callback) { - this.api.get('/users/search.json', query, callback); + return this.api.get('/users/search.json', query, callback); }, wants: function (id, callback) { - this.api.get('/users/' + id + '/wants/products.json', callback); + return this.api.get('/users/' + id + '/wants/products.json', callback); }, wanted: function (userId, productId, callback) { - this.api.get('/users/' + userId + '/wants/products/' + productId + '.json', callback); + return this.api.get('/users/' + userId + '/wants/products/' + productId + '.json', callback); }, owns: function (id, callback) { - this.api.get('/users/' + id + '/owns/products.json', callback); + return this.api.get('/users/' + id + '/owns/products.json', callback); }, owned: function (userId, productId, callback) { - this.api.get('/users/' + userId + '/owns/products/' + productId + '.json', callback); + return this.api.get('/users/' + userId + '/owns/products/' + productId + '.json', callback); }, following: function (id, callback) { - this.api.get('/users/' + id + '/following/users.json', callback); + return this.api.get('/users/' + id + '/following/users.json', callback); }, isFollowingUser: function (userOne, userTwo, callback) { - this.api.get('/users/' + userOne + '/following/users/' + userTwo + '.json', callback); + return this.api.get('/users/' + userOne + '/following/users/' + userTwo + '.json', callback); }, stores: function (id, callback) { - this.api.get('/users/' + id + '/following/stores.json', callback); + return this.api.get('/users/' + id + '/following/stores.json', callback); }, isFollowingStore: function (userId, productId, callback) { - this.api.get('/users/' + userId + '/following/stores/' + productId + '.json', callback); + return this.api.get('/users/' + userId + '/following/stores/' + productId + '.json', callback); }, searches: function (id, callback) { - this.api.get('/users/' + id + '/following/searches.json', callback); + return this.api.get('/users/' + id + '/following/searches.json', callback); }, followers: function (id, callback) { - this.api.get('/users/' + id + '/followers/users.json', callback); + return this.api.get('/users/' + id + '/followers/users.json', callback); }, collections: function (id, callback) { - this.api.get('/users/' + id + '/collections.json', callback); + return this.api.get('/users/' + id + '/collections.json', callback); }, collection: function (userId, collectionId, callback) { - this.api.get('/users/' + userId + '/collections/' + collectionId + '.json', callback); + return this.api.get('/users/' + userId + '/collections/' + collectionId + '.json', callback); } }; @@ -178,19 +187,19 @@ Svpply.API.Collections = function (api) { Svpply.API.Collections.prototype = { show: function (id, callback) { - this.api.get('/collections/' + id + '.json', callback); + return this.api.get('/collections/' + id + '.json', callback); }, find: function (query, callback) { - this.api.get('/collections/search.json', query, callback); + return this.api.get('/collections/search.json', query, callback); }, products: function (id, callback) { - this.api.get('/collections/' + id + '/products.json', callback); + return this.api.get('/collections/' + id + '/products.json', callback); }, users: function (id, callback) { - this.api.get('/collections/' + id + '/users.json', callback); + return this.api.get('/collections/' + id + '/users.json', callback); }, comments: function (id, callback) { - this.api.get('/collections/' + id + '/comments.json', callback); + return this.api.get('/collections/' + id + '/comments.json', callback); } }; @@ -205,10 +214,10 @@ Svpply.API.Shop = function (api) { Svpply.API.Shop.prototype = { categories: function (callback) { - this.api.get('/shop/categories.json', callback); + return this.api.get('/shop/categories.json', callback); }, products: function (category, subcategory, query, callback) { - this.api.get('/shop/' + category + '/' + subcategory + '.json', query, callback); + return this.api.get('/shop/' + category + '/' + subcategory + '.json', query, callback); } }; @@ -224,13 +233,13 @@ Svpply.API.Stores = function (api) { Svpply.API.Stores.prototype = { show: function (id, callback) { - this.api.get('/stores/' + id + '.json', callback); + return this.api.get('/stores/' + id + '.json', callback); }, products: function (id, callback) { - this.api.get('/stores/' + id + '/products.json', callback); + return this.api.get('/stores/' + id + '/products.json', callback); }, followers: function (id, callback) { - this.api.get('/stores/' + id + '/followers/users.json', callback); + return this.api.get('/stores/' + id + '/followers/users.json', callback); } }; diff --git a/package.json b/package.json index efea3d3..09e09f0 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "node": ">= 0.5.0" }, "dependencies": { - "request": ">= 1.9.0" + "request": ">= 1.9.0", + "when": "~2.2.1" }, "devDependencies": { "vows": "0.7.x"