Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion lib/plugins/trodrigues/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,56 @@
'use strict';

var Nipple = require('nipple');
var cheerio = require('cheerio');

// Declare internals
var internals = {};

// Defaults
internals.defaults = {};

function addInfoParameter(name, ths, response) {
ths.each(function (index, val) {
var data = val.children[0].data;
if(data && data.toLowerCase() == name){
response[name] = ths.eq(index).parent().children().eq(1).text();
}
});
}

exports.register = function(plugin, options, next) {

plugin.route({
method: 'GET',
path: '/trodrigues',
handler: function(request, reply) {
reply('why not zoidberg?');
reply('Call /trodrigues/{Futurama character name}');
}
});

plugin.route({
method: 'GET',
path: '/trodrigues/{character}',
handler: function(request, reply) {
if(!request.params.character){
return reply(new Error('No character specified'))
}
Nipple.get('https://en.wikipedia.org/wiki/'+request.params.character, function (err, res, html) {
if(!err) {
var response = {};
var $ = cheerio.load(html);
var infobox = $('.infobox');
var ths = infobox.find('th');
response.name = ths.eq(0).text();

addInfoParameter('species', ths, response);
addInfoParameter('origin', ths, response);
addInfoParameter('relatives', ths, response);
addInfoParameter('occupation', ths, response);

reply(response);
}
});
}
});

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"dependencies": {
"hapi": "^6.0.2",
"lab": "^3.2.1",
"mustache": "^0.8.2"
"mustache": "^0.8.2",
"cheerio": "^0.17.0",
"nipple": "^2.5.3"
}
}
18 changes: 17 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Hapi = require('hapi');
var Participants = require('./participants.json');
var good = require('good');

var server = Hapi.createServer('0.0.0.0', 8080);

Expand All @@ -15,6 +16,21 @@ Participants.forEach(function (participant) {
});
});

server.pack.register({
plugin: good,
options: {
subscribers: {
'console': ['log', 'error', 'debug']
}
}
}, function (err) {
if (err) {
console.log(err);
} else {
console.log('good has loaded');
}
});

server.start(function (err) {

if (err) {
Expand All @@ -23,4 +39,4 @@ server.start(function (err) {
}

console.log('Server started');
});
});