diff --git a/src/index.js b/src/index.js index 44cba2a..ffad1e6 100644 --- a/src/index.js +++ b/src/index.js @@ -1,13 +1,21 @@ import {Observable} from 'rx-lite'; import fetchJsonp from 'fetch-jsonp'; -const getStream = (collectionId) => { +const getStream = (collectionId, intervalSeconds = 8) => { if (!collectionId) { throw new Error('Please specify a collectionId'); } - const pollRate = 8 * 1000; + if (typeof(intervalSeconds) !== 'number') { + throw new Error(`Polling interval should be a positive integer. A ${typeof(intervalSeconds)} was provided.`); + } + + if (intervalSeconds <= 0) { + throw new Error(`Polling interval should be a positive integer. ${intervalSeconds} was specified.`); + } + + const pollRate = intervalSeconds * 1000; const requestStream = Observable.just(`http://app.climb.social/api/v1/collections/${collectionId}`); diff --git a/src/index.test.js b/src/index.test.js index 6063973..a7ff076 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -37,6 +37,47 @@ describe('Climb.social library', () => { }); + it('should accept an optional polling interval value', () => { + + expect(() => { + const stream = climb.getStream(collectionId, 0.001); + }).to.not.throw(); + + expect(() => { + const stream = climb.getStream(collectionId, 5); + }).to.not.throw(); + + expect(() => { + const stream = climb.getStream(collectionId, 50000); + }).to.not.throw(); + + }); + + it('should error with a negative polling value', () => { + + expect(() => { + const stream = climb.getStream(collectionId, -200); + }).to.throw(); + + expect(() => { + const stream = climb.getStream(collectionId, 0); + }).to.throw(); + + expect(() => { + const stream = climb.getStream(collectionId, -0.00001); + }).to.throw(); + + }); + + + it('should error if interval length is not a interval', () => { + + expect(() => { + const stream = climb.getStream(collectionId, '8'); + }).to.throw(); + + }); + it('should return an object with a `subscribe()` method (an Observable)', () => { const stream = climb.getStream(collectionId);