diff --git a/docs/cli.md b/docs/cli.md index 41abac8..9d25110 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -26,7 +26,6 @@ $ 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 @@ -36,8 +35,6 @@ 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. @@ -75,11 +72,9 @@ $ 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","Gallery":["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"} ``` -- 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 570625a..e7ba49c 100644 --- a/readme.md +++ b/readme.md @@ -39,7 +39,6 @@ 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 2c78e78..bf1e242 100644 --- a/src/contracts/IRedditResult.ts +++ b/src/contracts/IRedditResult.ts @@ -2,7 +2,6 @@ export default interface IRedditResult { Archived: boolean, Author: string, Downs: number, - Gallery: string[], Hidden: boolean, Permalink: string, Subreddit: string, @@ -10,4 +9,5 @@ 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 5c096ea..d2ac3b0 100644 --- a/src/helpers/imageHelper.ts +++ b/src/helpers/imageHelper.ts @@ -12,6 +12,10 @@ 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 0cee9d6..b5009f4 100644 --- a/src/helpers/outputHelper.ts +++ b/src/helpers/outputHelper.ts @@ -14,13 +14,7 @@ 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}`); @@ -28,6 +22,9 @@ 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 73d61c6..a3ad0e8 100644 --- a/tests/helpers/__snapshots__/outputHelper.test.ts.snap +++ b/tests/helpers/__snapshots__/outputHelper.test.ts.snap @@ -30,17 +30,3 @@ 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 1581964..790d6e7 100644 --- a/tests/helpers/outputHelper.test.ts +++ b/tests/helpers/outputHelper.test.ts @@ -105,40 +105,4 @@ 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