multiple-url-poller
Advanced tools
Comparing version 1.0.1 to 2.0.0
@@ -76,2 +76,3 @@ 'use strict'; | ||
this.isPaused = false; | ||
this.comparator.getCache().clear(); | ||
} | ||
@@ -113,7 +114,6 @@ }, { | ||
var url = (typeof requestOptions === 'undefined' ? 'undefined' : _typeof(requestOptions)) === 'object' ? requestOptions.url : requestOptions; | ||
this.request(requestOptions).then(function (body) { | ||
var url = (typeof requestOptions === 'undefined' ? 'undefined' : _typeof(requestOptions)) === 'object' ? requestOptions.url : requestOptions; | ||
var isInitialDiff = !_this.comparator.has(requestOptions); | ||
var diff = _this.comparator.diffAndUpdate(requestOptions, body); | ||
var anyUpdates = diff.some(function (singleDiff) { | ||
@@ -132,3 +132,7 @@ return singleDiff.added || singleDiff.removed; | ||
}).catch(function (error) { | ||
return _this.errorSubject.next(error); | ||
return _this.errorSubject.next({ | ||
requestOptions: requestOptions, | ||
error: error, | ||
url: url | ||
}); | ||
}); | ||
@@ -135,0 +139,0 @@ } |
{ | ||
"name": "multiple-url-poller", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Polls URLs and notifies upon changes", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -22,15 +22,23 @@ # URL Poller | ||
# Installation | ||
``` | ||
# Set up | ||
In the console run: | ||
``` bash | ||
npm install multiple-url-poller | ||
``` | ||
Include the poller: | ||
``` javascript | ||
const Poller = require('multiple-url-poller').Poller; | ||
``` | ||
# Example | ||
## Polling a website that displays current time and log diffs to the console | ||
``` javascript | ||
const Poller = require('multiple-url-poller').Poller; | ||
let interval = 5 * 1000; // time in ms | ||
let interval = 5 * 1000; // time in miliseconds | ||
let urls = ['https://time.is/']; | ||
@@ -46,3 +54,80 @@ let poller = new Poller({ interval, requests: urls }); | ||
## Polling a website that requires user authentication | ||
``` javascript | ||
const Poller = require('multiple-url-poller').Poller; | ||
let interval = 5 * 1000; // time in ms | ||
let requests = [{ | ||
url: 'https://supersecurewebsite.com/guarded-resource', | ||
auth: { | ||
user: 'username', | ||
pass: 'secretpassword' | ||
} | ||
}]; | ||
let poller = new Poller({ interval, requests }); | ||
let diffs$ = poller.getDiffObservable(); | ||
let subscription = diffs$ | ||
.subscribe(changesNotification => console.log('New diff', changesNotification.diff)); | ||
poller.start(); | ||
``` | ||
# Documentation | ||
The idea of this poller is to fetch contents of websites in certain intervals and | ||
compare consecutive ones to check whether any changes were introduces to those websites. | ||
One use case of such a package is an email notifier for college test results. | ||
In my college we usually do not get notified at all about the results from tests, | ||
so by setting up this poller to query lectrers' websites I can send myself (and other people | ||
from my class) email (using _nodemailer_) about the results. | ||
The only thing exposed in this package is the `Poller` class: | ||
* `new Poller(options)` - creates a new poller instance. It has to be started | ||
with the `start` method. | ||
Possible options: | ||
* `interval` (optional) - number of miliseconds between two consecutive polls. | ||
The default is 60 seconds. | ||
* `requests` - array of urls/request options that will be polled. | ||
It may contain either urls as strings or options objects that comply with | ||
the format expected by the `request` library (see [`request` library documentation] | ||
for more information) | ||
Poller instance methods: | ||
* `start()` - begins polling. First batch of requests is sent immediately, each following | ||
batch of requests will be sent after the interval specified in the options provided | ||
when creating a poller. | ||
* `stop()` - stops the poller and clears the cache. | ||
* `pause()` - pauses the poller, resetting the interval. | ||
* `resume()` - resumes the poller after it has been paused. Same as with `start`, | ||
initial requests are sent immediately. | ||
* `getDiffObservable()` - returns the RxJS observable that all diffs will be emitted to. | ||
Objects in the observable implement the following interface: | ||
``` typescript | ||
interface ChangesNotification { | ||
isInitialDiff: boolean; | ||
requestOptions: string | Object; // the url/options that those changes came from | ||
url: string; // the url extracted from requestOptions | ||
diff: SingleDiff[]; // array of single diffs (see below) | ||
body: string; // contents of the website | ||
} | ||
``` | ||
The SingleDiff object is the same as the diff object from `js-diff` library. | ||
* `getErrorObservable()` - returns the RxJS observable that emits error upon request errors. | ||
Objects in the observable implement the following interface: | ||
``` typescript | ||
interface RequestError { | ||
url: string; // same as in ChangesNotification | ||
requestOptions: string | Object; // same as in ChangesNotification | ||
error: Object; // error emitted by the `request` library | ||
} | ||
``` | ||
# Contributing | ||
@@ -49,0 +134,0 @@ If you would like to suggest a feature or report a problem please use the *Issues* tab on GitHub. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
114044
177
148