From 447ec5c5902a803ae68d42f8b0c95db64364048a Mon Sep 17 00:00:00 2001 From: Vylpes Date: Sun, 7 Feb 2021 16:15:40 +0000 Subject: [PATCH 1/5] Resolve "Pass Entire JSON String to the user" --- index.js | 20 ++++++++++++++++---- readme.md | 19 +++++++++++++++---- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index eaa2bce..c7cef1b 100644 --- a/index.js +++ b/index.js @@ -33,13 +33,25 @@ function randomBunny(subreddit, sortBy, cb) { const random = getRandom(0, size - 1); // Get variables from json to pass back - const image = data[random].data['url']; - const title = data[random].data['title']; + 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 (image.includes('.jpg')) { + if (json.url.includes('.jpg')) { found = true; - cb(image, title); + cb(json); } } }); diff --git a/readme.md b/readme.md index 64e64cd..a52c9d5 100644 --- a/readme.md +++ b/readme.md @@ -14,8 +14,8 @@ $ npm install --save random-bunny ```js const randomBunny = require('random-bunny'); -randomBunny('rabbits', 'new', (image, title) => { - console.log(title + ": " + image); +randomBunny('rabbits', 'new', res => { + console.log(res.title + ": " + res.url); }); ``` @@ -24,7 +24,18 @@ randomBunny('rabbits', 'new', (image, title) => { ### `randomBunny()` -Returns a `url` and `title` for a random post to the `callback`. Accepts 3 arguments: `subreddit`, `sortby` ('new', 'hot', 'top'), `callback(image, title)` +Returns a `json string` for a random post to the `callback`. Accepts 3 arguments: `subreddit`, `sortby` ('new', 'hot', 'top'), `callback(res)` + +The json string which gets returned consists of: +- archived +- downs +- hidden +- permalink +- subreddit +- subredditSubscribers +- title +- ups +- url ## Notes @@ -37,4 +48,4 @@ Returns a `url` and `title` for a random post to the `callback`. Accepts 3 argum ## License -MIT © [Vylpes](https://gitlab.vylpes.com/Vylpes); +MIT © [Vylpes](https://gitlab.vylpes.com/Vylpes) \ No newline at end of file -- 2.43.4 From 58f4e629d84f8fc15a7f8059347e61ee8c8e8557 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 3 Apr 2021 19:55:46 +0100 Subject: [PATCH 2/5] Add a promise function --- index.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index c7cef1b..3f54ecb 100644 --- a/index.js +++ b/index.js @@ -58,10 +58,64 @@ function randomBunny(subreddit, sortBy, cb) { }); } +function promise(subreddit, sortBy) { + return new Promise((resolve, reject) => { + // 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 -- 2.43.4 From 6ae96eb38dcd875895de971afc688a04fdbfbe32 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 3 Apr 2021 19:59:27 +0100 Subject: [PATCH 3/5] Adjust documentation --- readme.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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 From 3731482186031a3cd133dc455064e85d3d58e877 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 12 Apr 2021 20:16:34 +0100 Subject: [PATCH 4/5] Fix ci issues --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 3f54ecb..383c30c 100644 --- a/index.js +++ b/index.js @@ -59,7 +59,7 @@ function randomBunny(subreddit, sortBy, cb) { } function promise(subreddit, sortBy) { - return new Promise((resolve, reject) => { + return new Promise((resolve) => { // If the sortable list doesn't include sortBy, default to 'hot' if (!sortable.includes(sortBy)) sortBy = 'hot'; @@ -118,4 +118,4 @@ function getRandom(min, max) { module.exports = { randomBunny, promise, -} \ No newline at end of file +}; \ No newline at end of file -- 2.43.4 From bfa56d657caec9dab2db4c7a323d3c9f4d56d1c5 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 12 Apr 2021 20:17:46 +0100 Subject: [PATCH 5/5] Fix ci issues 2 --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 383c30c..7b59cfe 100644 --- a/index.js +++ b/index.js @@ -59,7 +59,7 @@ function randomBunny(subreddit, sortBy, cb) { } function promise(subreddit, sortBy) { - return new Promise((resolve) => { + return new Promise(resolve => { // If the sortable list doesn't include sortBy, default to 'hot' if (!sortable.includes(sortBy)) sortBy = 'hot'; -- 2.43.4