Permalink
Browse files

[minor] Refactor helpers.defaultUser into Client.

Since everything inherits Client, there's no point in having to require
helpers and then use `defaultUsers.call(this, appName)` and such. Binding to
Client allows `this.defaultUsers(appName)` to be called instead.
  • Loading branch information...
Southern committed Jul 29, 2013
1 parent 270a4c5 commit 86108c08aafec23e54577d03fdf7545e36ebf154
@@ -8,8 +8,7 @@
*/

var util = require('util'),
Client = require('./client').Client,
defaultUser = require('./helpers').defaultUser;
Client = require('./client').Client;

//
// ### function Apps (options)
@@ -62,7 +61,7 @@ Apps.prototype.list = function (username, callback) {
// Creates an application with the specified package.json manifest in `app`.
//
Apps.prototype.create = function (app, callback) {
var appName = defaultUser.call(this, app.name);
var appName = this.defaultUser(app.name);

this.request({ method: 'POST', uri: ['apps', appName], body: app }, callback);
};
@@ -74,8 +73,8 @@ Apps.prototype.create = function (app, callback) {
// Views the application specified by `name`.
//
Apps.prototype.view = function (appName, callback) {
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/')),
var appName = this.defaultUser(appName),
argv = ['apps'].concat(appName.split('/')),
self = this;

this.request({ uri: argv }, function (err, result) {
@@ -101,8 +100,8 @@ Apps.prototype.view = function (appName, callback) {
// Updates the application with `name` with the specified attributes in `attrs`
//
Apps.prototype.update = function (appName, attrs, callback) {
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/'));
var appName = this.defaultUser(appName),
argv = ['apps'].concat(appName.split('/'));

this.request({ method: 'PUT', uri: argv, body: attrs }, callback);
};
@@ -114,8 +113,8 @@ Apps.prototype.update = function (appName, attrs, callback) {
// Destroys the application with `name` for the authenticated user.
//
Apps.prototype.destroy = function (appName, callback) {
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/'));
var appName = this.defaultUser(appName),
argv = ['apps'].concat(appName.split('/'));

this.request({ method: 'DELETE', uri: argv, appName: appName }, callback);
};
@@ -133,8 +132,8 @@ Apps.prototype.start = function (appName, cloud, callback) {
cloud = null;
}

appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/')).concat('start');
var appName = this.defaultUser(appName),
argv = ['apps'].concat(appName.split('/')).concat('start');

if (cloud) {
this.clouds[appName] = !Array.isArray(cloud)
@@ -152,8 +151,8 @@ Apps.prototype.start = function (appName, cloud, callback) {
// Starts the application with `name` for the authenticated user.
//
Apps.prototype.restart = function (appName, callback) {
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/')).concat('restart');
var appName = this.defaultUser(appName),
argv = ['apps'].concat(appName.split('/')).concat('restart');

this.cloud({ method: 'POST', uri: argv, appName: appName }, this.request, callback);
};
@@ -165,8 +164,8 @@ Apps.prototype.restart = function (appName, callback) {
// Stops the application with `name` for the authenticated user.
//
Apps.prototype.stop = function (appName, callback) {
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/')).concat('stop');
var appName = this.defaultUser(appName),
argv = ['apps'].concat(appName.split('/')).concat('stop');

this.cloud({ method: 'POST', uri: argv, appName: appName }, this.request, callback);
};
@@ -179,7 +178,7 @@ Apps.prototype.stop = function (appName, callback) {
// in the current Nodejitsu environment.
//
Apps.prototype.available = function (app, callback) {
var appName = defaultUser.call(this, app.name),
var appName = this.defaultUser(app.name),
argv = ['apps'].concat(appName.split('/')).concat('available');

this.request({ method: 'POST', uri: argv, body: app }, callback);
@@ -193,8 +192,8 @@ Apps.prototype.available = function (app, callback) {
// Runs `app` on `drones` drones.
//
Apps.prototype.setDrones = function (appName, drones, callback) {
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/')).concat('cloud'),
var appName = this.defaultUser(appName),
argv = ['apps'].concat(appName.split('/')).concat('cloud'),
cloud = [{ drones: drones }];

this.cloud({ method: 'POST', uri: argv, body: cloud, appName: appName }, this.request, callback);
@@ -208,8 +207,8 @@ Apps.prototype.setDrones = function (appName, drones, callback) {
// Deploy the given application in a new datacenter.
//
Apps.prototype.datacenter = function (appName, cloud, callback) {
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/')).concat('cloud'),
var appName = this.defaultUser(appName),
argv = ['apps'].concat(appName.split('/')).concat('cloud'),
self = this;

if (!Array.isArray(cloud)) cloud = [cloud];
@@ -34,6 +34,22 @@ var Client = exports.Client = function (options) {

util.inherits(Client, EventEmitter);

//
// ### function defaultUser (appName)
// #### @data {String} App name, user/app, or user/database.
//
// A helper to prepend a default username.
// needs 'this' to be able to options.get('username').
//
Client.prototype.defaultUser = function (data) {
if (!~data.indexOf('/')) {
data = this.options.get('username') + '/' + data;
}

return data;
};


//
// ### function endpoints(callback)
// #### @callback {function} Continuation to respond to when complete.
@@ -8,7 +8,6 @@
*/

var util = require('util'),
defaultUser = require('./helpers').defaultUser,
Client = require('./client').Client;

//
@@ -32,8 +31,8 @@ util.inherits(Databases, Client);
// Provisions a database for the user
//
Databases.prototype.create = function (databaseType, databaseName, callback) {
databaseName = defaultUser.call(this, databaseName);
var argv = [ 'databases' ].concat(databaseName.split('/'));
var databaseName = this.defaultUser(databaseName),
argv = [ 'databases' ].concat(databaseName.split('/'));

this.request({ method: 'POST', uri: argv, body: { type: databaseType }}, function (err, result, res) {
if (err) return callback(err);
@@ -49,8 +48,8 @@ Databases.prototype.create = function (databaseType, databaseName, callback) {
// Gets the metadata for the specified database
//
Databases.prototype.get = function (databaseName, callback) {
databaseName = defaultUser.call(this, databaseName);
var argv = [ 'databases' ].concat(databaseName.split('/'));
var databaseName = this.defaultUser(databaseName),
argv = [ 'databases' ].concat(databaseName.split('/'));

this.request({ uri: argv }, function (err, result) {
if (err) return callback(err);
@@ -85,8 +84,8 @@ Databases.prototype.list = function (username, callback) {
// Deprovisions specified database
//
Databases.prototype.destroy = function (databaseName, callback) {
databaseName = defaultUser.call(this, databaseName);
var argv = [ 'databases' ].concat(databaseName.split('/'));
var databaseName = this.defaultUser(databaseName),
argv = [ 'databases' ].concat(databaseName.split('/'));

this.request({ method: 'DELETE', uri: argv }, callback);
};
@@ -1,20 +1,5 @@
'use strict';

//
// ### function defaultUser (appName)
// #### @data {String} App name, user/app, or user/database.
//
// A helper to prepend a default username.
// needs 'this' to be able to options.get('username').
//
exports.defaultUser = function (data) {
if (!~data.indexOf('/')) {
data = this.options.get('username') + '/' + data;
}

return data;
};

//
// Async flow control helper function, to clean up some messy code.
//
@@ -8,8 +8,7 @@
*/

var util = require('util'),
Client = require('./client').Client,
defaultUser = require('./helpers').defaultUser;
Client = require('./client').Client;

//
// ### function Keys (options)
@@ -8,8 +8,7 @@
*/

var util = require('util'),
Client = require('./client').Client,
defaultUser = require('./helpers').defaultUser;
Client = require('./client').Client;

//
// ### function Logs (options)
@@ -32,8 +31,8 @@ util.inherits(Logs, Client);
// It retrieves the specified amount of logs for the application
//
Logs.prototype.byApp = function (appName, amount, callback) {
appName = defaultUser.call(this, appName);
var argv = ['logs'].concat(appName.split('/')),
var appName = this.defaultUser(appName),
argv = ['logs'].concat(appName.split('/')),
options = {
from: 'NOW-1DAY',
until: 'NOW',
@@ -8,8 +8,7 @@
*/

var util = require('util'),
Client = require('./client').Client,
defaultUser = require('./helpers').defaultUser;
Client = require('./client').Client;

//
// ### function Snapshots (options)
@@ -31,8 +30,8 @@ util.inherits(Snapshots, Client);
// Lists all applications for the authenticated user
//
Snapshots.prototype.list = function (appName, callback) {
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/')).concat('snapshots');
var appName = this.defaultUser(appName),
argv = ['apps'].concat(appName.split('/')).concat('snapshots');

this.request({ uri: argv }, function (err, result) {
if (err) return callback(err);
@@ -51,8 +50,8 @@ Snapshots.prototype.list = function (appName, callback) {
// `app.name = name` using the `*.tgz` package data in `filename` file.
//
Snapshots.prototype.create = function (appName, snapshotName, filename, callback) {
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/')).concat(['snapshots', snapshotName]);
var appName = this.defaultUser(appName),
argv = ['apps'].concat(appName.split('/')).concat(['snapshots', snapshotName]);

return this.upload({ uri: argv, file: filename }, callback);
};
@@ -66,8 +65,8 @@ Snapshots.prototype.create = function (appName, snapshotName, filename, callback
// `snapshot.id === snapshotName`.
//
Snapshots.prototype.fetch = function (appName, snapshotName, callback) {
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/')).concat(['snapshots', snapshotName + '.tgz']);
var appName = this.defaultUser(appName),
argv = ['apps'].concat(appName.split('/')).concat(['snapshots', snapshotName + '.tgz']);

callback = callback || function () {};
return this.request({ uri: argv }, callback);
@@ -82,8 +81,8 @@ Snapshots.prototype.fetch = function (appName, snapshotName, callback) {
// `snapshot.id === snapshotName`.
//
Snapshots.prototype.destroy = function (appName, snapshotName, callback) {
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/')).concat(['snapshots', snapshotName]);
var appName = this.defaultUser(appName),
argv = ['apps'].concat(appName.split('/')).concat(['snapshots', snapshotName]);

this.request({ method: 'DELETE', uri: argv }, callback);
};
@@ -97,8 +96,8 @@ Snapshots.prototype.destroy = function (appName, snapshotName, callback) {
// `snapshot.id === snapshotName`.
//
Snapshots.prototype.activate = function (appName, snapshotName, callback) {
appName = defaultUser.call(this, appName);
var argv = ['apps'].concat(appName.split('/')).concat(['snapshots', snapshotName, 'activate']);
var appName = this.defaultUser(appName),
argv = ['apps'].concat(appName.split('/')).concat(['snapshots', snapshotName, 'activate']);

this.request({ method: 'POST', uri: argv }, callback);
};
@@ -8,8 +8,7 @@
*/

var util = require('util'),
Client = require('./client').Client,
defaultUser = require('./helpers').defaultUser;
Client = require('./client').Client;

//
// ### function Tokens (options)
@@ -8,8 +8,7 @@
*/

var util = require('util'),
Client = require('./client').Client,
defaultUser = require('./helpers').defaultUser;
Client = require('./client').Client;

//
// ### function Users (options)

0 comments on commit 86108c0

Please sign in to comment.