From 80fb1ad6977552438a48cebab2fe5cef747b1d70 Mon Sep 17 00:00:00 2001 From: Vylpes Date: Mon, 12 Apr 2021 20:22:57 +0100 Subject: [PATCH] Feature/8 return a promise (#5) * Resolve "Pass Entire JSON String to the user" * Add a promise function * Adjust documentation * Fix ci issues * Fix ci issues 2 Co-authored-by: Ethan Lane --- index.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- readme.md | 15 ++++++++++++++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index c7cef1b..7b59cfe 100644 --- a/index.js +++ b/index.js @@ -58,10 +58,64 @@ function randomBunny(subreddit, sortBy, cb) { }); } +function promise(subreddit, sortBy) { + return new Promise(resolve => { + // If the sortable list doesn't include sortBy, default to 'hot' + if (!sortable.includes(sortBy)) sortBy = 'hot'; + + // Fetch the json from reddit + // For example, if you're getting a random image from r/rabbits, sorted by new: + // https://www.reddit.com/r/rabbits/new.json + fetch(`https://www.reddit.com/r/${subreddit}/${sortBy}.json`).then(res => { + res.json().then(res => { + // Get the part of the json string which the data comes from + const data = res.data.children; + const size = data.length; + + // Found is used for the while loop in order to break out of the loop. + // We need to loop as the json string will contain invalid data for what we need + // Specifically videos. + let found = false; + + // Loop until a valid image post is found + while (!found) { + // Generate random number + const random = getRandom(0, size - 1); + + // Get variables from json to pass back + const randomSelect = data[random].data; + + // The json string to send back + const json = { + archived: randomSelect['archived'], + downs: randomSelect['downs'], + hidden: randomSelect['hidden'], + permalink: randomSelect['permalink'], + subreddit: randomSelect['subreddit'], + subredditSubscribers: randomSelect['subreddit_subscribers'], + title: randomSelect['title'], + ups: randomSelect['ups'], + url: randomSelect['url'] + }; + + // If the post is a .jpg, send back the data and stop looping + if (json.url.includes('.jpg')) { + found = true; + resolve(json); + } + } + }); + }); + }) +} + // Generate a random number function getRandom(min, max) { return Math.floor((Math.random() * max) + min); } // Export Functions -module.exports = randomBunny; \ No newline at end of file +module.exports = { + randomBunny, + promise, +}; \ No newline at end of file diff --git a/readme.md b/readme.md index a52c9d5..2c516be 100644 --- a/readme.md +++ b/readme.md @@ -12,13 +12,20 @@ $ npm install --save random-bunny ## Usage ```js -const randomBunny = require('random-bunny'); +const { randomBunny } = require('random-bunny'); randomBunny('rabbits', 'new', res => { console.log(res.title + ": " + res.url); }); ``` +```js +const { promise } = require('random-bunny'); + +promise('rabbits', 'new').then((res) => { + console.log(res.title); +}); +``` ## API @@ -37,6 +44,12 @@ The json string which gets returned consists of: - ups - url +### `promise()` + +Returns a `json string` for a random post in a `promise`. Accepts 2 arguments: `subreddit`, `sortby` ('new', 'hot', 'top'). + +The json string returned in the promise consists of the same above. + ## Notes * Node 4 or newer. -- 2.43.4