From 1828605e4db324813a012bb7b94172ebe976aa5d Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 2 May 2014 13:15:16 -0700 Subject: [PATCH] Merge pull request #27 from Automattic/add/api-methods Add/api methods --- packages/wpcom.js/docs/post.md | 2 +- packages/wpcom.js/index.js | 6 +- packages/wpcom.js/lib/like.js | 76 ++++++++++++++++++++++++++ packages/wpcom.js/lib/post.js | 16 +++++- packages/wpcom.js/test/test.post.js | 85 +++++++++++++++++++++++++---- 5 files changed, 167 insertions(+), 18 deletions(-) create mode 100644 packages/wpcom.js/lib/like.js diff --git a/packages/wpcom.js/docs/post.md b/packages/wpcom.js/docs/post.md index 97ff79d96f098..0a177a7908b3f 100644 --- a/packages/wpcom.js/docs/post.md +++ b/packages/wpcom.js/docs/post.md @@ -73,4 +73,4 @@ Delete a Post. Note: If the post object is of type post or page and the trash is enabled, this request will send the post to the trash. A second request will permanently delete the post. -### Post#likes(fn) +### Post#likesList(fn) diff --git a/packages/wpcom.js/index.js b/packages/wpcom.js/index.js index 5ff3a35f5e14c..c5bf1604001ec 100644 --- a/packages/wpcom.js/index.js +++ b/packages/wpcom.js/index.js @@ -45,13 +45,13 @@ WPCOM.prototype.site = function(id){ /** * List Freshly Pressed Posts * - * @param {Object} params (optional) + * @param {Object} [query] * @param {Function} fn callback function * @api public */ -WPCOM.prototype.freshlyPressed = function(params, fn){ - this.sendRequest('/freshly-pressed', null, params, fn); +WPCOM.prototype.freshlyPressed = function(query, fn){ + this.sendRequest('/freshly-pressed', query, null, fn); }; /** diff --git a/packages/wpcom.js/lib/like.js b/packages/wpcom.js/lib/like.js new file mode 100644 index 0000000000000..3a06873feb471 --- /dev/null +++ b/packages/wpcom.js/lib/like.js @@ -0,0 +1,76 @@ + +/** + * Module dependencies. + */ + +var debug = require('debug')('wpcom:like'); + +/** + * Like methods + * + * @param {String} pid post id + * @param {String} sid site id + * @param {WPCOM} wpcom + * @api public + */ + +function Like(pid, sid, wpcom){ + if (!sid) { + throw new Error('`side id` is not correctly defined'); + } + + if (!pid) { + throw new Error('`post id` is not correctly defined'); + } + + if (!(this instanceof Like)) return new Like(pid, sid, wpcom); + + this.wpcom = wpcom; + this._pid = pid; + this._sid = sid; +} + +/** + * Get your Like status for a Post + * + * @param {Object} [query] + * @param {Function} fn + * @api public + */ + +Like.prototype.mine = function(query, fn){ + var path = '/sites/' + this._sid + '/posts/' + this._pid + '/likes/mine'; + this.wpcom.sendRequest(path, query, null, fn); +}; + +/** + * Like a post + * + * @param {Object} [query] + * @param {Function} fn + * @api public + */ + +Like.prototype.add = function(query, fn){ + var path = '/sites/' + this._sid + '/posts/' + this._pid + '/likes/new'; + this.wpcom.sendRequest({ path: path, method: 'post' }, query, null, fn); +}; + +/** + * Remove your Like from a Post + * + * @param {Function} fn + * @api public + */ + +Like.prototype['delete'] = +Like.prototype.del = function(fn){ + var path = '/sites/' + this._sid + '/posts/' + this._pid + '/likes/mine/delete'; + this.wpcom.sendRequest({ path: path, method: 'post' }, null, null, fn); +}; + +/** + * Expose `Like` module + */ + +module.exports = Like; diff --git a/packages/wpcom.js/lib/post.js b/packages/wpcom.js/lib/post.js index 4b87a87bc35fa..bc51436129cff 100644 --- a/packages/wpcom.js/lib/post.js +++ b/packages/wpcom.js/lib/post.js @@ -1,7 +1,9 @@ + /** * Module dependencies. */ +var Like = require('./like'); var debug = require('debug')('wpcom:post'); /** @@ -120,18 +122,28 @@ Post.prototype.del = function(fn){ }; /** - * Get post likes + * Get post likes list * * @param {Object} [query] * @param {Function} fn * @api public */ -Post.prototype.likes = function(query, fn){ +Post.prototype.likesList = function(query, fn){ var path = '/sites/' + this._sid + '/posts/' + this._id + '/likes'; this.wpcom.sendRequest(path, query, null, fn); }; +/** + * Create a `Like` instance + * + * @api public + */ + +Post.prototype.like = function(){ + return Like( this._id, this._sid, this.wpcom); +}; + /** * Expose `Post` module */ diff --git a/packages/wpcom.js/test/test.post.js b/packages/wpcom.js/test/test.post.js index 7082dcef0f25a..0f670ab04957a 100644 --- a/packages/wpcom.js/test/test.post.js +++ b/packages/wpcom.js/test/test.post.js @@ -160,39 +160,100 @@ describe('WPCOM#Site#Post', function(){ }); - describe('delete()', function(){ + describe('likesList()', function(){ - it('should delete the new added post', function(done){ + it('should get post likes list', function(done){ var site = util.private_site(); var post = site.post(new_post.ID); - post.delete(function(err, data){ + post.likesList(function(err, data){ if (err) throw err; assert.ok(data); - assert.equal(new_post.ID, data.ID); + + assert.equal('number', typeof data.found); + assert.equal('boolean', typeof data.i_like); + assert.equal('object', typeof data.likes); + assert.ok(data.likes instanceof Array); done(); }); + }); }); - describe('likes()', function(){ + describe('like.add()', function(){ - it('should get post likes', function(done){ + it('should add a post like', function(done){ var site = util.private_site(); - var post = site.post(new_post.ID); + var like = site.post(new_post.ID).like(); - post.likes(function(err, data){ + like.add(function(err, data){ if (err) throw err; assert.ok(data); + assert.ok(data.success); + assert.ok(data.i_like); + assert.equal(1, data.like_count); - assert.equal('number', typeof data.found); - assert.equal('boolean', typeof data.i_like); - assert.equal('object', typeof data.likes); - assert.ok(data.likes instanceof Array); + done(); + }); + + }); + + }); + + describe('like.mine()', function(){ + + it('should get the post like status of mine', function(done){ + util.private_site() + .post(new_post.ID) + .like() + .mine(function(err, data){ + if (err) throw err; + + assert.ok(data); + assert.equal(1, data.like_count); + assert.ok(data.i_like); + + done(); + }); + }); + + }); + + describe('like.delete()', function(){ + + it('should remove your like from the post', function(done){ + util.private_site() + .post(new_post.ID) + .like() + .del(function(err, data){ + if (err) throw err; + + assert.ok(data); + assert.ok(data.success); + assert.equal(0, data.like_count); + assert.ok(!(data.i_like)); + + done(); + }); + }); + + }); + + describe('delete()', function(){ + + it('should delete the new added post', function(done){ + var site = util.private_site(); + var post = site.post(new_post.ID); + + post.delete(function(err, data){ + if (err) throw err; + + assert.ok(data); + assert.equal(new_post.ID, data.ID); done(); });