Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

use mikeal/request for easier http interactions, closes #1

These tests are not great, need to stub http requests ideally.
  • Loading branch information...
commit 6170a9caa52e4827687f0cec04121fe99874e497 1 parent c96d107
@chrismytton authored
View
91 lib/index.js
@@ -1,4 +1,4 @@
-var Bot, Campfire, Cli, EventEmitter, Xmpp, http;
+var Bot, Campfire, Cli, EventEmitter, Xmpp, http, request;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
@@ -8,6 +8,7 @@ var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments)
return child;
}, __slice = Array.prototype.slice;
EventEmitter = require('events').EventEmitter;
+request = require('request');
http = require('http');
exports.createBot = function(name) {
return new Bot(name);
@@ -75,11 +76,11 @@ exports.Bot = Bot = (function() {
this.descriptions = [];
return typeof callback === "function" ? callback() : void 0;
};
- Bot.prototype.get = function(path, body, callback) {
- return this.request('GET', path, body, callback);
+ Bot.prototype.get = function(uri, body, callback) {
+ return this.request('GET', uri, body, callback);
};
- Bot.prototype.post = function(path, body, callback) {
- return this.request('POST', path, body, callback);
+ Bot.prototype.post = function(uri, body, callback) {
+ return this.request('POST', uri, body, callback);
};
Bot.prototype.help = function(message) {
if (Object.keys(this.descriptions).length === 0) {
@@ -101,62 +102,32 @@ exports.Bot = Bot = (function() {
return _results;
}, this));
};
- Bot.prototype.request = function(method, path, body, callback) {
- var client, headers, match, port, req;
- if (match = path.match(/^(https?):\/\/([^\/]+?)(\/.*)/)) {
- headers = {
- Host: match[2],
- 'Content-Type': 'application/json',
- 'User-Agent': this.name
- };
- port = match[1] === 'https' ? 443 : 80;
- client = http.createClient(port, match[2], port === 443);
- path = match[3];
- if (typeof body === 'function' && !callback) {
- callback = body;
- body = null;
- }
- if (method === 'POST' && body) {
- if (typeof body !== 'string') {
- body = JSON.stringify(body);
- }
- headers['Content-Length'] = body.length;
- }
- req = client.request(method, path, headers);
- req.on('response', function(response) {
- var data;
- if (response.statusCode === 200) {
- data = '';
- response.setEncoding('utf8');
- response.on('data', function(chunk) {
- return data += chunk;
- });
- return response.on('end', function() {
- if (callback) {
- try {
- body = JSON.parse(data);
- } catch (e) {
- body = data;
- }
- return callback(body);
- }
- });
- } else if (response.statusCode === 302) {
- return request(method, path, body, callback);
- } else {
- console.log("" + response.statusCode + ": " + path);
- response.setEncoding('utf8');
- response.on('data', function(chunk) {
- return console.log(chunk.toString());
- });
- return process.exit(1);
- }
- });
- if (method === 'POST' && body) {
- req.write(body);
- }
- return req.end();
+ Bot.prototype.request = function(method, uri, body, callback) {
+ var options;
+ options = {
+ method: method,
+ uri: uri
+ };
+ options.headers = {
+ 'User-Agent': this.name
+ };
+ if (typeof body === 'function' && !callback) {
+ callback = body;
+ body = null;
}
+ if (typeof body === 'string') {
+ options.body = body;
+ } else {
+ options.json = body;
+ }
+ return request(options, function(err, response, body) {
+ try {
+ body = JSON.parse(body);
+ } catch (e) {
+
+ }
+ return typeof callback === "function" ? callback(body, response) : void 0;
+ });
};
return Bot;
})();
View
3  package.json
@@ -19,7 +19,8 @@
},
"dependencies": {
"node-xmpp": "0.2.9",
- "ranger": "0.2.4"
+ "ranger": "0.2.4",
+ "request": "1.9.8"
},
"devDependencies": {
"coffee-script": "1.1",
View
70 src/index.coffee
@@ -1,6 +1,7 @@
# The main `Bot` class that all robots are created from, this contains the
# main functions for assembling a bot.
{EventEmitter} = require 'events'
+request = require 'request'
http = require 'http'
# Factory method for creating a new `Bot` instance. Accepts a name
@@ -112,13 +113,13 @@ exports.Bot = class Bot extends EventEmitter
# Helper method for making a `GET` request, proxies to the `request`
# method.
- get: (path, body, callback) ->
- @request('GET', path, body, callback)
+ get: (uri, body, callback) ->
+ @request('GET', uri, body, callback)
# Helper method for making a `POST` request, proxies to the `request`
# method.
- post: (path, body, callback) ->
- @request('POST', path, body, callback)
+ post: (uri, body, callback) ->
+ @request('POST', uri, body, callback)
# Handler for the default help action, gathers all of the registered
# descriptions and sends a message describing each action.
@@ -135,46 +136,27 @@ exports.Bot = class Bot extends EventEmitter
# Helper to make http requests, tries to automatically handle JSON input and
# output.
- request: (method, path, body, callback) ->
- if match = path.match(/^(https?):\/\/([^\/]+?)(\/.*)/)
- headers = { Host: match[2], 'Content-Type': 'application/json', 'User-Agent': @name }
- port = if match[1] == 'https' then 443 else 80
- client = http.createClient(port, match[2], port == 443)
- path = match[3]
-
- if typeof(body) is 'function' and not callback
- callback = body
- body = null
-
- if method is 'POST' and body
- body = JSON.stringify body if typeof body isnt 'string'
- headers['Content-Length'] = body.length
-
- req = client.request(method, path, headers)
-
- req.on 'response', (response) ->
- if response.statusCode is 200
- data = ''
- response.setEncoding('utf8')
- response.on 'data', (chunk) ->
- data += chunk
- response.on 'end', ->
- if callback
- try
- body = JSON.parse(data)
- catch e
- body = data
- callback body
- else if response.statusCode is 302
- request(method, path, body, callback)
- else
- console.log "#{response.statusCode}: #{path}"
- response.setEncoding('utf8')
- response.on 'data', (chunk) ->
- console.log chunk.toString()
- process.exit(1)
- req.write(body) if method is 'POST' and body
- req.end()
+ request: (method, uri, body, callback) ->
+ options = { method: method, uri: uri }
+
+ options.headers = { 'User-Agent': @name }
+
+ if typeof(body) is 'function' and not callback
+ callback = body
+ body = null
+
+ if typeof body is 'string'
+ options.body = body
+ else
+ options.json = body
+
+ request options, (err, response, body) ->
+ try
+ body = JSON.parse body
+ catch e
+ # Ignore and pass through the raw body.
+
+ callback? body, response
# Command line interface.
Cli = require './interfaces/cli'
View
5 test/bots.test.coffee
@@ -58,3 +58,8 @@ module.exports = testCase
test.done()
robot.start()
+
+ 'test making requests': (test) ->
+ robot.get 'http://hecticjeff.net/', (body) ->
+ test.ok body.match(/hecticjeff/i)
+ test.done()
Please sign in to comment.
Something went wrong with that request. Please try again.