diff --git a/docs/cli.md b/docs/cli.md index 9d25110..41abac8 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -26,6 +26,7 @@ $ random-bunny Archived = false Author = Rabbit_Owner Downvotes = 0 +Gallery = https://i.redd.it/sfz0srdrimjc1.png, https://i.redd.it/sfz0srdrimjc1.png Hidden = false Permalink = /r/Rabbits/comments/1av1rg9/cute_baby_bun/ Subreddit = Rabbits @@ -35,6 +36,8 @@ Upvotes = 211 Url = https://i.redd.it/sfz0srdrimjc1.png ``` +- The `Gallery` field is only shown when there is more than 1 image returned, which then the `Url` field is the first image of that list. + ## Help The command also includes a help option in case you are stuck. @@ -72,9 +75,11 @@ $ random-bunny --json $ randon-bunny -j -{"Archived":false,"Author":"Rabbit_Owner","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1av1rg9/cute_baby_bun/","Subreddit":"Rabbits","SubredditSubscribers":486085,"Title":"Cute baby bun","Ups":210,"Url":"https://i.redd.it/sfz0srdrimjc1.png"} +{"Archived":false,"Author":"Rabbit_Owner","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1av1rg9/cute_baby_bun/","Subreddit":"Rabbits","SubredditSubscribers":486085,"Title":"Cute baby bun","Ups":210,"Url":"https://i.redd.it/sfz0srdrimjc1.png","Gallery":["https://i.redd.it/sfz0srdrimjc1.png"]} ``` +- The `Url` field is the first image of the `Gallery` array + ## Sort You can also choose the sorting option which reddit will use to return the available posts to randomise from. diff --git a/readme.md b/readme.md index e7ba49c..570625a 100644 --- a/readme.md +++ b/readme.md @@ -39,6 +39,7 @@ The json string which gets returned consists of: - archived - author - downs +- gallery - hidden - permalink - subreddit diff --git a/src/contracts/IRedditResult.ts b/src/contracts/IRedditResult.ts index bf1e242..2c78e78 100644 --- a/src/contracts/IRedditResult.ts +++ b/src/contracts/IRedditResult.ts @@ -2,6 +2,7 @@ export default interface IRedditResult { Archived: boolean, Author: string, Downs: number, + Gallery: string[], Hidden: boolean, Permalink: string, Subreddit: string, @@ -9,5 +10,4 @@ export default interface IRedditResult { Title: string, Ups: number, Url: string, - Gallery: string[], } \ No newline at end of file diff --git a/src/helpers/imageHelper.ts b/src/helpers/imageHelper.ts index d2ac3b0..5c096ea 100644 --- a/src/helpers/imageHelper.ts +++ b/src/helpers/imageHelper.ts @@ -12,10 +12,6 @@ export default class ImageHelper { const dom = htmlparser.parseDocument(fetched.body); const img = htmlparser.DomUtils.findAll((x => x.tagName == "img" && x.attributes.find(y => y.value.includes("https://preview.redd.it")) != null), dom.children); - if (!img) { - return []; - } - const imgSrc = img .flatMap(x => x.attributes.find(x => x.name == "src")?.value) .filter(x => x != undefined); diff --git a/src/helpers/outputHelper.ts b/src/helpers/outputHelper.ts index b5009f4..0cee9d6 100644 --- a/src/helpers/outputHelper.ts +++ b/src/helpers/outputHelper.ts @@ -14,7 +14,13 @@ export default class OutputHelper { outputLines.push(`Archived = ${result.Archived}`); outputLines.push(`Author = ${result.Author}`); outputLines.push(`Downvotes = ${result.Downs}`); + + if (result.Gallery.length > 1) { + outputLines.push(`Gallery = ${result.Gallery.join(", ")}`); + } + outputLines.push(`Hidden = ${result.Hidden}`); + outputLines.push(`Permalink = ${result.Permalink}`); outputLines.push(`Subreddit = ${result.Subreddit}`); outputLines.push(`Subreddit Subscribers = ${result.SubredditSubscribers}`); @@ -22,9 +28,6 @@ export default class OutputHelper { outputLines.push(`Upvotes = ${result.Ups}`); outputLines.push(`Url = ${result.Url}`); - if (result.Gallery.length > 1) { - outputLines.push(`Gallery = ${result.Gallery.join(", ")}`); - } if (options.queryMetadata != null) { outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`); diff --git a/tests/helpers/__snapshots__/outputHelper.test.ts.snap b/tests/helpers/__snapshots__/outputHelper.test.ts.snap index a3ad0e8..73d61c6 100644 --- a/tests/helpers/__snapshots__/outputHelper.test.ts.snap +++ b/tests/helpers/__snapshots__/outputHelper.test.ts.snap @@ -30,3 +30,17 @@ Query.Subreddit = rabbits Query.Sort By = hot Query.Limit = 100" `; + +exports[`GenerateOutput GIVEN the Gallery input has more than 1 item, EXPECT Gallery line to be added 1`] = ` +"Archived = false +Author = author +Downvotes = 0 +Gallery = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d, https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d +Hidden = false +Permalink = /r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/ +Subreddit = Rabbits +Subreddit Subscribers = 654751 +Title = This is my Ms Bear! +Upvotes = 17 +Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d" +`; diff --git a/tests/helpers/outputHelper.test.ts b/tests/helpers/outputHelper.test.ts index 790d6e7..1581964 100644 --- a/tests/helpers/outputHelper.test.ts +++ b/tests/helpers/outputHelper.test.ts @@ -105,4 +105,40 @@ describe("GenerateOutput", () => { // Assert expect(result).toMatchSnapshot(); }); + + test("GIVEN the Gallery input has more than 1 item, EXPECT Gallery line to be added", () => { + // Arrange + const response = { + IsSuccess: true, + Query: { + subreddit: "rabbits", + sortBy: "hot", + limit: 100, + }, + Result: { + Archived: false, + Author: 'author', + Downs: 0, + Hidden: false, + Permalink: "/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/", + Subreddit: "Rabbits", + SubredditSubscribers: 654751, + Title: "This is my Ms Bear!", + Ups: 17, + Url: "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d", + Gallery: [ + "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d", + "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d" + ], + }, + } as IReturnResult; + + const options = {} as ICliOptions; + + // Act + const result = OutputHelper.GenerateOutput(response, options); + + // Assert + expect(result).toMatchSnapshot(); + }); }); \ No newline at end of file