whatsthis
Version:
Discover embedable content from URL's
78 lines (65 loc) • 1.65 kB
JavaScript
;
var urlRegex = require('url-regex')
, parse = require('url-parse')
, Supply = require('supply');
/**
* Representation of a single parse result.
*
* @constructor
* @param {String} text The input that we need to figure out.
* @private
*/
function Result(text) {
this.match = (text.match(urlRegex()) || []).pop();
this.url = parse(this.match || '', { protocol: 'http' }, true);
this.service = 'unknown';
this.source = text;
this.type = 'text';
this.html = '';
}
/**
* Identification and categorization of messages.
*
* @param {Object} options
* @public
*/
function What(options) {
options = options || {};
//
// Add the default set of identification modules.
//
if (!options.defaults) {
this.use('image', require('./services/image'));
this.use('youtube', require('./services/youtube'));
this.use('twitter', require('./services/twitter'));
this.use('xboxdvr', require('./services/xboxdvr'));
this.use('instagram', require('./services/instagram'));
}
}
What.prototype = new Supply();
What.prototype.constructor = What;
/**
* Parse method for all the things.
*
* @param {String} message The message that needs to be parsed.
* @param {Function} fn
* @returns {What} self
* @public
*/
What.prototype.parse = function parse(message, fn) {
var result = new Result(message);
if (!result.match) {
return fn(undefined, result);
}
this.each(result, function each(err, early) {
if ('text' === result.type && result.match) {
result.type = 'link';
}
fn(err, result);
});
return this;
};
//
// Expose the module.
//
module.exports = What;