Permalink
Browse files

Added createSite method/test and updated README

  • Loading branch information...
1 parent 9453d4d commit 0dc6a72b80adcdc938a199a64560b784a3eeffa8 @andzdroid committed Jun 11, 2011
Showing with 76 additions and 0 deletions.
  1. +24 −0 README.md
  2. +10 −0 lib/posterous.js
  3. +42 −0 tests/sites.js
View
@@ -52,3 +52,27 @@ There is an optional first parameter for specifying the page number of the resul
`getSubscriptionPosts` retrieves a list of posts from the user's subscribed sites, corresponding to the API endpoint of `users/me/subscriptions/posts`.
There is an optional first parameter for specifying the page number of the results.
+
+### getSites(callback)
+`getSites` retrieves a list of the user's Posterous sites, corresponding to the API endpoint of `sites`.
+
+### createSite(options, callback)
+`createSite` creates a new Posterous site for the user, corresponding to the API endpoint of `sites`.
+
+The options parameter should be an object corresponding to the options in the docs for this API method:
+
+ {
+ 'site[hostname]': hostname,
+ 'site[virtual_host]': virtualhost,
+ 'site[name]': name,
+ 'site[is_private]': 'false',
+ 'site[is_group]': 'false',
+ 'site[time_zone]': 'UTC',
+ 'site[subhead]: subhead
+ }
+
+**site[hostname]** and **site[name]** are required options.
+
+See the API docs for descriptions of the options.
+
+The result will be in the same format as the data returned by `getSites`.
View
@@ -147,6 +147,16 @@ Posterous.prototype.getSites = function getSites(callback) {
});
};
+Posterous.prototype.createSite = function createSite(options, callback) {
+ this.request('POST', 'sites', options, function(err, result) {
+ if (err) {
+ return callback(err);
+ }
+
+ callback(null, result);
+ });
+};
+
module.exports = Posterous;
View
@@ -1,6 +1,18 @@
var Posterous = require('../index');
var config = require('./config');
+
+function makeid()
+{
+ var text = "";
+ var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+
+ for( var i=0; i < 15; i++ )
+ text += possible.charAt(Math.floor(Math.random() * possible.length));
+
+ return text;
+}
+
module.exports['Listing Sites'] = function(test) {
var posterous = new Posterous(config.user, config.pass);
@@ -24,3 +36,33 @@ module.exports['Listing Sites'] = function(test) {
});
});
};
+
+module.exports['Create, update, retrieve and delete site'] = function(test) {
+ var posterous = new Posterous(config.user, config.pass);
+
+ console.log('Fetching api token...');
+ posterous.auth(function(err, result) {
+ if (err) {
+ test.ok(!err, 'Could not get api token, check your username/password in config/index.js');
+ }
+
+ console.log('Creating new site...');
+
+ //Create new private site with random subdomain and name
+ var options = {
+ 'site[hostname]': makeid(),
+ 'site[name]': makeid(),
+ 'site[is_private]': 'true'
+ };
+
+ posterous.createSite(options, function(err, result) {
+ test.ok(!err, 'An error occurred: ' + err);
+
+ test.ok(result, 'Could not create a new site');
+
+ console.log(result);
+
+ test.done();
+ });
+ });
+};

0 comments on commit 0dc6a72

Please sign in to comment.