diff --git a/src/cli.ts b/src/cli.ts index 13ce7f7..d103e86 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -2,8 +2,6 @@ import { Command, Option } from "commander"; import randomBunny from "./index"; import ICliOptions from "./contracts/ICliOptions"; import { exit } from "process"; -import OutputHelper from "./helpers/outputHelper"; -import { writeFileSync } from "fs"; const program = new Command(); @@ -14,7 +12,6 @@ program .option('-s, --subreddit ', 'The subreddit to search', 'rabbits') .option('-j, --json', 'Output as JSON') .option('-q, --query-metadata', 'Include query metadata in result') - .option('-o ', 'Output to file') .addOption(new Option('--sort ', 'Sort by').default('hot').choices(['hot', 'new', 'top'])); program.parse(); @@ -24,14 +21,31 @@ const options: ICliOptions = program.opts(); randomBunny(options.subreddit, options.sort) .then((response) => { if (response.IsSuccess) { - const output = OutputHelper.GenerateOutput(response, options); + const result = response.Result!; - if (options.o) { - writeFileSync(options.o, output); - } else { - console.log(output); + const outputLines: string[] = []; + + if (options.json) { + console.log(JSON.stringify(result)); + return; } + outputLines.push(`Archived = ${result.Archived}`); + outputLines.push(`Downvotes = ${result.Downs}`); + outputLines.push(`Hidden = ${result.Hidden}`); + outputLines.push(`Permalink = ${result.Permalink}`); + outputLines.push(`Subreddit = ${result.Subreddit}`); + outputLines.push(`Subreddit Subscribers = ${result.SubredditSubscribers}`); + outputLines.push(`Title = ${result.Title}`); + outputLines.push(`Upvotes = ${result.Ups}`); + outputLines.push(`Url = ${result.Url}`); + + if (options.queryMetadata != null) { + outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`); + outputLines.push(`Query.Sort By = ${response.Query.sortBy}`); + } + + console.log(outputLines.join("\n")); exit(0); } else { const error = response.Error!; diff --git a/src/contracts/ICliOptions.ts b/src/contracts/ICliOptions.ts index 355c92f..cc3af03 100644 --- a/src/contracts/ICliOptions.ts +++ b/src/contracts/ICliOptions.ts @@ -1,7 +1,6 @@ export default interface ICliOptions { subreddit: string, json?: boolean, - sort: "new" | "hot" | "top", - o?: string, + sort: string, queryMetadata?: boolean, } \ No newline at end of file diff --git a/src/helpers/outputHelper.ts b/src/helpers/outputHelper.ts deleted file mode 100644 index 40a2927..0000000 --- a/src/helpers/outputHelper.ts +++ /dev/null @@ -1,31 +0,0 @@ -import ICliOptions from "../contracts/ICliOptions"; -import IReturnResult from "../contracts/IReturnResult"; - -export default class OutputHelper { - public static GenerateOutput(response: IReturnResult, options: ICliOptions): string { - const result = response.Result!; - - const outputLines: string[] = []; - - if (options.json) { - return JSON.stringify(result); - } - - outputLines.push(`Archived = ${result.Archived}`); - outputLines.push(`Downvotes = ${result.Downs}`); - outputLines.push(`Hidden = ${result.Hidden}`); - outputLines.push(`Permalink = ${result.Permalink}`); - outputLines.push(`Subreddit = ${result.Subreddit}`); - outputLines.push(`Subreddit Subscribers = ${result.SubredditSubscribers}`); - outputLines.push(`Title = ${result.Title}`); - outputLines.push(`Upvotes = ${result.Ups}`); - outputLines.push(`Url = ${result.Url}`); - - if (options.queryMetadata != null) { - outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`); - outputLines.push(`Query.Sort By = ${response.Query.sortBy}`); - } - - return outputLines.join("\n"); - } -} \ No newline at end of file diff --git a/src/helpers/imageHelper.ts b/src/imageHelper.ts similarity index 100% rename from src/helpers/imageHelper.ts rename to src/imageHelper.ts diff --git a/src/index.ts b/src/index.ts index b1ecc5d..75ad628 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,9 +5,17 @@ import { List } from 'linqts'; import IFetchResult from "./contracts/IFetchResult"; import { ErrorCode } from "./constants/ErrorCode"; import ErrorMessages from "./constants/ErrorMessages"; -import ImageHelper from "./helpers/imageHelper"; +import ImageHelper from "./imageHelper"; + +const sortable = [ + 'new', + 'hot', + 'top' +]; + +export default async function randomBunny(subreddit: string, sortBy: string = 'hot'): Promise { + if (!sortable.includes(sortBy)) sortBy = 'hot'; -export default async function randomBunny(subreddit: string, sortBy: "new" | "hot" | "top" = 'hot'): Promise { const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json?limit=100`) .then((res) => { return res; diff --git a/tests/helpers/imageHelper.test.ts b/tests/imageHelper.test.ts similarity index 98% rename from tests/helpers/imageHelper.test.ts rename to tests/imageHelper.test.ts index 3c6f44e..77227b1 100644 --- a/tests/helpers/imageHelper.test.ts +++ b/tests/imageHelper.test.ts @@ -1,4 +1,4 @@ -import ImageHelper from "../../src/helpers/imageHelper"; +import ImageHelper from "../src/imageHelper"; import fetch from "got-cjs"; jest.mock('got-cjs'); diff --git a/tests/index.test.ts b/tests/index.test.ts index ffa9015..053f347 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -1,6 +1,6 @@ import { ErrorCode } from "../src/constants/ErrorCode"; import ErrorMessages from "../src/constants/ErrorMessages"; -import ImageHelper from "../src/helpers/imageHelper"; +import ImageHelper from "../src/imageHelper"; import randomBunny from "../src/index"; import fetch from "got-cjs"; @@ -37,7 +37,7 @@ describe('randomBunny', () => { expect(result.Result).toBeDefined(); expect(result.Error).toBeUndefined(); - expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100'); + expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100'); }); test('GIVEN sortBy is NOT supplied, expect it to default to hot', async () => { @@ -69,7 +69,39 @@ describe('randomBunny', () => { expect(result.Result).toBeDefined(); expect(result.Error).toBeUndefined(); - expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/hot.json?limit=100'); + expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json?limit=100'); + }); + + test('GIVEN sortBy is NOT valid, expect it to default to hot', async () => { + fetchMock.mockResolvedValue({ + body: JSON.stringify({ + data: { + children: [ + { + data: { + archived: false, + downs: 0, + hidden: false, + permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/', + subreddit: 'Rabbits', + subreddit_subscribers: 298713, + title: 'Someone told pickles it’s Monday… *internal fury*', + ups: 1208, + url: 'https://i.redd.it/cr8xudsnkgua1.jpg', + }, + }, + ], + } + }), + }); + + const result = await randomBunny('rabbits', 'invalid'); + + expect(result.IsSuccess).toBeTruthy(); + expect(result.Result).toBeDefined(); + expect(result.Error).toBeUndefined(); + + expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json?limit=100'); }); test('GIVEN the fetch fails, EXPECT failure result', async () => { @@ -84,7 +116,7 @@ describe('randomBunny', () => { expect(result.Error!.Code).toBe(ErrorCode.FailedToFetchReddit); expect(result.Error!.Message).toBe(ErrorMessages.FailedToFetchReddit); - expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100'); + expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100'); }); test('GIVEN the result is NOT valid JSON, EXPECT failure result', async () => { @@ -101,7 +133,7 @@ describe('randomBunny', () => { expect(result.Error!.Code).toBe(ErrorCode.UnableToParseJSON); expect(result.Error!.Message).toBe(ErrorMessages.UnableToParseJSON); - expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100'); + expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100'); }); test('GIVEN randomSelect does NOT find a response, EXPECT failure result', async () => { @@ -157,8 +189,8 @@ describe('randomBunny', () => { expect(result.Error!.Code).toBe(ErrorCode.NoImageResultsFound); expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound); - expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100'); - expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100'); + expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100'); + expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100'); }); test("GIVEN data fetched is a gallery AND an image is returned from the helper, EXPECT this to be used", async () => { @@ -191,7 +223,7 @@ describe('randomBunny', () => { expect(result.IsSuccess).toBeTruthy(); expect(result.Result).toBeDefined(); - expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100'); + expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100'); expect(ImageHelper.FetchImageFromRedditGallery).toHaveBeenCalledTimes(1); expect(ImageHelper.FetchImageFromRedditGallery).toHaveBeenCalledWith("https://i.redd.it/gallery/cr8xudsnkgua1");