Last active
April 8, 2016 15:31
-
-
Save sailsinaction/eed1fd98827a4347e7ce to your computer and use it in GitHub Desktop.
Chapter 5
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
.d8888b. 888 888 888888888 .d8888b. d8b 888 | |
d88P Y88b 888 888 888 d88P Y88b Y8P 888 | |
888 888 888 888 888 888 888 888 | |
888 88888b. 8888b. 88888b. 888888 .d88b. 888d888 8888888b. 888 888 .d8888b 888888 .d8888b | |
888 888 "88b "88b 888 "88b 888 d8P Y8b 888P" "Y88b 888 88888 888 88K 888 88K | |
888 888 888 888 .d888888 888 888 888 88888888 888 888 888888 888 888 888 "Y8888b. 888 "Y8888b. | |
Y88b d88P 888 888 888 888 888 d88P Y88b. Y8b. 888 Y88b d88P Y88b d88P 888 X88 Y88b. X88 | |
"Y8888P" 888 888 "Y888888 88888P" "Y888 "Y8888 888 "Y8888P" "Y8888P88 888 88888P' "Y888 88888P' | |
888 | |
888 | |
888 |
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
/** | |
* Bootstrap | |
* (sails.config.bootstrap) | |
* | |
* An asynchronous bootstrap function that runs before your Sails app gets lifted. | |
* This gives you an opportunity to set up your data model, run jobs, or perform some special logic. | |
* | |
* For more information on bootstrapping your app, check out: | |
* http://sailsjs.org/#/documentation/reference/sails.config/sails.config.bootstrap.html | |
*/ | |
module.exports.bootstrap = function(cb) { | |
console.log('Hello World!'); | |
return cb(); | |
}; |
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
/** | |
* Bootstrap | |
* (sails.config.bootstrap) | |
* | |
* An asynchronous bootstrap function that runs before your Sails app gets lifted. | |
* This gives you an opportunity to set up your data model, run jobs, or perform some special logic. | |
* | |
* For more information on bootstrapping your app, check out: | |
* http://sailsjs.org/#/documentation/reference/sails.config/sails.config.bootstrap.html | |
*/ | |
module.exports.bootstrap = function(cb) { | |
Video.count().exec(function(err, numVideos) { | |
if (err) { | |
return cb(err); | |
} | |
if (numVideos > 0) { | |
console.log('Number of video records: ', numVideos); | |
return cb(); | |
} | |
// No records in the video model, seed the database. | |
console.log('There are no video records.'); | |
return cb(); | |
}); | |
}; |
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
/** | |
* Bootstrap | |
* (sails.config.bootstrap) | |
* | |
* An asynchronous bootstrap function that runs before your Sails app gets lifted. | |
* This gives you an opportunity to set up your data model, run jobs, or perform some special logic. | |
* | |
* For more information on bootstrapping your app, check out: | |
* http://sailsjs.org/#/documentation/reference/sails.config/sails.config.bootstrap.html | |
*/ | |
module.exports.bootstrap = function(cb) { | |
Video.count().exec(function(err, numVideos) { | |
if (err) { | |
return cb(err); | |
} | |
if (numVideos > 0) { | |
console.log('Existing video records: ', numVideos) | |
return cb(); | |
} | |
var Youtube = require('machinepack-youtube'); | |
// List Youtube videos which match the specified search query. | |
Youtube.searchVideos({ | |
query: 'grumpy cat', | |
apiKey: 'AIzaSyDG3ecGW1V6PyF5UYtmR6Jnkl29F_S2alE', | |
limit: 15, | |
}).exec({ | |
// An unexpected error occurred. | |
error: function(err) { | |
console.log('an error: ', err); | |
}, | |
// OK. | |
success: function(result) { | |
console.log('the result: ', result); | |
}, | |
}); | |
}); | |
}; |
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
/** | |
* Bootstrap | |
* (sails.config.bootstrap) | |
* | |
* An asynchronous bootstrap function that runs before your Sails app gets lifted. | |
* This gives you an opportunity to set up your data model, run jobs, or perform some special logic. | |
* | |
* For more information on bootstrapping your app, check out: | |
* http://sailsjs.org/#/documentation/reference/sails.config/sails.config.bootstrap.html | |
*/ | |
module.exports.bootstrap = function(cb) { | |
Video.count().exec(function(err, numVideos) { | |
if (err) { | |
return cb(err); | |
} | |
if (numVideos > 0) { | |
console.log('Existing video records: ', numVideos) | |
return cb(); | |
} | |
var Youtube = require('machinepack-youtube'); | |
// List Youtube videos which match the specified search query. | |
Youtube.searchVideos({ | |
query: 'grumpy cat', | |
apiKey: 'AIzaSyDNuOChtnL1SuidLwFMyicTOq5e-t-MOTU', | |
limit: 15, | |
}).exec({ | |
// An unexpected error occurred. | |
error: function(err) { | |
console.log('an error: ', err); | |
return cb(err); | |
}, | |
// OK. | |
success: function(foundVideos) { | |
console.log('the foundVideos: ', foundVideos); | |
return cb(); | |
}, | |
}); | |
}); | |
}; |
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
/** | |
* Bootstrap | |
* (sails.config.bootstrap) | |
* | |
* An asynchronous bootstrap function that runs before your Sails app gets lifted. | |
* This gives you an opportunity to set up your data model, run jobs, or perform some special logic. | |
* | |
* For more information on bootstrapping your app, check out: | |
* http://sailsjs.org/#/documentation/reference/sails.config/sails.config.bootstrap.html | |
*/ | |
module.exports.bootstrap = function(cb) { | |
Video.count().exec(function(err, numVideos) { | |
if (err) { | |
return cb(err); | |
} | |
if (numVideos > 0) { | |
console.log('Existing video records: ', numVideos) | |
return cb(); | |
} | |
var Youtube = require('machinepack-youtube'); | |
// List Youtube videos which match the specified search query. | |
Youtube.searchVideos({ | |
query: 'grumpy cat', | |
apiKey: 'AIzaSyDNuOChtnL1SuidLwFMyicTOq5e-t-MOTU', | |
limit: 15, | |
}).exec({ | |
// An unexpected error occurred. | |
error: function(err) { | |
console.log('an error: ', err); | |
return cb(err); | |
}, | |
// OK. | |
success: function(foundVideos) { | |
console.log('the foundVideos: ', foundVideos); | |
_.each(foundVideos, function(video) { | |
video.src = 'https://www.youtube.com/embed/' + video.id; | |
delete video.description; | |
delete video.publishedAt; | |
delete video.id; | |
delete video.url; | |
}); | |
console.log(foundVideos); | |
return cb(); | |
}, | |
}); | |
}); | |
}; |
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
/** | |
* Bootstrap | |
* (sails.config.bootstrap) | |
* | |
* An asynchronous bootstrap function that runs before your Sails app gets lifted. | |
* This gives you an opportunity to set up your data model, run jobs, or perform some special logic. | |
* | |
* For more information on bootstrapping your app, check out: | |
* http://sailsjs.org/#/documentation/reference/sails.config/sails.config.bootstrap.html | |
*/ | |
module.exports.bootstrap = function(cb) { | |
Video.count().exec(function(err, numVideos) { | |
if (err) { | |
return cb(err); | |
} | |
if (numVideos > 0) { | |
console.log('Existing video records: ', numVideos) | |
return cb(); | |
} | |
var Youtube = require('machinepack-youtube'); | |
// List Youtube videos which match the specified search query. | |
Youtube.searchVideos({ | |
query: 'grumpy cat', | |
apiKey: 'AIzaSyDNuOChtnL1SuidLwFMyicTOq5e-t-MOTU', | |
limit: 15, | |
}).exec({ | |
// An unexpected error occurred. | |
error: function(err) { | |
console.log('an error: ', err); | |
return cb(err); | |
}, | |
// OK. | |
success: function(foundVideos) { | |
// Transform the incoming foundVideos to match the front end expected format | |
_.each(foundVideos, function(video) { | |
video.src = 'https://www.youtube.com/embed/' + video.id; | |
delete video.description; | |
delete video.publishedAt; | |
delete video.id; | |
delete video.url; | |
}); | |
// Add the transformed video records to the video model | |
Video.create(foundVideos).exec(function(err, videoRecordsCreated) { | |
if (err) { | |
return cb(err); | |
} | |
console.log(videoRecordsCreated); | |
return cb(); | |
}); | |
}, | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment