Skip to content

Instantly share code, notes, and snippets.

@phishy

phishy/Story.js Secret

Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phishy/c9ac6b2aad7d2f87a5de to your computer and use it in GitHub Desktop.
Save phishy/c9ac6b2aad7d2f87a5de to your computer and use it in GitHub Desktop.
// a functional copy can be found here: https://ide.c9.io/phishy/sails
/**
* Story.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/#!documentation/models
*/
var Promise = require('bluebird');
var request = Promise.promisify(require('request'));
/**
* need to be able to stub Story and Vendor
* need to be able to spy on request
*/
module.exports = {
attributes: {
},
/**
* send story to api then mark as updated
**/
process: function (story) {
Vendor.findOne({ id: story.vendor }).then(function (vendor) {
story.vendor = vendor;
return request({
method: 'post',
url: vendor.url,
body: story,
headers: {
'Content-type': 'application/json'
}
}).then(function (res) {
this.update(story.id, { status: 'sent' }).then(function (res) {
sails.log.info('story sent');
return true;
}).fail(function (err) {
sails.log.error('failed to update story after send');
});
}).catch(function (err) {
sails.log.error('failed to send story', err);
return err;
});
});
}
};
// a functional copy can be found here: https://ide.c9.io/phishy/sails
var Story = require('../api/models/Story.js');
describe('Story', function(){
describe('#process', function(){
it('should POST a story to a url and update database', function(done){
Story.process({ name: 'cool story', vendor: '123', url: 'http://localhost/receiver' }).then(function(res){
// assert that correct parameters got passed to request
// assert that correct parameters got passed to this.update
assert.ok(res);
done();
}).catch(function(err){
done(err);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment