Code coverage report for lib/baker.js

Statements: 100% (38 / 38)      Branches: 100% (6 / 6)      Functions: 100% (10 / 10)      Lines: 100% (38 / 38)      Ignored: none     

All files » lib/ » baker.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 903 3 3             1 10                 3   1   1 1 1 1     1                   3   1   2 2 2   2 2 1   1     2       2       3   3   1   4   4   4 1 3 1 1   1   2 2         3     3
var async   = require('async');
var p       = require('path');
var webshot = require('webshot');
 
/**
 * class Baker
 *
 * @param {Object} eggtart: Eggtart instance to bake
 */
function Baker(eggtart) {
  this.eggtart = eggtart;
}
 
/**
 * Delete all posts with at least a tag in the specified array of tags.
 *
 * @param {Array} tags: Tags which posts are to be deleted
 * @param {Function} cb: standard cb(err, result) callback
 */
Baker.prototype.delete = function (tags, cb) {
 
  var self = this;
 
  function postCb(post, cb) {
    var url = post.$.href;
    console.log('%s - Deleted %s', 'success'.green, url);
    self.eggtart.posts().delete({ url: url }, cb);
  }
 
  this._iterPostsByTags(tags, postCb, cb);
};
 
/**
 * Take screenshot of all posts with at least a tag in the specified array of tags.
 * Each post will generate a screenshot file in PNG format.
 *
 * @param {Array} tags: Tags which posts are to be captured in a screenshot file
 * @param {Function} cb: standard cb(err, result) callback
 */
Baker.prototype.screenshot = function (tags, cb) {
 
  function postCb(post, cb) {
 
    var url   = post.$.href;
    var title = post.$.description;
    var file  = p.join(post._tag + '-' + title.replace(/\W/g, '') + '.png');
 
    webshot(url, file, { shotSize: {  width: 'all', height: 'all' } }, function (err) {
      if (err) {
        console.error('%s - %s while creating %s', 'error'.red, err.message, file);
      } else {
        console.log('%s - Created %s', 'success'.green, file);
      }
      // don't pass error here, when an error occurs then simply log it but proceed with the rest of the bookmarks
      cb();
    });
  }
 
  this._iterPostsByTags(tags, postCb, cb);
};
 
// iterate through all posts within the specified tags
Baker.prototype._iterPostsByTags = function (tags, postCb, cb) {
 
  var self = this;
 
  function iterPosts(tag, cb) {
 
    console.log('* tag: %s', tag.cyan);
 
    self.eggtart.posts().recent({ tag: tag }, function (err, result) {
 
      if (err) {
        cb(err);
      } else if (result.posts) {
        result.posts.post.forEach(function (item) {
          item._tag = tag;
        });
        async.each(result.posts.post, postCb, cb);
      } else {
        console.log('%s - %s', 'warn'.yellow, 'No bookmark found');
        cb(err, result);
      }
    });
  }
 
  async.eachSeries(tags, iterPosts, cb);
};
 
module.exports = Baker;