UNPKG

uk-petition-poller

Version:

Tool to monitor the number of signatures on a UK Government petition

63 lines (52 loc) 1.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _requestPromise = _interopRequireDefault(require("request-promise")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } class PetitionTracker { constructor() { this.events = {}; this.lastSignatureCount = 0; this.lastSuccessfulUpdate = null; this.lastUpdateSuccessful = null; } on(eventKey, eventFn) { this.events[eventKey] = eventFn; } pollerFunction() { (0, _requestPromise.default)(`https://petition.parliament.uk/petitions/${this.petitionCode}/count.json`).then(jsonString => { const json = JSON.parse(jsonString); this.lastSignatureCount = json['signature_count']; this.lastSuccessfulUpdate = Date.now(); this.lastUpdateSuccessful = true; this.callOn('change', { signature_count: json['signature_count'], last_successful_update: this.lastSuccessfulUpdate, last_call_ok: this.lastUpdateSuccessful }); }).catch(() => { this.lastUpdateSuccessful = false; return this.callOn('change', { signature_count: this.lastSignatureCount, last_successful_update: this.lastSuccessfulUpdate, last_call_ok: this.lastUpdateSuccessful }); }); } async start(petitionCode) { this.petitionCode = petitionCode; await this.pollerFunction(); setInterval(this.pollerFunction.bind(this), 60000); } callOn(eventKey, data) { const callback = this.events[eventKey]; if (callback) { callback(data); } } } var _default = PetitionTracker; exports.default = _default; //# sourceMappingURL=petition-tracker.js.map