]>
git.r.bdr.sh - rbdr/generador-de-insultos/blob - lib/Conjugator.js
4 var Promise
= require('bluebird');
5 var cheerio
= require('cheerio');
7 var request
= require('request');
10 var Conjugator
= Class({}, "Conjugator")({
12 _baseUrl: "http://www.spanishdict.com/conjugate/",
13 _selector: ".vtable-word",
15 init : function init(config
) {
16 config
= config
|| {};
18 Object
.keys(config
).forEach(function (property
) {
19 this[property
] = config
[property
];
23 conjugate : function conjugate(verb
) {
24 return new Promise(function (resolve
, reject
) {
25 request(this._baseUrl
+ verb
, function (err
, res
, body
) {
26 var $, result
, finalVerb
;
30 $ = cheerio
.load(body
);
31 result
= $(this._selector
)[10];
34 console
.log("Verb not found: ", verb
);
35 return reject(new Error("Not a valid verb"));
38 finalVerb
= this._extractWord(result
);
40 console
.log(verb
, finalVerb
);
46 _extractWord : function _extractWord(node
) {
47 var word
= "", components
;
49 node
.children
.forEach(function (child
) {
50 if (child
.type
=== "text") {
53 word
+= this._extractWord(child
);
57 word
= word
.split(",")[0]; // multiple conjugations, take 1
58 components
= word
.split(" "); // some special cases have two words
59 // use the last, why not
61 return components
[components
.length
- 1];
66 module
.exports
= Conjugator
;