Skip to content

Instantly share code, notes, and snippets.

@mklabs
Created August 2, 2011 21:22
Show Gist options
  • Select an option

  • Save mklabs/1121254 to your computer and use it in GitHub Desktop.

Select an option

Save mklabs/1121254 to your computer and use it in GitHub Desktop.
A simple Hook, requests feed xml, emits json data

my first hook

trying to learn this cool stuff

omg, hook.io is like magic.

xml2js

a simple Hook which takes xml body and emits matching json structure to the Hook cloud

Here is the gist of the gist:

  • A tiny, hyper-simplified cron which request on regular intervals an xml feed uri.
  • An xml to json conversion is done through the use of https://github.com/Leonidas-from-XIV/node-xml2js
  • once done, a processJson event is emited
  • from there, another hook should process the data... (might think of anything).
//
// requires hookio-request spawned
// requires https://github.com/Leonidas-from-XIV/node-xml2js installed
// npm install xml2js
var Hook = require('hook.io').Hook,
util = require('util'),
xml2js = require('xml2js');
// only one parser? create a parser per gotResponse?
var parser = new xml2js.Parser();
var Xml = exports.Xml = function(options) {
var self = this;
if(!options.interval) throw new Error('missing interval');
if(!options.uri) throw new Error('missing URI');
Hook.call(this, options);
this.on('*::gotResponse', function(data) {
self.log(self.name, 'process xml please..', arguments);
parser.parseString(data.body);
});
parser.addListener('end', function(result) {
self.log(self.name, 'xml 2 json conversion done', result);
self.emit('processJson', result);
});
// This is just to simulate cron job, change this to use something better...
(function cron() {
setTimeout(function(){
// testing stuff with public github timeline
self.emit('sendRequest', {uri: options.uri});
return cron();
}, options.interval);
})();
this.on('hook::ready', function() {
self.log(self.name, 'Starting things with options', options);
// send first request on hook ready
self.emit('sendRequest', {uri: options.uri});
});
};
//
// Inherit from `hookio.Hook`
//
util.inherits(Xml, Hook);
var xml = new Xml({
name: "xml",
debug: true,
uri: 'https://github.com/timeline.',
// set to 6min
interval: 6 * 1000 * 60
});
xml.start();
var Hook = require('hook.io').Hook,
util = require('util');
var JsonProcessor = exports.JsonProcessor = function (options) {
var self = this;
Hook.call(this, options);
this.on('*::processJson', function(json) {
console.log('process json please..', json);
// Backed up with mongoose + mongodb collection for tomorrow
});
};
util.inherits(JsonProcessor, Hook);
var processor = new JsonProcessor({
name: "json-processor",
debug: true
});
processor.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment