0.0.3 • Published 10 years ago

changed-http v0.0.3

Weekly downloads
13
License
ISC
Repository
github
Last release
10 years ago

changed

Build Status Code Climate

Polls HTTP resources and fires events when they change.

Features

  • Supports HTTP and HTTPS
  • Transparently decodes gzipped resources

Installation

npm install robinjmurphy/changed

Usage

var changed = require('changed-http');

var resource = new changed.Resource('http://www.example.com');

resource.on('changed', function (current, previous) {
  console.log('Resource changed. Response body was ' + previous + ' , is now ' + current + '.');
});

resource.startPolling(5000);

API

new changed.Resource(url, options)

Parameters
  • url - string
  • options - object - configuration options
Options

The options object supports all of the standard options from http.request and https.request. In addition, it supports the following properties:

  • compare - function - overrides the default response body comparison. Receives the current response body as its first argument and the previous response body as its second argument. Should return true if the responses differ.

.startPolling(interval)

Start polling the resource for changes.

Parameters
  • interval - number - the interval time in milliseconds (default 10000)

.stopPolling()

Stop polling the resource.


Events

changed

Fired when the resource's body changes.

resource.on('changed', function (current, previous) {
  // `current` is a string containing the curent response body
  // `previous` is a string containing previous response body 
});
error

Fired when an error occurs.

resource.on('error', function (error) {
  // `error` is an Error object
});
response

Fired each time a response is received whilst polling.

resource.on('response', function (body, res) {
  // `body` is a string containig the response body
  // `res` is the http/https response object
});

Logging

All polling requests are logged using console.info by default. To use a custom logger, like Winston, just set the changed.logger property:

var changed = require('changed-http');
var winston = require('winston');

changed.logger = winston;

Examples

Custom response comparison

In the following example the changed event is only fired when the foo property in a JSON response changes.

var changed = require('changed-http');

var resource = new changed.Resource('http://www.example.com/some/json/file.json', {
  compare: function (current, previous) {
    var currentJson = JSON.parse(current);
    var previousJson = JSON.parse(previous);
    
    return (currentJson.foo !== previousJson.foo);
  }
});

resource.startPolling(5000);
0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago