Skip to content

Commit

Permalink
add mocha test framework, add hacker news page links parser
Browse files Browse the repository at this point in the history
  • Loading branch information
29decibel committed May 10, 2013
1 parent 976cad5 commit cb868bb
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 1 deletion.
15 changes: 15 additions & 0 deletions .gitignore
@@ -0,0 +1,15 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

npm-debug.log
node_modules
16 changes: 16 additions & 0 deletions lib/link_readability.js
@@ -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;

39 changes: 39 additions & 0 deletions lib/page_parser.js
@@ -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;
8 changes: 7 additions & 1 deletion package.json
Expand Up @@ -16,5 +16,11 @@
"rss"
],
"author": "mike li",
"license": "BSD"
"license": "BSD",
"dependencies": {
"cheerio": "~0.11.0",
"request": "~2.21.0",
"readability": "~0.1.0",
"underscore": "~1.4.4"
}
}
18 changes: 18 additions & 0 deletions test/link_readability_test.js
@@ -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();
});
});
26 changes: 26 additions & 0 deletions test/page_parser_test.js
@@ -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();
});
});

});


});
10 changes: 10 additions & 0 deletions test/rss_generator_test.js
@@ -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));
})
})
})

0 comments on commit cb868bb

Please sign in to comment.