hack-news
Advanced tools
Comparing version 2.0.1 to 2.0.2
38
index.js
@@ -169,2 +169,6 @@ const https = require('https'); | ||
if (id && typeof id != "number") { | ||
throw new Error("The number paramter must be a number"); | ||
} | ||
callback = callback || () => {}; | ||
@@ -175,3 +179,3 @@ return new Promise((resolve, reject) => { | ||
res.on('data', (d) => { | ||
const stories = JSON.parse(d, (key, value) => { | ||
const story = JSON.parse(d, (key, value) => { | ||
return value && value.type === 'Buffer' | ||
@@ -181,4 +185,4 @@ ? new Buffer(value.data) | ||
}); | ||
resolve(stories); | ||
callback(null, stories); | ||
resolve(story); | ||
callback(null, story); | ||
}); | ||
@@ -192,1 +196,29 @@ | ||
} | ||
//get a specific User | ||
exports.userWithId = function (id, callback) { | ||
if (id && typeof id != "string") { | ||
throw new Error("The paramter must be a string and be one of the following ask, show or job"); | ||
} | ||
callback = callback || () => {}; | ||
return new Promise((resolve, reject) => { | ||
https.get('https://hacker-news.firebaseio.com/v0/user/'+ id +'.json?print=pretty', (res) => { | ||
res.on('data', (d) => { | ||
const user = JSON.parse(d, (key, value) => { | ||
return value && value.type === 'Buffer' | ||
? new Buffer(value.data) | ||
: value; | ||
}); | ||
resolve(user); | ||
callback(null, user); | ||
}); | ||
}).on('error', (e) => { | ||
reject(e); | ||
callback(e, null); | ||
}); | ||
}); | ||
} |
{ | ||
"name": "hack-news", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"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", |
@@ -104,2 +104,22 @@ ## Installation | ||
All items have some of the following properties, with required properties in bold: | ||
| Field | Description | | ||
| ----- | :---------- | | ||
| id | The item's unique id. | | ||
| deleted | true if the item is deleted. | | ||
| type | The type of item. One of "job", "story", "comment", "poll", or "pollopt". | | ||
| by | The username of the item's author. | | ||
| time | Creation date of the item, in Unix Time. | | ||
| text | The comment, story or poll text. HTML. | | ||
| dead | true if the item is dead. | | ||
| parent | The item's parent. For comments, either another comment or the relevant story. For pollopts, the relevant poll. | | ||
| kids | The ids of the item's comments, in ranked display order. | | ||
| url | The URL of the story. | | ||
| score | The story's score, or the votes for a pollopt. | | ||
| title | The title of the story, poll or job. | | ||
| parts | A list of related pollopts, in display order. | | ||
| descendants | In the case of stories or polls, the total comment count. | | ||
```javascript | ||
@@ -134,2 +154,46 @@ var hn = require('hack-news'); | ||
####Users | ||
The examples below show how to select a single user with his or hers ID you can also use | ||
the user method with the other methods provided. | ||
All users have some of the following properties. | ||
| Field | Description | | ||
| ----- | :---------- | | ||
| id | The user's unique username. Case-sensitive. Required. | | ||
| delay | Delay in minutes between a comment's creation and its visibility to other users. | | ||
| created | Creation date of the user, in Unix Time. | | ||
| karma | The user's karma. | | ||
| about | The user's optional self-description. HTML. | | ||
| submitted | List of the user's stories, polls and comments. | | ||
```javascript | ||
var hn = require('hack-news'); | ||
//This will return a object filled with data corresponding to the ID that was used. | ||
hn.userWithId('thefox', (error, user) => { | ||
if (error) { | ||
console.log(error); | ||
} | ||
console.log(user); | ||
}); | ||
//Used with another method | ||
hn.storyWithId(7567, (error, story) => { | ||
var myUser = story.by; | ||
hn.userWithId(myUser, (error, user) => { | ||
console.log(user); | ||
}); | ||
}); | ||
//Using Promises | ||
hn.userWithId('thefox').then(user => {console.log(user);}); | ||
//Used with another method. | ||
hn.storyWithId(7875).then(story => { | ||
var myUser = story.by; | ||
hn.storyWithId(myUser).then(user => {console.log(user);}); | ||
}); | ||
``` | ||
This is it for now but stay tuned I will be adding a lot more. |
12621
182
198