summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-05-04 16:57:31 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-05-04 16:57:31 +0200
commit3a7e65ca638bfcd615dcd24d4ee800596621be8b (patch)
tree76121f6812dbe0ae4867477828c570e330f561de
parenta55635dd4da622a82a091b13c533388e0d8a28cb (diff)
downloadbitraf-bot-3a7e65ca638bfcd615dcd24d4ee800596621be8b.tar.gz
bitraf-bot-3a7e65ca638bfcd615dcd24d4ee800596621be8b.tar.bz2
bitraf-bot-3a7e65ca638bfcd615dcd24d4ee800596621be8b.tar.xz
bitraf-bot-3a7e65ca638bfcd615dcd24d4ee800596621be8b.zip
o Adding a small module to process application/vnd.collection+json documents.
-rw-r--r--.gitignore4
-rw-r--r--lib/BitrafBot.js47
-rw-r--r--main.js2
-rw-r--r--node_modules/collection_json/index.js40
-rw-r--r--node_modules/collection_json/package.json12
-rw-r--r--node_modules/collection_json/test/collection_json.test.js39
-rw-r--r--test/checkins.test.js27
7 files changed, 144 insertions, 27 deletions
diff --git a/.gitignore b/.gitignore
index b2ce437..ba487e6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
.*.un~
*.swp
-node_modules/
+/node_modules/*
+!/node_modules/collection_json
+/node_modules/collection_json/node_modules
diff --git a/lib/BitrafBot.js b/lib/BitrafBot.js
index 9388ddb..92bb8d9 100644
--- a/lib/BitrafBot.js
+++ b/lib/BitrafBot.js
@@ -3,9 +3,27 @@ var IRC = require('../node_modules/node-irc/IRC.js').IRC
, irc = new IRC('irc.freenode.net', 6667)
, url = require('url')
, http = require('http')
+ , collection_json = require('collection_json')
, _ = require('underscore');
-var BitrafBot = function(config) {
+function onCheckinsEnd(chunks) {
+ var collection = collection_json.fromString(chunks);
+// console.log("collection.items", collection.items);
+ var checkins = -1, lastCheckin;
+ collection.mapItemData(function(data) {
+ checkins = parseInt(data["checkins"]);
+ lastCheckin = data["last-checkin"];
+ });
+ if(checkins != -1) {
+ return 'checkins: ' + checkins + ', last checkin: ' + (typeof lastCheckin === 'undefined' ? 'unknown' : lastCheckin);
+ }
+ else {
+ return 'Could not find any checkins';
+ }
+}
+module.exports.onCheckinsEnd = onCheckinsEnd;
+
+function BitrafBot(config) {
var nick = '';
var nickCount = 0;
var nickAdditions = [ '', '^', '-', '_', '\\', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ];
@@ -39,28 +57,8 @@ var BitrafBot = function(config) {
chunks += chunk;
});
res.on('end', function () {
- try {
- data = JSON.parse(chunks);
- var checkins = -1;
- var lastCheckin = 'unknown';
- _.each(data['collection'].items, function(item, i) {
- _.each(item.data, function(item, i) {
- switch(item.name) {
- case 'checkins': checkins = parseInt(item.value); break;
- case 'last-checkin': lastCheckin = item.value; break;
- }
- });
- });
- if(checkins != -1) {
- irc.privmsg(to, 'checkins: ' + checkins + ', last checkin: ' + lastCheckin);
- }
- else {
- irc.privmsg(to, 'Could not find any checkins');
- }
- }
- catch(ex) {
- irc.privmsg(to, 'Could not parse document');
- }
+ var s = onCheckinsEnd(chunks);
+ irc.privmsg(to, s);
});
}).on('error', function(e) {
irc.privmsg(to, 'Problem with request: ' + e.message);
@@ -85,5 +83,4 @@ var BitrafBot = function(config) {
irc.quit('bye');
});
}
-
-module.exports = BitrafBot;
+module.exports.BitrafBot = BitrafBot;
diff --git a/main.js b/main.js
index 105543d..6f6e620 100644
--- a/main.js
+++ b/main.js
@@ -25,7 +25,7 @@ try {
config = _.defaults(config, defaults);
console.log("configuration: ", config);
- var bot = require('./lib/BitrafBot.js')(config);
+ var bot = require('./lib/BitrafBot.js').BitrafBot(config);
var r = repl.start('irc> ');
r.context.bot = bot;
r.context.config = config;
diff --git a/node_modules/collection_json/index.js b/node_modules/collection_json/index.js
new file mode 100644
index 0000000..5705fec
--- /dev/null
+++ b/node_modules/collection_json/index.js
@@ -0,0 +1,40 @@
+var _ = require('underscore');
+
+/**
+ * Takes any object and make it look like a collection+json object with utility methods.
+ */
+function fromObject(root) {
+ root = root || {};
+ var collection = root.collection || {};
+ collection.items = collection.items || {};
+ collection.links = collection.links || {};
+ collection.queries = collection.queries || {};
+ collection.templates = collection.templates || {};
+
+ collection.mapItemData = function(f) {
+ return _.map(collection.items, function(item) {
+// console.log("item", item);
+ var map = _.reduce(item.data, function(map, field) {
+// console.log("field", field);
+ map[field.name] = field.value;
+ return map;
+ }, {});
+// console.log("map", map);
+ return f(map);
+ });
+ };
+
+ return collection;
+}
+module.exports.fromObject = fromObject;
+
+function fromString(s) {
+ var object = {};
+ try {
+ object = JSON.parse(s);
+ }
+ catch(ex) {
+ }
+ return fromObject(object);
+}
+module.exports.fromString = fromString;
diff --git a/node_modules/collection_json/package.json b/node_modules/collection_json/package.json
new file mode 100644
index 0000000..f14b77c
--- /dev/null
+++ b/node_modules/collection_json/package.json
@@ -0,0 +1,12 @@
+{
+ "name": "collection_json",
+ "version": "0.0.1",
+ "description": "application/vnd.collection+json Utility library",
+ "author": "Trygve Laugstøl <trygvis@inamo.no>",
+ "dependencies": {
+ "underscore": "latest"
+ },
+ "devDependencies": {
+ "mocha": "1.0.3"
+ }
+}
diff --git a/node_modules/collection_json/test/collection_json.test.js b/node_modules/collection_json/test/collection_json.test.js
new file mode 100644
index 0000000..b74d99b
--- /dev/null
+++ b/node_modules/collection_json/test/collection_json.test.js
@@ -0,0 +1,39 @@
+var assert = require('assert')
+ , collection_json = require('../');
+
+function identity(x) { return x; }
+
+var collection1 = {collection: {
+ items: [
+ { href: "http://example.com/1", links: [],
+ data: [
+ { prompt: "prompt1", name: "field-1", value: "1", baz: "woot" },
+ { prompt: "prompt2", name: "field-2", value: "2", foz: "asdf" },
+ ]
+ },
+ { href: "http://example.com/2", links: [],
+ data: [
+ { prompt: "prompt1", name: "field-1", value: "3" },
+ { prompt: "prompt2", name: "field-2", value: "4" },
+ ]
+ }
+ ]
+}};
+
+describe('fromObject', function() {
+ it('Has basic fields', function() {
+ var collection = collection_json.fromObject({});
+ assert.deepEqual([], collection.items);
+ assert.deepEqual([], collection.links);
+ assert.deepEqual([], collection.queries);
+ assert.deepEqual([], collection.templates);
+ });
+
+ it('Can map data field in items', function() {
+ assert.deepEqual(collection_json.fromObject(collection1).mapItemData(identity),
+ [{"field-1": "1", "field-2": "2"}
+ ,{"field-1": "3", "field-2": "4"}]);
+ function f(map) { return parseInt(map["field-1"]) + parseInt(map["field-2"]); }
+ assert.deepEqual(collection_json.fromObject(collection1).mapItemData(f), [3, 7]);
+ });
+});
diff --git a/test/checkins.test.js b/test/checkins.test.js
new file mode 100644
index 0000000..66514de
--- /dev/null
+++ b/test/checkins.test.js
@@ -0,0 +1,27 @@
+var BitrafBot = require('../lib/BitrafBot.js')
+ , assert = require('assert');
+
+var json = JSON.stringify({ "collection": {
+ "version": "1.0",
+ "links": [],
+ "items": [
+ { "data": [
+ {"name": "date", "value": "2012-05-04"},
+ {"name": "checkins", "value": "4"},
+ {"name": "last-checkin", "value": "2012-05-04T16:09:20+02:00"} ]
+ }
+ ],
+ "sort": {
+ "fields": [
+ { "name": "date" },
+ { "name": "checkins" }
+ ]
+ }
+}});
+
+describe('Checkins', function() {
+ it('can parse checkins', function() {
+ var str = BitrafBot.onCheckinsEnd(json);
+ assert.equal('checkins: 4, last checkin: 2012-05-04T16:09:20+02:00', str);
+ });
+});