-
-
Save phishy/c9ac6b2aad7d2f87a5de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}); | |
}); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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