Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor to use a nifty http.request helper.

And some other fixes.
  • Loading branch information...
commit 05c4546ce4cb22d3664a4668fe8ee5fa19288d8c 1 parent ab2563c
@mythmon mythmon authored
Showing with 63 additions and 79 deletions.
  1. +7 −75 api.js
  2. +5 −4 standup-irc.js
  3. +51 −0 utils.js
View
82 api.js
@@ -1,7 +1,4 @@
/* Functions to access the remote web api of a standup app. */
-var http = require('http');
-var events = require('events');
-
var utils = require('./utils');
exports.status = {
@@ -11,92 +8,27 @@ exports.status = {
* - `content`: The text of the status.
*/
create: function(user, project, content) {
- var body = JSON.stringify({
+ if (project.charAt(0) === '#') {
+ project = project.slice(1);
+ }
+ var data = {
user: utils.canonicalUsername(user),
project: project,
content: content,
api_key: CONFIG.standup.api_key
- });
- var options = {
- host: CONFIG.standup.host,
- port: CONFIG.standup.port,
- path: '/api/v1/status',
- method: 'POST',
- headers: {
- 'content-type': 'application/json',
- 'content-length': body.length
- }
};
-
- var emitter = new events.EventEmitter();
- // Make the request
- var req = http.request(options, function(res) {
- var resp_data = "";
- // Read data as it comes in
- res.on('data', function(chunk) {
- resp_data += chunk;
- });
- // When we have received the entire response
- res.on('end', function() {
- var json = JSON.parse(resp_data);
- if (res.statusCode === 200) {
- emitter.emit('ok', json);
- } else {
- emitter.emit('error', json);
- }
- });
- });
- req.on('error', function(e) {
- emitter.emit('error', String(e));
- });
- req.end(body);
-
- return emitter;
+ return utils.request('/api/v1/status/', 'POST', data);
},
/* Delete a status.
* - `id`: The id of the status to delete.
*/
delete_: function(id, user) {
- var body = JSON.stringify({
+ var data = {
user: utils.canonicalUsername(user),
api_key: CONFIG.standup.api_key
- });
- var options = {
- host: CONFIG.standup.host,
- port: CONFIG.standup.port,
- path: '/api/v1/status/' + id,
- method: 'DELETE',
- headers: {
- 'content-type': 'application/json',
- 'content-length': body.length
- }
};
-
- var emitter = new events.EventEmitter();
- // Make the request
- var req = http.request(options, function(res) {
- var resp_data = "";
- // Read data as it comes in
- res.on('data', function(chunk) {
- resp_data += chunk;
- });
- // When we have received the entire response
- res.on('end', function() {
- var json = JSON.parse(resp_data);
- if (res.statusCode === 200) {
- emitter.emit('ok', json);
- } else {
- emitter.emit('error', res.statusCode, json);
- }
- });
- });
- req.end(body);
- req.on('error', function(e) {
- emitter.emit('error', String(e));
- });
-
- return emitter;
+ return utils.request('/api/v1/status/' + id, 'DELETE', data);
}
};
View
9 standup-irc.js
@@ -128,17 +128,18 @@ var commands = {
/* Create a status. */
status: function(user, channel, message, args) {
- var project = args[1];
+ var project = args[0];
if (project.charAt(0) == '#') {
project = project.slice(1);
}
- var ret = api.status.create(user, channel, args);
+ var ret = api.status.create(user, project, args.slice(1).join(' '));
ret.on('ok', function(data) {
client.say(channel, 'Ok, submitted status #' + data.id);
});
- ret.on('error', function(data) {
+ ret.on('error', function(err, data) {
client.say(channel, 'Uh oh, something went wrong.');
- logger.error('Problem adding status: ' + JSON.stringify(data));
+ logger.error('Problem adding status: ' + err + ' ' +
+ JSON.stringify(data));
});
},
View
51 utils.js
@@ -1,4 +1,7 @@
var _ = require('underscore');
+var http = require('http');
+var events = require('events');
+
/* Find a user's canonical username based on `canonicalNicks`.
*
@@ -22,6 +25,7 @@ this.canonicalUsername = function(ircNick, canonicalNicks) {
return matches[0];
};
+
// Check if `a` is a prefix of `b`.
this.isPrefix = function(a, b) {
var i;
@@ -35,3 +39,50 @@ this.isPrefix = function(a, b) {
}
return true;
};
+
+
+this.request = function(path, method, data, emitter) {
+ if (data === undefined) {
+ data = {};
+ }
+ var body = JSON.stringify(data);
+ var options = {
+ host: CONFIG.standup.host,
+ port: CONFIG.standup.port,
+ path: path,
+ method: method,
+ headers: {
+ 'content-type': 'application/json',
+ 'content-length': body.length
+ }
+ };
+
+ if (emitter === undefined) {
+ emitter = new events.EventEmitter();
+ }
+ // Make the request
+ var req = http.request(options, function(res) {
+ var resp_data = "";
+ // Read data as it comes in
+ res.on('data', function(chunk) {
+ resp_data += chunk;
+ });
+ // When we have received the entire response
+ res.on('end', function() {
+ if (res.statusCode === 200) {
+ var json = JSON.parse(resp_data);
+ emitter.emit('ok', json);
+ } else if (res.statusCode === 301) {
+ this.request(res.headers.Location, method, data, emitter);
+ } else {
+ emitter.emit('error', res.statusCode, resp_data);
+ }
+ });
+ });
+ req.end(body);
+ req.on('error', function(e) {
+ emitter.emit('error', String(e));
+ });
+
+ return emitter;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.