Compare commits
No commits in common. "f315361278476000034342844007b6bc6cf133b8" and "f53650d573bde5cb259e1d633d2ec556f55c178f" have entirely different histories.
f315361278
...
f53650d573
7 changed files with 74 additions and 52 deletions
30
src/cli.ts
30
src/cli.ts
|
@ -2,8 +2,6 @@ import { Command, Option } from "commander";
|
||||||
import randomBunny from "./index";
|
import randomBunny from "./index";
|
||||||
import ICliOptions from "./contracts/ICliOptions";
|
import ICliOptions from "./contracts/ICliOptions";
|
||||||
import { exit } from "process";
|
import { exit } from "process";
|
||||||
import OutputHelper from "./helpers/outputHelper";
|
|
||||||
import { writeFileSync } from "fs";
|
|
||||||
|
|
||||||
const program = new Command();
|
const program = new Command();
|
||||||
|
|
||||||
|
@ -14,7 +12,6 @@ program
|
||||||
.option('-s, --subreddit <subreddit>', 'The subreddit to search', 'rabbits')
|
.option('-s, --subreddit <subreddit>', 'The subreddit to search', 'rabbits')
|
||||||
.option('-j, --json', 'Output as JSON')
|
.option('-j, --json', 'Output as JSON')
|
||||||
.option('-q, --query-metadata', 'Include query metadata in result')
|
.option('-q, --query-metadata', 'Include query metadata in result')
|
||||||
.option('-o <file>', 'Output to file')
|
|
||||||
.addOption(new Option('--sort <sort>', 'Sort by').default('hot').choices(['hot', 'new', 'top']));
|
.addOption(new Option('--sort <sort>', 'Sort by').default('hot').choices(['hot', 'new', 'top']));
|
||||||
|
|
||||||
program.parse();
|
program.parse();
|
||||||
|
@ -24,14 +21,31 @@ const options: ICliOptions = program.opts();
|
||||||
randomBunny(options.subreddit, options.sort)
|
randomBunny(options.subreddit, options.sort)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
if (response.IsSuccess) {
|
if (response.IsSuccess) {
|
||||||
const output = OutputHelper.GenerateOutput(response, options);
|
const result = response.Result!;
|
||||||
|
|
||||||
if (options.o) {
|
const outputLines: string[] = [];
|
||||||
writeFileSync(options.o, output);
|
|
||||||
} else {
|
if (options.json) {
|
||||||
console.log(output);
|
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);
|
exit(0);
|
||||||
} else {
|
} else {
|
||||||
const error = response.Error!;
|
const error = response.Error!;
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
export default interface ICliOptions {
|
export default interface ICliOptions {
|
||||||
subreddit: string,
|
subreddit: string,
|
||||||
json?: boolean,
|
json?: boolean,
|
||||||
sort: "new" | "hot" | "top",
|
sort: string,
|
||||||
o?: string,
|
|
||||||
queryMetadata?: boolean,
|
queryMetadata?: boolean,
|
||||||
}
|
}
|
|
@ -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");
|
|
||||||
}
|
|
||||||
}
|
|
12
src/index.ts
12
src/index.ts
|
@ -5,9 +5,17 @@ import { List } from 'linqts';
|
||||||
import IFetchResult from "./contracts/IFetchResult";
|
import IFetchResult from "./contracts/IFetchResult";
|
||||||
import { ErrorCode } from "./constants/ErrorCode";
|
import { ErrorCode } from "./constants/ErrorCode";
|
||||||
import ErrorMessages from "./constants/ErrorMessages";
|
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<IReturnResult> {
|
||||||
|
if (!sortable.includes(sortBy)) sortBy = 'hot';
|
||||||
|
|
||||||
export default async function randomBunny(subreddit: string, sortBy: "new" | "hot" | "top" = 'hot'): Promise<IReturnResult> {
|
|
||||||
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json?limit=100`)
|
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json?limit=100`)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
return res;
|
return res;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import ImageHelper from "../../src/helpers/imageHelper";
|
import ImageHelper from "../src/imageHelper";
|
||||||
import fetch from "got-cjs";
|
import fetch from "got-cjs";
|
||||||
|
|
||||||
jest.mock('got-cjs');
|
jest.mock('got-cjs');
|
|
@ -1,6 +1,6 @@
|
||||||
import { ErrorCode } from "../src/constants/ErrorCode";
|
import { ErrorCode } from "../src/constants/ErrorCode";
|
||||||
import ErrorMessages from "../src/constants/ErrorMessages";
|
import ErrorMessages from "../src/constants/ErrorMessages";
|
||||||
import ImageHelper from "../src/helpers/imageHelper";
|
import ImageHelper from "../src/imageHelper";
|
||||||
import randomBunny from "../src/index";
|
import randomBunny from "../src/index";
|
||||||
import fetch from "got-cjs";
|
import fetch from "got-cjs";
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ describe('randomBunny', () => {
|
||||||
expect(result.Result).toBeDefined();
|
expect(result.Result).toBeDefined();
|
||||||
expect(result.Error).toBeUndefined();
|
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 () => {
|
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.Result).toBeDefined();
|
||||||
expect(result.Error).toBeUndefined();
|
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 () => {
|
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!.Code).toBe(ErrorCode.FailedToFetchReddit);
|
||||||
expect(result.Error!.Message).toBe(ErrorMessages.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 () => {
|
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!.Code).toBe(ErrorCode.UnableToParseJSON);
|
||||||
expect(result.Error!.Message).toBe(ErrorMessages.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 () => {
|
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!.Code).toBe(ErrorCode.NoImageResultsFound);
|
||||||
expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound);
|
expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound);
|
||||||
|
|
||||||
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).toHaveBeenCalledWith('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 () => {
|
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.IsSuccess).toBeTruthy();
|
||||||
expect(result.Result).toBeDefined();
|
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).toHaveBeenCalledTimes(1);
|
||||||
expect(ImageHelper.FetchImageFromRedditGallery).toHaveBeenCalledWith("https://i.redd.it/gallery/cr8xudsnkgua1");
|
expect(ImageHelper.FetchImageFromRedditGallery).toHaveBeenCalledWith("https://i.redd.it/gallery/cr8xudsnkgua1");
|
||||||
|
|
Loading…
Reference in a new issue