hack-news
Advanced tools
Comparing version 1.0.11 to 1.0.12
31
index.js
@@ -134,1 +134,32 @@ const https = require('https'); | ||
} | ||
//ask show or job top number of stories | ||
exports.numbOfAskShowOrJobStories = function (asj, numberOfArticles, callback) { | ||
if (asj && typeof asj != "string") { | ||
throw new Error("The paramter must be a string and be one of the following ask, show or job"); | ||
} | ||
if (numberOfArticles && typeof numberOfArticles != 'number') { | ||
throw new Error("The second paramter must be a number"); | ||
} | ||
callback = callback || () => {}; | ||
return new Promise((resolve, reject) => { | ||
https.get('https://hacker-news.firebaseio.com/v0/' + asj +'stories.json?print=pretty', (res) => { | ||
res.on('data', (d) => { | ||
const stories = JSON.parse(d, (key, value) => { | ||
return value && value.type === 'Buffer' | ||
? new Buffer(value.data) | ||
: value; | ||
}); | ||
resolve(stories.splice(0, numberOfArticles)); | ||
callback(stories.splice(0, numberOfArticles)); | ||
}); | ||
}).on('error', (e) => { | ||
reject(e); | ||
callback(e); | ||
}); | ||
}); | ||
} |
{ | ||
"name": "hack-news", | ||
"version": "1.0.11", | ||
"version": "1.0.12", | ||
"description": "A simple library of methods to help you interact with the Hacker News API. There's more on the way.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -87,4 +87,15 @@ ## Installation | ||
hn.askShowOrJobStories('show').then(asj => {console.log(asj);}); | ||
//If you wanted the Top Ten Ask, Show, Job you ca use the numbOfAskShowOrJobStories method like so. | ||
hn.numbOfAskShowOrJobStories('show', 10, (asj, error) => { | ||
if (error) { | ||
console.log(error); | ||
} | ||
console.log(asj); | ||
}); | ||
//Using Promises | ||
hn.numbOfAskShowOrJobStories('job', 10).then(asj => {console.log(asj);}); | ||
``` | ||
This is it for now but stay tuned I will be adding a lot more. |
7938
4
136
101