Compare commits

...

3 commits

Author SHA1 Message Date
ee8bffa7ae Update the output to be in alphabetical order
All checks were successful
Test / build (push) Successful in 11s
2024-10-26 16:30:42 +01:00
91c88b1180 Update documentation 2024-10-26 16:28:14 +01:00
b1adc39a35 Add new tests 2024-10-26 16:22:43 +01:00
7 changed files with 64 additions and 9 deletions

View file

@ -26,6 +26,7 @@ $ random-bunny
Archived = false Archived = false
Author = Rabbit_Owner Author = Rabbit_Owner
Downvotes = 0 Downvotes = 0
Gallery = https://i.redd.it/sfz0srdrimjc1.png, https://i.redd.it/sfz0srdrimjc1.png
Hidden = false Hidden = false
Permalink = /r/Rabbits/comments/1av1rg9/cute_baby_bun/ Permalink = /r/Rabbits/comments/1av1rg9/cute_baby_bun/
Subreddit = Rabbits Subreddit = Rabbits
@ -35,6 +36,8 @@ Upvotes = 211
Url = https://i.redd.it/sfz0srdrimjc1.png 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 ## Help
The command also includes a help option in case you are stuck. The command also includes a help option in case you are stuck.
@ -72,9 +75,11 @@ $ random-bunny --json
$ randon-bunny -j $ 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 ## Sort
You can also choose the sorting option which reddit will use to return the available posts to randomise from. You can also choose the sorting option which reddit will use to return the available posts to randomise from.

View file

@ -39,6 +39,7 @@ The json string which gets returned consists of:
- archived - archived
- author - author
- downs - downs
- gallery
- hidden - hidden
- permalink - permalink
- subreddit - subreddit

View file

@ -2,6 +2,7 @@ export default interface IRedditResult {
Archived: boolean, Archived: boolean,
Author: string, Author: string,
Downs: number, Downs: number,
Gallery: string[],
Hidden: boolean, Hidden: boolean,
Permalink: string, Permalink: string,
Subreddit: string, Subreddit: string,
@ -9,5 +10,4 @@ export default interface IRedditResult {
Title: string, Title: string,
Ups: number, Ups: number,
Url: string, Url: string,
Gallery: string[],
} }

View file

@ -12,10 +12,6 @@ export default class ImageHelper {
const dom = htmlparser.parseDocument(fetched.body); 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); 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 const imgSrc = img
.flatMap(x => x.attributes.find(x => x.name == "src")?.value) .flatMap(x => x.attributes.find(x => x.name == "src")?.value)
.filter(x => x != undefined); .filter(x => x != undefined);

View file

@ -14,7 +14,13 @@ export default class OutputHelper {
outputLines.push(`Archived = ${result.Archived}`); outputLines.push(`Archived = ${result.Archived}`);
outputLines.push(`Author = ${result.Author}`); outputLines.push(`Author = ${result.Author}`);
outputLines.push(`Downvotes = ${result.Downs}`); outputLines.push(`Downvotes = ${result.Downs}`);
if (result.Gallery.length > 1) {
outputLines.push(`Gallery = ${result.Gallery.join(", ")}`);
}
outputLines.push(`Hidden = ${result.Hidden}`); outputLines.push(`Hidden = ${result.Hidden}`);
outputLines.push(`Permalink = ${result.Permalink}`); outputLines.push(`Permalink = ${result.Permalink}`);
outputLines.push(`Subreddit = ${result.Subreddit}`); outputLines.push(`Subreddit = ${result.Subreddit}`);
outputLines.push(`Subreddit Subscribers = ${result.SubredditSubscribers}`); outputLines.push(`Subreddit Subscribers = ${result.SubredditSubscribers}`);
@ -22,9 +28,6 @@ export default class OutputHelper {
outputLines.push(`Upvotes = ${result.Ups}`); outputLines.push(`Upvotes = ${result.Ups}`);
outputLines.push(`Url = ${result.Url}`); outputLines.push(`Url = ${result.Url}`);
if (result.Gallery.length > 1) {
outputLines.push(`Gallery = ${result.Gallery.join(", ")}`);
}
if (options.queryMetadata != null) { if (options.queryMetadata != null) {
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`); outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);

View file

@ -30,3 +30,17 @@ Query.Subreddit = rabbits
Query.Sort By = hot Query.Sort By = hot
Query.Limit = 100" 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"
`;

View file

@ -105,4 +105,40 @@ describe("GenerateOutput", () => {
// Assert // Assert
expect(result).toMatchSnapshot(); 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();
});
}); });