Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
joeblynch committed Jul 30, 2012
1 parent efabaf4 commit f12322a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 76 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
.idea
node_modules
.DS_Store
8 changes: 4 additions & 4 deletions lib/api-client.js
Expand Up @@ -4,19 +4,19 @@ var https = require('https'),
var ApiClient = module.exports = Compose(function(opts) {
opts || (opts = {});

this._authId || (this._authId = opts.authID || ApiClient.authID);
this._authToken || (this._authToken = opts.authToken || ApiClient.authToken);
this.authId = opts.authID || ApiClient.authID;
this.authToken = opts.authToken || ApiClient.authToken;
}, {
API_VERSION: '1',

sendRaw: function(method, resources, params, cb) {
var req = https.request({
method: 'POST',
path: ['/v', this.API_VERSION, '/Account/' + this._authId, '/' ,
path: ['/v', this.API_VERSION, '/Account/' + this.authId, '/' ,
Array.isArray(resources) ? resources.join('/') : resources, '/'].join(''),
hostname: 'api.plivo.com',
headers: { 'Content-Type': 'application/json' },
auth: this._authId + ':' + this._authToken
auth: this.authId + ':' + this.authToken
}, function(res) {
cb && cb(null, res);
}).on('error', function(err) {
Expand Down
19 changes: 9 additions & 10 deletions lib/app.js
Expand Up @@ -10,10 +10,10 @@ var express = require('express'),
var App = module.exports = Compose(EventEmitter, ApiClient, function(opts) {
var self = this;

opts.authID && (this._authID = opts.authID);
opts._authToken && (this._authToken = opts.authToken);
this._appID = opts.appID;
this._rootUrl = typeof opts.rootUrl === 'string'
opts.authID && (this.authID = opts.authID);
opts.authToken && (this.authToken = opts.authToken);
this.appID = opts.appID;
this.rootUrl = typeof opts.rootUrl === 'string'
? url.parse(opts.rootUrl.match(/\/$/) ? opts.rootUrl : opts.rootUrl + '/', false, true)
: opts.rootUrl;

Expand All @@ -27,7 +27,7 @@ var App = module.exports = Compose(EventEmitter, ApiClient, function(opts) {
app.use(app.router);
});

var port = self._rootUrl.port || (self._rootUrl.protocol === 'https:' ? 443 : 80);
var port = self.rootUrl.port || (self.rootUrl.protocol === 'https:' ? 443 : 80);
app.listen(port);

console.log('listening on port ' + port);
Expand Down Expand Up @@ -58,7 +58,7 @@ var App = module.exports = Compose(EventEmitter, ApiClient, function(opts) {
}

function _updateEventAppUrl(event) {
var rootUrl = self._rootUrl.href;
var rootUrl = self.rootUrl.href;

if (!_pendingAppUrls) {
_pendingAppUrls = {};
Expand All @@ -79,16 +79,15 @@ var App = module.exports = Compose(EventEmitter, ApiClient, function(opts) {
_updateEventAppUrl(event);
}
});


}, {
call: function(fromNumber, toNumber, callback) {
var callbackUrl = self._rootUrl.href + callback;
// TODO: need generic way to support both strings for callback routes, as well as fns for apps that don't need to scale.
var callbackUrl = self.rootUrl.href + callback;
ApiClient.prototype.call.call(this, fromNumber, toNumber, callbackUrl);
},

updateApp: function(urls, cb) {
ApiClient.prototype.updateApp.call(this, this._appID, urls, cb);
ApiClient.prototype.updateApp.call(this, this.appID, urls, cb);
return this;
},

Expand Down
126 changes: 64 additions & 62 deletions lib/response.js
@@ -1,79 +1,81 @@
var Compose = require('compose');

(function() {
var _add = function(res, type, attrs, body) {
var buf = res._buf;
function _add(res, type, attrs, body) {
var buf = res._buf;

buf.push('<', type);
buf.push('<', type);

if (attrs) {
for (var key in attrs) {
if (attrs.hasOwnProperty(key)) {
buf.push(' ', key, '="', attrs[key], '"');
}
if (attrs) {
for (var key in attrs) {
if (attrs.hasOwnProperty(key)) {
buf.push(' ', key, '="', attrs[key], '"');
}
}
}

if (body) {
buf.push('>');
buf.push(typeof body === 'function' ? body(res) : body);
buf.push('</', type, '>');
} else {
buf.push(' />');
}
if (body) {
buf.push('>');
buf.push(typeof body === 'function' ? body(res) : body);
buf.push('</', type, '>');
} else {
buf.push(' />');
}
}

var Response = module.exports = Compose(function(res, app) {
this._app = app;
this._buf = [];
this._res = res;
}, {
send: function(res) {
res || (res = this._res);
res.header('Content-Type', 'text/xml');
res.send('<Response>' + this._buf.join('') + '</Response>');
return this;
},
var Response = module.exports = Compose(function(res, app) {
this._app = app;
this._res = res;
this._buf = [];
}, {
toString: function() {

dtmf: function(digits) {
_add(this, 'DTMF', null, digits);
return this;
},
},

getDigits: function(opts, body) {
if (this._app && !opts.action.match(/\/\//)) {
opts.action = this._app._rootUrl.href + opts.action;
}
send: function(res) {
res || (res = this._res);
res.header('Content-Type', 'text/xml');
res.send(['<Response>', this._buf.join(''), '</Response>'].join(''));
return this;
},

dtmf: function(digits) {
_add(this, 'DTMF', null, digits);
return this;
},

_add(this, 'GetDigits', opts, body);
return this;
},
getDigits: function(opts, body) {
if (this._app && !opts.action.match(/\/\//)) {
opts.action = this._app.rootUrl.href + opts.action;
}

dialNumber: function(number, dialOpts, numberOpts) {
_add(this, 'Dial', dialOpts, function() {
_add(this, 'Number', numberOpts, number);
});
return this;
},
_add(this, 'GetDigits', opts, body);
return this;
},

hangup: function(opts) {
_add(this, 'Hangup', opts);
return this;
},
dialNumber: function(number, dialOpts, numberOpts) {
_add(this, 'Dial', dialOpts, function() {
_add(this, 'Number', numberOpts, number);
});
return this;
},

message: function(src, dest, msg) {
_add(this, 'Message', { src: src, dest: dest }, msg);
return this;
},
hangup: function(opts) {
_add(this, 'Hangup', opts);
return this;
},

play: function(url, opts) {
_add(this, 'Play', opts, url);
return this;
},
message: function(src, dest, msg) {
_add(this, 'Message', { src: src, dest: dest }, msg);
return this;
},

wait: function(length) {
_add(this, 'Wait', length && { length: length });
return this;
}
});
})();
play: function(url, opts) {
_add(this, 'Play', opts, url);
return this;
},

wait: function(length) {
_add(this, 'Wait', length && { length: length });
return this;
}
});

0 comments on commit f12322a

Please sign in to comment.