Skip to content

Commit

Permalink
fixed spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
ahallock committed Dec 15, 2013
1 parent 5b0bf9c commit 08c8218
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 253 deletions.
86 changes: 43 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* Module dependencies.
*/
var Cache = require('./lib/cache')
, Queue = require('./lib/queue')
, task = require('./lib/task')
, CodePackage = require('./lib/code-package')
, querystring = require('querystring')
;
, Queue = require('./lib/queue')
, task = require('./lib/task')
, CodePackage = require('./lib/code-package')
, querystring = require('querystring')
;

/**
* Returns the projects function.
Expand All @@ -17,50 +17,50 @@ var Cache = require('./lib/cache')
* @api public
*/
function ironio(token, options) {
var api = require('./lib/api')(token, options);
var path = 'projects';
var api = require('./lib/api')(token, options);
var path = 'projects';

function projects(id) {
var projectPath = path + '/' + id;
function projects(id) {
var projectPath = path + '/' + id;

function createListFn(name) {
return function(params, fn) {
if (typeof params === 'function') {
fn = params;
params = null;
}
var servicePath = projectPath + '/' + name +
(params ? '?' + querystring.stringify(params) : '');
api.get(servicePath, fn);
};
}
function createListFn(name) {
return function(params, fn) {
if (typeof params === 'function') {
fn = params;
params = null;
}
var servicePath = projectPath + '/' + name +
(params ? '?' + querystring.stringify(params) : '');
api.get(servicePath, fn);
};
}

function createService(type, endpointNamespace) {
function service(id) {
return new type(projectPath + '/' + endpointNamespace + '/' + id, api);
}
service.list = createListFn(endpointNamespace);
return service;
}
function createService(type, endpointNamespace) {
function service(id) {
return new type(projectPath + '/' + endpointNamespace + '/' + id, api);
}
service.list = createListFn(endpointNamespace);
return service;
}

// `tasks` needs some specialization to make the API nicer
var tasks = task(projectPath + '/' + 'tasks', api);
tasks.list = createListFn('tasks');
tasks.scheduled.list = createListFn('schedules');
// `tasks` needs some specialization to make the API nicer
var tasks = task(projectPath + '/' + 'tasks', api);
tasks.list = createListFn('tasks');
tasks.scheduled.list = createListFn('schedules');

return {
caches: createService(Cache, 'caches')
, queues: createService(Queue, 'queues')
, codePackages: createService(CodePackage, 'codes')
, tasks: tasks
};
}
return {
caches: createService(Cache, 'caches')
, queues: createService(Queue, 'queues')
, codePackages: createService(CodePackage, 'codes')
, tasks: tasks
};
}

return {
projects: projects
};
return {
projects: projects
};
}

/*!
Expand Down
180 changes: 90 additions & 90 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
* Module dependencies.
*/
var request = require('request')
, fs = require('fs');
, fs = require('fs');

/**
* @constant
*/
var CLOUDS = {
aws: {
cache: 'cache-aws-us-east-1'
, queue: 'mq-aws-us-east-1'
, worker: 'worker-aws-us-east-1'
},
rackspace: {
queue: 'mq-rackspace-dfw'
}
aws: {
cache: 'cache-aws-us-east-1'
, queue: 'mq-aws-us-east-1'
, worker: 'worker-aws-us-east-1'
},
rackspace: {
queue: 'mq-rackspace-dfw'
}
};

