2015-08-24 23:19:06 +00:00
|
|
|
var cheerio = require('cheerio'),
|
2015-06-04 00:14:45 +00:00
|
|
|
request = require('request'),
|
2015-06-19 21:32:17 +00:00
|
|
|
MDNlinks = require('../../seed/bonfireMDNlinks'),
|
2015-06-04 00:14:45 +00:00
|
|
|
resources = require('./resources.json'),
|
2015-07-12 02:51:08 +00:00
|
|
|
nonprofits = require('../../seed/nonprofits.json');
|
|
|
|
|
2015-06-01 22:30:43 +00:00
|
|
|
/**
|
|
|
|
* Cached values
|
|
|
|
*/
|
2015-08-24 23:19:06 +00:00
|
|
|
var allNonprofitNames;
|
2015-06-01 22:30:43 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2015-06-23 00:24:55 +00:00
|
|
|
dasherize: function dasherize(name) {
|
|
|
|
return ('' + name)
|
|
|
|
.toLowerCase()
|
|
|
|
.replace(/\s/g, '-')
|
|
|
|
.replace(/[^a-z0-9\-\.]/gi, '');
|
|
|
|
},
|
|
|
|
|
|
|
|
unDasherize: function unDasherize(name) {
|
2015-08-06 22:37:07 +00:00
|
|
|
return ('' + name)
|
|
|
|
// replace dash with space
|
|
|
|
.replace(/\-/g, ' ')
|
|
|
|
// strip nonalphanumarics chars except whitespace
|
|
|
|
.replace(/[^a-zA-Z\d\s]/g, '')
|
|
|
|
.trim();
|
2015-06-23 00:24:55 +00:00
|
|
|
},
|
|
|
|
|
2015-07-12 02:51:08 +00:00
|
|
|
randomPhrase: function() {
|
2015-06-01 22:30:43 +00:00
|
|
|
return resources.phrases[
|
|
|
|
Math.floor(Math.random() * resources.phrases.length)
|
|
|
|
];
|
|
|
|
},
|
|
|
|
|
2015-07-12 02:51:08 +00:00
|
|
|
randomVerb: function() {
|
2015-06-01 22:30:43 +00:00
|
|
|
return resources.verbs[
|
|
|
|
Math.floor(Math.random() * resources.verbs.length)
|
|
|
|
];
|
|
|
|
},
|
|
|
|
|
2015-07-12 02:51:08 +00:00
|
|
|
randomCompliment: function() {
|
2015-06-01 22:30:43 +00:00
|
|
|
return resources.compliments[
|
|
|
|
Math.floor(Math.random() * resources.compliments.length)
|
|
|
|
];
|
|
|
|
},
|
|
|
|
|
2015-07-12 02:51:08 +00:00
|
|
|
allNonprofitNames: function() {
|
2015-06-01 22:30:43 +00:00
|
|
|
if (allNonprofitNames) {
|
|
|
|
return allNonprofitNames;
|
|
|
|
} else {
|
2015-07-12 02:51:08 +00:00
|
|
|
allNonprofitNames = nonprofits.map(function(elem) {
|
2015-06-01 22:30:43 +00:00
|
|
|
return {name: elem.name};
|
|
|
|
});
|
|
|
|
return allNonprofitNames;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-07-12 02:51:08 +00:00
|
|
|
whichEnvironment: function() {
|
2015-06-01 22:30:43 +00:00
|
|
|
return process.env.NODE_ENV;
|
|
|
|
},
|
|
|
|
|
2015-06-25 22:03:46 +00:00
|
|
|
getURLTitle: function(url, callback) {
|
|
|
|
var result = {
|
|
|
|
title: '',
|
|
|
|
image: '',
|
|
|
|
url: '',
|
|
|
|
description: ''
|
|
|
|
};
|
|
|
|
request(url, function(err, response, body) {
|
|
|
|
if (err || response.statusCode !== 200) {
|
|
|
|
return callback(new Error('failed'));
|
|
|
|
}
|
|
|
|
var $ = cheerio.load(body);
|
|
|
|
var metaDescription = $("meta[name='description']");
|
|
|
|
var metaImage = $("meta[property='og:image']");
|
|
|
|
var urlImage = metaImage.attr('content') ?
|
|
|
|
metaImage.attr('content') :
|
|
|
|
'';
|
|
|
|
|
|
|
|
var metaTitle = $('title');
|
|
|
|
var description = metaDescription.attr('content') ?
|
|
|
|
metaDescription.attr('content') :
|
|
|
|
'';
|
|
|
|
|
|
|
|
result.title = metaTitle.text().length < 90 ?
|
|
|
|
metaTitle.text() :
|
|
|
|
metaTitle.text().slice(0, 87) + '...';
|
|
|
|
|
|
|
|
result.image = urlImage;
|
|
|
|
result.description = description;
|
|
|
|
callback(null, result);
|
|
|
|
});
|
2015-06-19 21:32:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getMDNLinks: function(links) {
|
|
|
|
if (!links) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
// takes in an array of links, which are strings
|
|
|
|
|
|
|
|
// for each key value, push the corresponding link
|
|
|
|
// from the MDNlinks object into a new array
|
|
|
|
return links.map(function(value) {
|
|
|
|
return MDNlinks[value];
|
|
|
|
});
|
2015-06-01 22:30:43 +00:00
|
|
|
}
|
|
|
|
};
|