|
// |
|
// 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(); |