/**
Expand All @@ -26,98 +26,98 @@ var CLOUDS = {
* @return {Object} api
*/
function api(token, options) {
options = options || {};
var cloud = options.cloud || 'aws';
options = options || {};
var cloud = options.cloud || 'aws';

function buildRequestOptions(endpoint, method, body) {
var service = 'queue';
if (/projects\/\w{24}\/caches/.test(endpoint)) {
service = 'cache';
} else if (/projects\/\w{24}\/(codes|tasks|schedules)/.test(endpoint)) {
service = 'worker';
}
var subdomain = CLOUDS[cloud][service];
var version = service === 'worker' ? '2' : '1';
var url = 'https://' + subdomain + '.iron.io/' +
version + '/' + endpoint;
var requestOptions = {
url: url
, json: true
, method: method
, headers: {
Authorization: 'OAuth ' + token
}
};
if (body) requestOptions.body = body;
return requestOptions;
}
function buildRequestOptions(endpoint, method, body) {
var service = 'queue';
if (/projects\/\w{24}\/caches/.test(endpoint)) {
service = 'cache';
} else if (/projects\/\w{24}\/(codes|tasks|schedules)/.test(endpoint)) {
service = 'worker';
}
var subdomain = CLOUDS[cloud][service];
var version = service === 'worker' ? '2' : '1';
var url = 'https://' + subdomain + '.iron.io/' +
version + '/' + endpoint;
var requestOptions = {
url: url
, json: true
, method: method
, headers: {
Authorization: 'OAuth ' + token
}
};
if (body) requestOptions.body = body;
return requestOptions;
}

function checkErr(res) {
if (res.statusCode !== 200) {
return new Error('Response code: ' + res.statusCode);
}
return null;
}
function checkErr(res) {
if (res.statusCode !== 200) {
return new Error('Response code: ' + res.statusCode);
}
return null;
}

function exec(endpoint, method, body, fn) {
var options = buildRequestOptions(endpoint, method, body);
request(options, function(err, res, body) {
if (err) return fn(err);
fn(checkErr(res), body);
});
}
function get(endpoint, fn) {
exec(endpoint, 'GET', null, fn);
}
function exec(endpoint, method, body, fn) {
var options = buildRequestOptions(endpoint, method, body);
request(options, function(err, res, body) {
if (err) return fn(err);
fn(checkErr(res), body);
});
}
function get(endpoint, fn) {
exec(endpoint, 'GET', null, fn);
}

function post(endpoint, body, fn) {
exec(endpoint, 'POST', body, fn);
}
function put(endpoint, body, fn) {
exec(endpoint, 'PUT', body, fn);
}
function post(endpoint, body, fn) {
exec(endpoint, 'POST', body, fn);
}
function put(endpoint, body, fn) {
exec(endpoint, 'PUT', body, fn);
}

function del(endpoint, body, fn) {
if (typeof body === 'function') {
fn = body;
body = null;
}
exec(endpoint, 'DELETE', body, fn);
}
function del(endpoint, body, fn) {
if (typeof body === 'function') {
fn = body;
body = null;
}
exec(endpoint, 'DELETE', body, fn);
}

function download(endpoint, fn) {
var options = buildRequestOptions(endpoint, 'GET');
var req = request(options);
var filename;
function download(endpoint, fn) {
var options = buildRequestOptions(endpoint, 'GET');
var req = request(options);
var filename;

req.on('response', function(res){
var err = checkErr(res);
if (err) throw err;
req.on('response', function(res){
var err = checkErr(res);
if (err) throw err;

filename = res.headers['content-disposition'].split('=')[1];
res.pipe(fs.createWriteStream('./' + filename));
});
filename = res.headers['content-disposition'].split('=')[1];
res.pipe(fs.createWriteStream('./' + filename));
});

req.on('end', function() {
fn(null, filename);
});
req.on('end', function() {
fn(null, filename);
});

req.on('error', function(err) {
fn(err);
});
}
req.on('error', function(err) {
fn(err);
});
}

return {
get: get
, post: post
, put: put
, del: del
, download: download
};
return {
get: get
, post: post
, put: put
, del: del
, download: download
};
}

/*!
Expand Down
14 changes: 7 additions & 7 deletions lib/code-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ CodePackage.prototype.revisions = function(fn) {
};

CodePackage.prototype.download = function(revision, fn) {
if (typeof revision === 'function') {
fn = revision;
revision = false;
}
if (typeof revision === 'function') {
fn = revision;
revision = false;
}

var downloadPath = this.path + '/download' +
(revision ? '?revision=' + revision : '');
this.api.download(downloadPath, fn);
var downloadPath = this.path + '/download' +
(revision ? '?revision=' + revision : '');
this.api.download(downloadPath, fn);
}

/*!
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-ironio",
"version": "0.4.0",
"version": "0.4.1",
"description": "iron.io client",
"main": "index.js",
"author": "Andrew Hallock <andrew@blazelocal.com>",
Expand Down
Loading

0 comments on commit 08c8218

Please sign in to comment.