Skip to content

Commit

Permalink
Update to factory pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlangberg committed Sep 13, 2015
1 parent 9020e82 commit c34be5d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 31 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 3.0.0 (2015-30-05)

## Breaking changes

-- Change from singleton to factory. ```require``` will now work differently. See example in README.

# 2.0.0 (2015-30-05)

## Breaking changes
Expand Down
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ Plugin for [goldwasher](https://www.npmjs.org/package/goldwasher), using [node-s
```
npm install goldwasher-schedule
```
If you haven't already downloaded [goldwasher](https://www.npmjs.org/package/goldwasher), you need to install this too, to run the example:
```
npm install goldwasher
```

## Usage

```javascript
var gs = require('goldwasher-schedule');
gs.setup(targets, options, callback);
var goldwasher = require('goldwasher-schedule');
var gs = goldwasher(targets, options, callback);
gs.start();
gs.stop();
```
Expand Down Expand Up @@ -100,14 +96,14 @@ var targets = [
];

// set up the schedule
gs.setup(targets, function(error, results) {
gs(targets, function(error, results) {
console.log(results);
}).start();
```

## Advanced example
```javascript
var gs = require('goldwasher-schedule');
var goldwasher = require('goldwasher-schedule');

// first will use default options below, second has custom options
var targets = [
Expand All @@ -134,7 +130,7 @@ var processResults = function(error, results) {
};

// set up the schedule
gs.setup(targets, options, processResults);
var gs = goldwasher(targets, options, processResults);

// start the schedule
gs.start();
Expand Down
16 changes: 13 additions & 3 deletions docs/goldwasher-schedule.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ <h1 class="page-title">Source: goldwasher-schedule.js</h1>
};

var schedule = {
jobs: []
jobs: [],
running: 0
};

/**
Expand Down Expand Up @@ -78,10 +79,13 @@ <h1 class="page-title">Source: goldwasher-schedule.js</h1>
R.forEach(function(target) {
var options = R.merge(_this.options, target);
var job = nodeSchedule.scheduleJob(options.rule, function() {
_this.running++;
gn(
options.url,
options,
function(error, result, response, body) {
_this.running--;

if (error) {
return _this.callback(error);
}
Expand All @@ -104,7 +108,13 @@ <h1 class="page-title">Source: goldwasher-schedule.js</h1>
}, _this.jobs);
};

module.exports = schedule;
module.exports = function(targets, userOptions, callback) {
var gs = Object.create(schedule);

gs.setup(targets, userOptions, callback);

return gs;
};
</code></pre>
</article>
</section>
Expand All @@ -121,7 +131,7 @@ <h2><a href="index.html">Index</a></h2>
<br clear="both">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha5</a> on Thu Sep 10 2015 23:30:10 GMT+0200 (CEST)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha5</a> on Sun Sep 13 2015 15:01:28 GMT+0200 (CEST)
</footer>

<script> prettyPrint(); </script>
Expand Down
14 changes: 6 additions & 8 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ <h3> </h3>
<p>Plugin for <a href="https://www.npmjs.org/package/goldwasher">goldwasher</a>, using <a href="https://www.npmjs.org/package/node-schedule">node-schedule</a> to schedule <a href="https://www.npmjs.org/package/goldwasher-needle">goldwasher-needle</a> requests at time intervals. Requires <a href="https://www.npmjs.org/package/goldwasher">goldwasher</a> to work.</p>
<h2 id="installation">Installation</h2>
<pre><code>npm install goldwasher-schedule
</code></pre><p>If you haven&#39;t already downloaded <a href="https://www.npmjs.org/package/goldwasher">goldwasher</a>, you need to install this too, to run the example:</p>
<pre><code>npm install goldwasher
</code></pre><h2 id="usage">Usage</h2>
<pre><code class="lang-javascript">var gs = require(&#39;goldwasher-schedule&#39;);
gs.setup(targets, options, callback);
<pre><code class="lang-javascript">var goldwasher = require(&#39;goldwasher-schedule&#39;);
var gs = goldwasher(targets, options, callback);
gs.start();
gs.stop();
</code></pre>
Expand Down Expand Up @@ -119,12 +117,12 @@ <h2 id="example">Example</h2>
];

// set up the schedule
gs.setup(targets, function(error, results) {
gs(targets, function(error, results) {
console.log(results);
}).start();
</code></pre>
<h2 id="advanced-example">Advanced example</h2>
<pre><code class="lang-javascript">var gs = require(&#39;goldwasher-schedule&#39;);
<pre><code class="lang-javascript">var goldwasher = require(&#39;goldwasher-schedule&#39;);

// first will use default options below, second has custom options
var targets = [
Expand All @@ -151,7 +149,7 @@ <h2 id="advanced-example">Advanced example</h2>
};

// set up the schedule
gs.setup(targets, options, processResults);
var gs = goldwasher(targets, options, processResults);

// start the schedule
gs.start();
Expand All @@ -178,7 +176,7 @@ <h2><a href="index.html">Index</a></h2>
<br clear="both">

<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha5</a> on Thu Sep 10 2015 23:30:10 GMT+0200 (CEST)
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha5</a> on Sun Sep 13 2015 15:01:28 GMT+0200 (CEST)
</footer>

<script> prettyPrint(); </script>
Expand Down
4 changes: 2 additions & 2 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var gs = require('./lib/goldwasher-schedule');
var goldwasher = require('./lib/goldwasher-schedule');

// first will use default options below, second has custom options
var targets = [
Expand All @@ -27,7 +27,7 @@ var processResults = function(error, results) {
};

// set up the schedule
gs.setup(targets, options, processResults);
var gs = goldwasher(targets, options, processResults);

// start the schedule
gs.start();
Expand Down
14 changes: 12 additions & 2 deletions lib/goldwasher-schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var getDefaults = function(options) {
};

var schedule = {
jobs: []
jobs: [],
running: 0
};

/**
Expand Down Expand Up @@ -51,10 +52,13 @@ schedule.start = function() {
R.forEach(function(target) {
var options = R.merge(_this.options, target);
var job = nodeSchedule.scheduleJob(options.rule, function() {
_this.running++;
gn(
options.url,
options,
function(error, result, response, body) {
_this.running--;

if (error) {
return _this.callback(error);
}
Expand All @@ -77,4 +81,10 @@ schedule.stop = function() {
}, _this.jobs);
};

module.exports = schedule;
module.exports = function(targets, userOptions, callback) {
var gs = Object.create(schedule);

gs.setup(targets, userOptions, callback);

return gs;
};
14 changes: 7 additions & 7 deletions test/goldwasher-schedule.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var chai = require('chai');
chai.use(require('chai-things'));
var should = chai.should();
var sinon = require('sinon');
var gs = require('../lib/goldwasher-schedule');
var goldwasher = require('../lib/goldwasher-schedule');

var options = {
schedule: {
Expand Down Expand Up @@ -44,21 +44,21 @@ after(function() {
describe('initialization', function() {

it('loads without options', function(done) {
gs.setup(targets, function() {
goldwasher(targets, function() {
});

done();
});

it('accepts options', function(done) {
gs.setup(targets, options, function() {
goldwasher(targets, options, function() {
});

done();
});

it('passes down options', function(done) {
gs.setup(targets, options, function() {
var gs = goldwasher(targets, options, function() {
});

gs.options.should.have.property('goldwasher');
Expand All @@ -72,7 +72,7 @@ describe('initialization', function() {
describe('running', function() {

it('runs and stops', function(done) {
gs.setup(targets, options, function(error, results) {
var gs = goldwasher(targets, options, function(error, results) {
results.length.should.be.greaterThan(0);
gs.stop();
done();
Expand All @@ -90,7 +90,7 @@ describe('running', function() {
}
];

gs.setup(
var gs = goldwasher(
targets,
options,
function(error, results, target) {
Expand All @@ -106,7 +106,7 @@ describe('running', function() {

it('returns error on goldwasher-needle failure', function(done) {
var targets = [{url: 'foo'}];
gs.setup(targets, options, function(error, results) {
var gs = goldwasher(targets, options, function(error, results) {
should.exist(error);
gs.stop();
done();
Expand Down

0 comments on commit c34be5d

Please sign in to comment.