Browse files

Refactored most of the code. Separated API definitions from the code.

Also updated tests.
  • Loading branch information...
1 parent 5edbf10 commit d9e68979c687a310d8295a2d87d12f3e5343b3d8 @andzdroid committed Jun 17, 2011
Showing with 211 additions and 192 deletions.
  1. +151 −0 lib/api.js
  2. +50 −182 lib/posterous.js
  3. +5 −5 tests/sites.js
  4. +4 −4 tests/subscriptions.js
  5. +1 −1 tests/user.js
View
151 lib/api.js
@@ -0,0 +1,151 @@
+module.exports = {
+ 'User': {
+ path: 'users/me',
+ methods: ['GET']
+ },
+ 'Users': {
+ path: 'users',
+ methods: ['POST']
+ },
+ 'Subscriptions': {
+ path: 'users/me/subscriptions',
+ methods: ['GET']
+ },
+ 'SubscriptionPosts': {
+ path: 'users/me/subscriptions/posts',
+ methods: ['GET']
+ },
+ 'Sites': {
+ path: 'sites',
+ methods: ['GET', 'POST']
+ },
+ 'Site': {
+ path: 'sites/{0}',
+ args: 1,
+ methods: ['GET', 'PUT', 'DELETE']
+ },
+ 'Photos': {
+ path: 'sites/{0}/photos/public',
+ args: 1,
+ methods: ['GET']
+ },
+ 'Subscribe': {
+ path: 'sites/{0}/subscribe',
+ args: 1,
+ methods: ['PUT']
+ },
+ 'Unsubscribe': {
+ path: 'sites/{0}/unsubscribe',
+ args: 1,
+ methods: ['PUT']
+ },
+ 'HeaderImage': {
+ path: 'sites/{0}/header_image',
+ args: 1,
+ methods: ['POST', 'DELETE']
+ },
+ 'SiteProfile': {
+ path: 'sites/{0}/profile',
+ args: 1,
+ methods: ['GET', 'POST', 'PUT', 'DELETE']
+ },
+ 'SiteProfileImage': {
+ path: 'sites/{0}/profile/image',
+ args: 1,
+ methods: ['PUT', 'DELETE']
+ },
+ 'Tags': {
+ path: 'sites/{0}/tags',
+ args: 1,
+ methods: ['GET']
+ },
+ 'Contributors': {
+ path: 'sites/{0}/contributors',
+ args: 1,
+ methods: ['GET', 'POST']
+ },
+ 'Contributor': {
+ path: 'sites/{0}/contributors/{1}',
+ args: 2,
+ methods: ['DELETE']
+ },
+ 'Subscribers': {
+ path: 'sites/{0}/subscribers',
+ args: 1,
+ methods: ['GET', 'POST']
+ },
+ 'Subscriber': {
+ path: 'sites/{0}/subscribers/{1}',
+ args: 2,
+ methods: ['DELETE']
+ },
+ 'ExternalSites': {
+ path: 'sites/{0}/external_sites',
+ args: 1,
+ methods: ['GET']
+ },
+ 'ExternalSite': {
+ path: 'sites/{0}/external_sites/{1}',
+ args: 2,
+ methods: ['GET', 'DELETE']
+ },
+ 'Theme': {
+ path: 'sites/{0}/theme',
+ args: 1,
+ methods: ['GET', 'POST']
+ },
+ 'AllPosts': {
+ path: 'explore/public',
+ methods: ['GET']
+ },
+ 'Posts': {
+ path: 'sites/{0}/posts',
+ args: 1,
+ method: ['GET', 'POST']
+ },
+ 'PublicPosts': {
+ path: 'sites/{0}/posts/public',
+ args: 1,
+ method: ['GET']
+ },
+ 'Post': {
+ path: 'sites/{0}/posts/{1}',
+ args: 2,
+ method: ['GET', 'PUT', 'DELETE']
+ },
+ 'PostPhotos': {
+ path: 'sites/{0}/posts/{1}/photos',
+ args: 2,
+ method: ['GET']
+ },
+ 'PostVideos': {
+ path: 'sites/{0}/posts/{1}/videos',
+ args: 2,
+ method: ['GET']
+ },
+ 'PostAudioFiles': {
+ path: 'sites/{0}/posts/{1}/audio_files',
+ args: 2,
+ method: ['GET']
+ },
+ 'Comments': {
+ path: 'sites/{0}/posts/{1}/comments',
+ args: 2,
+ method: ['GET', 'POST']
+ },
+ 'Comment': {
+ path: 'sites/{0}/posts/{1}/comments/{2}',
+ args: 3,
+ method: ['GET', 'PUT', 'DELETE']
+ },
+ 'Likes': {
+ path: 'sites/{0}/posts/{1}/likes',
+ args: 2,
+ method: ['GET', 'POST']
+ },
+ 'Like': {
+ path: 'sites/{0}/posts/{1}/likes/{2}',
+ args: 3,
+ method: ['GET', 'DELETE']
+ }
+};
View
232 lib/posterous.js
@@ -1,5 +1,6 @@
var http = require('http');
var qs = require('querystring');
+var api = require('./api');
var HOST = 'posterous.com';
var PATH = '/api/2/';
@@ -79,214 +80,81 @@ Posterous.prototype.request = function(method, api, args, callback) {
req.end();
};
-
-//Get api token
-Posterous.prototype.auth = function(callback) {
- var t = this;
-
- this.request('GET', 'auth/token', null, function(err, result) {
- if (err) {
- return callback(err);
- }
-
- //Save api token
- t._token = result.api_token;
-
- callback(null, result.api_token);
- });
-};
-
-
-//Users
-Posterous.prototype.getUser = function(callback) {
- this.request('GET', 'users/me', null, function(err, result) {
- callback(err, result);
- });
-};
-
-
-//Subscriptions
-//getSubscriptions([page number,] callback)
-//Use as getSubscriptions(2, callback) for page 2 of subscriptions
-//Or getSubscriptions(callback) for page 1 of subcriptions
-Posterous.prototype.getSubscriptions = function(page, callback) {
- if (callback == undefined) {
- callback = page;
- page = 1;
+Posterous.prototype.makeRequest = function(method, args) {
+ if (args.length < 3) {
+ return console.error('[Posterous] You must pass arguments to the method.');
}
- this.request('GET', 'users/me/subscriptions', {page: page}, function(err, result) {
- callback(err, result);
- });
-};
+ var apiName = args[0];
+ var callback = args[args.length - 1];
+ var options = args[args.length - 2];
-//Subscription posts
-//Use the same way as getSubscriptions
-Posterous.prototype.getSubscriptionPosts = function(page, callback) {
- if (callback == undefined) {
- callback = page;
- page = 1;
+ //Check last argument is a callback function
+ if (typeof callback != 'function') {
+ return console.error('[Posterous] You must pass a callback function.');
}
- this.request('GET', 'users/me/subscriptions/posts', {page: page}, function(err, result) {
- callback(err, result);
- });
-};
-
-
-//Sites
-Posterous.prototype.getSites = function(callback) {
- this.request('GET', 'sites', null, function(err, result) {
- callback(err, result);
- });
-};
-
-Posterous.prototype.createSite = function(options, callback) {
- this.request('POST', 'sites', options, function(err, result) {
- callback(err, result);
- });
-};
-
-Posterous.prototype.getSite = function(id, callback) {
- this.request('GET', 'sites/' + id, null, function(err, result) {
- callback(err, result);
- });
-};
-
-Posterous.prototype.updateSite = function(id, options, callback) {
- this.request('PUT', 'sites/' + id, options, function(err, result) {
- callback(err, result);
- });
-};
-
-Posterous.prototype.deleteSite = function(id, callback) {
- this.request('DELETE', 'sites/' + id, null, function(err, result) {
- callback(err, result);
- });
-};
-
-//Get 20 random photos from a site
-//allowed keys for `options` parameter are `page` and `since_id`
-Posterous.prototype.getPhotos = function(id, options, callback) {
- this.request('GET', 'sites/' + id + '/photos', options, function(err, result) {
- callback(err, result);
- });
-};
-
-Posterous.prototype.subscribe = function(id, callback) {
- this.request('PUT', 'sites/' + id + '/subscribe', null, function(err, result) {
- callback(err, result);
- });
-};
+ //Check API method name
+ if (!(apiName in api)) {
+ return callback('This API method doesn\'t exist.');
+ }
-Posterous.prototype.unsubscribe = function(id, callback) {
- this.request('PUT', 'sites/' + id + '/unsubscribe', null, function(err, result) {
- callback(err, result);
- });
-};
+ var info = api[apiName];
+ var path = info.path;
-Posterous.prototype.setHeader = function(id, file, callback) {
- this.request('POST', 'sites/' + id + '/header_image', {file: file}, function(err, result) {
- callback(err, result);
- });
-};
+ //Check API method
+ if (info.methods.indexOf(method) == -1) {
+ return callback('This API method is not allowed.');
+ }
-Posterous.prototype.deleteHeader = function(id, callback) {
- this.request('DELETE', 'sites/' + id + '/header_image', null, function(err, result) {
- callback(err, result);
- });
-};
+ //Check number of arguments
+ if ('args' in info) {
+ if (args.length - 3 != info.args) {
+ return callback('You passed the wrong number of arguments to the method.');
+ }
+ //Create API path
+ for (var i = 1; i < info.args + 1; i++) {
+ path = path.replace('{' + (i - 1) + '}', args[i]);
+ }
+ }
-//Site profiles
-Posterous.prototype.getSiteProfile = function(id, callback) {
- this.request('GET', 'sites/' + id + '/profile', null, function(err, result) {
- callback(err, result);
- });
-};
+ //console.log(method + ' ' + path);
-Posterous.prototype.createSiteProfile = function(id, options, callback) {
- this.request('POST', 'sites/'+ id + '/profile', options, function(err, result) {
- callback(err, result);
- });
+ this.request(method, path, options, callback);
};
-Posterous.prototype.updateSiteProfile = function(id, options, callback) {
- this.request('PUT', 'sites/' + id + '/profile', options, function(err, result) {
- callback(err, result);
- });
-};
-Posterous.prototype.deleteSiteProfile = function(id, callback) {
- this.request('DELETE', 'sites/' + id + '/profile', null, function(err, result) {
- callback(err, result);
- });
+Posterous.prototype.get = function() {
+ this.makeRequest('GET', arguments);
};
-Posterous.prototype.updateSiteProfileImage = function(id, file, callback) {
- this.request('PUT', 'sites/' + id + '/profile/image', {file: file}, function(err, result) {
- callback(err, result);
- });
+Posterous.prototype.post = function() {
+ this.makeRequest('POST', arguments);
};
-Posterous.prototype.deleteSiteProfileImage = function(id, callback) {
- this.request('DELETE', 'sites/' + id + '/profile/image', null, function(err, result) {
- callback(err, result);
- });
+Posterous.prototype.put = function() {
+ this.makeRequest('PUT', arguments);
};
-
-//Tags
-Posterous.prototype.getTags = function(id, callback) {
- this.request('GET', 'sites/' + id + '/tags', null, function(err, result) {
- callback(err, result);
- });
+Posterous.prototype.del = Posterous.prototype.delete = function() {
+ this.makeRequest('DELETE', arguments);
};
-//Contributors
-Posterous.prototype.getContributors = function(id, callback) {
- this.request('GET', 'sites/' + id + '/contributors', null, function(err, result) {
- callback(err, result);
- });
-};
-
-Posterous.prototype.removeContributor = function(id, user, callback) {
- this.request('DELETE', 'sites/' + id + '/contributors/' + user, null, function(err, result) {
- callback(err, result);
- });
-};
-
-
-//Subscribers
-Posterous.prototype.getSubscribers = function(id, callback) {
- this.request('GET', 'sites/' + id + '/subscribers', null, function(err, result) {
- callback(err, result);
- });
-};
-
-Posterous.prototype.removeSubscriber = function(id, user, callback) {
- this.request('DELETE', 'sites/' + id + '/subscribers/' + user, null, function(err, result) {
- callback(err, result);
- });
-};
+//Get api token
+Posterous.prototype.auth = function(callback) {
+ var t = this;
-//Autopost sites
-Posterous.prototype.getAutopostSites = function(id, callback) {
- this.request('GET', 'sites/' + id + '/external_sites', null, function(err, result) {
- callback(err, result);
- });
-};
+ this.request('GET', 'auth/token', null, function(err, result) {
+ if (err) {
+ return callback(err);
+ }
-Posterous.prototype.getAutopostSite = function(id, autopost, callback) {
- this.request('GET', 'sites/' + id + '/external_sites/' + autopost, null, function(err, result) {
- callback(err, result);
- });
-};
+ //Save api token
+ t._token = result.api_token;
-Posterous.prototype.deleteAutopostSite = function(id, autopost, callback) {
- this.request('DELETE', 'sites/' + id + '/external_sites/' + autopost, null, function(err, result) {
- callback(err, result);
+ callback(null, result.api_token);
});
};
View
10 tests/sites.js
@@ -22,7 +22,7 @@ module.exports['Listing Sites'] = function(test) {
}
console.log('Fetching sites...');
- posterous.getSites(function(err, result) {
+ posterous.get('Sites', null, function(err, result) {
test.ok(!err, 'An error occurred: ' + err);
test.ok(result, 'Could not retrieve list of sites.');
@@ -61,7 +61,7 @@ module.exports['Create, update, retrieve and delete site'] = function(test) {
'site[is_private]': 'true'
};
- posterous.createSite(options, function(err, result) {
+ posterous.post('Sites', options, function(err, result) {
test.ok(!err, 'An error occurred: ' + err);
test.ok(result, 'Could not create a new site');
@@ -71,13 +71,13 @@ module.exports['Create, update, retrieve and delete site'] = function(test) {
var site_id = result.id;
console.log('Updating site name...');
- posterous.updateSite(site_id, {'site[name]': 'unit test stage 2'}, function(err, result) {
+ posterous.put('Site', site_id, {'site[name]': 'unit test stage 2'}, function(err, result) {
test.ok(!err, 'An error occurred: ' + err);
test.ok(result, 'Could not update site name');
console.log('Fetching site details...');
- posterous.getSite(site_id, function(err, result) {
+ posterous.get('Site', site_id, null, function(err, result) {
test.ok(!err, 'An error occurred: ' + err);
test.ok(result, 'Could not fetch site details');
@@ -86,7 +86,7 @@ module.exports['Create, update, retrieve and delete site'] = function(test) {
test.equals(result.name, 'unit test stage 2', 'Site name was not updated.');
console.log('Deleting site...');
- posterous.deleteSite(site_id, function(err, result) {
+ posterous.del('Site', site_id, null, function(err, result) {
test.ok(!err, 'An error occurred: ' + err);
test.ok(result, 'Could not delete site');
View
8 tests/subscriptions.js
@@ -11,13 +11,13 @@ module.exports['Subscribed Sites'] = function(test) {
}
console.log('Fetching page 1 of subscriptions...');
- posterous.getSubscriptions(function(err, result) {
+ posterous.get('Subscriptions', null, function(err, result) {
test.ok(!err, 'An error occurred: ' + err);
test.ok(result, 'Could not retrieve the user\'s subscriptions');
console.log('Fetching page 2 of subscriptions...');
- posterous.getSubscriptions(2, function(err, result) {
+ posterous.get('Subscriptions', {page: 2}, function(err, result) {
test.ok(!err, 'An error occurred: ' + err);
test.ok(result, 'Coudl not retrieve page 2 of subscriptions');
@@ -38,13 +38,13 @@ module.exports['Subscription Posts'] = function(test) {
}
console.log('Fetching page 1 of subscription posts...');
- posterous.getSubscriptionPosts(function(err, result) {
+ posterous.get('SubscriptionPosts', null, function(err, result) {
test.ok(!err, 'An error occurred: ' + err);
test.ok(result, 'Could not retrieve page 1 of subscription posts');
console.log('Fetching page 2 of subscription posts...');
- posterous.getSubscriptionPosts(2, function(err, result) {
+ posterous.get('SubscriptionPosts', {page: 2}, function(err, result) {
test.ok(!err, 'An error occurred: ' + err);
test.ok(result, 'Could not retrieve page 2 of posts');
View
2 tests/user.js
@@ -12,7 +12,7 @@ module.exports['Current User'] = function(test) {
}
console.log('Fetching user details...');
- posterous.getUser(function(err, result) {
+ posterous.get('User', null, function(err, result) {
test.ok(!err, 'An error occurred: ' + err);
test.ok(result, 'Did not retrieve user information');

0 comments on commit d9e6897

Please sign in to comment.