Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add mocha test framework, add hacker news page links parser
- Loading branch information
29decibel
committed
May 10, 2013
1 parent
976cad5
commit cb868bb
Showing
7 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
lib-cov | ||
*.seed | ||
*.log | ||
*.csv | ||
*.dat | ||
*.out | ||
*.pid | ||
*.gz | ||
|
||
pids | ||
logs | ||
results | ||
|
||
npm-debug.log | ||
node_modules |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var readability = require('readability'), | ||
_ = require('underscore'); | ||
|
||
|
||
function ReadableLinks(){ | ||
} | ||
|
||
/** | ||
* attach readable text and html to links | ||
*/ | ||
ReadableLinks.attach(links, function(callback){ | ||
}); | ||
|
||
|
||
exports.ReadableLinks = ReadableLinks; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
var request = require('request'), | ||
cheerio = require('cheerio'), | ||
hacker_news_url = "https://news.ycombinator.com/"; | ||
|
||
|
||
/** | ||
* PageParser parse the first page of hacker news | ||
*/ | ||
function PageParser(){ | ||
} | ||
|
||
/** | ||
* getLinks of the first page | ||
*/ | ||
PageParser.prototype.getLinks = function(callback){ | ||
// make a request to get the body of hackernews page | ||
request(hacker_news_url, function (error, response, body) { | ||
var $, links, link; | ||
|
||
if (!error && response.statusCode == 200) { | ||
// get the body | ||
$ = cheerio.load(body); | ||
|
||
// query the links | ||
links = $('td.title a').filter(function(i, el) { | ||
return el.attribs.href != 'news2'; | ||
}).map(function(i, el){ | ||
return { title:el.children[0] ? el.children[0].data : "", url:el.attribs.href}; | ||
}); | ||
|
||
// trigger callback | ||
callback(links); | ||
} | ||
}) | ||
} | ||
|
||
|
||
// exports | ||
exports.PageParser = PageParser; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var ReadableLinks = require("../lib/link_readability").ReadableLinks, | ||
assert = require("assert"); | ||
|
||
|
||
describe("ReadableLinks", function(){ | ||
var links = [ | ||
{ title:'nice', url:"http://lwn.net/SubscriberLink/549580/82983789866b5fad/"} | ||
]; | ||
|
||
it("#attach readable article text/html", function(done){ | ||
|
||
var resultLinks = ReadableLinks.attach(links); | ||
assert.notEqual(null, resultLinks[0].text); | ||
assert.notEqual(null, resultLinks[0].html); | ||
|
||
done(); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var PageParser = require("../lib/page_parser").PageParser, | ||
assert = require("assert"); | ||
|
||
describe('PageParser', function(){ | ||
var parser = new PageParser(); | ||
|
||
describe('#getLinks', function(){ | ||
|
||
it("should return the links ", function(done){ | ||
parser.getLinks(function(links){ | ||
assert.equal(30, links.length); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("should contains title and url property", function(done){ | ||
parser.getLinks(function(links){ | ||
var link = links[0]; | ||
done(); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
|
||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
var assert = require("assert"); | ||
|
||
describe('Array', function(){ | ||
describe('#indexOf()', function(){ | ||
it('should return -1 when the value is not present', function(){ | ||
assert.equal(-1, [1,2,3].indexOf(5)); | ||
assert.equal(-1, [1,2,3].indexOf(0)); | ||
}) | ||
}) | ||
}) |