Refactored duplicated fetch method out to new class

master
Dolan Miu 2017-05-06 16:53:12 +01:00
parent 5026bdb36f
commit 90e051136b
5 changed files with 62 additions and 132 deletions

View File

@ -2,10 +2,12 @@ import { Request, Response, Router } from "express";
import * as request from "request";
import { ReferenceRepo, RepoFileFetcher } from "../../github";
import { ColorConverter } from "../color-converter";
import { Fetcher } from "../fetcher";
export class AwardsRouter {
public router: Router;
private referenceRepo: ReferenceRepo;
private fetcher: Fetcher;
constructor(private repoFileFetcher: RepoFileFetcher) {
this.router = Router();
@ -16,6 +18,8 @@ export class AwardsRouter {
});
this.referenceRepo.fetch();
this.fetcher = new Fetcher(this.repoFileFetcher, this.referenceRepo);
this.init();
}
@ -24,7 +28,7 @@ export class AwardsRouter {
const owner = req.params.owner as string;
const repo = req.params.repo as string;
this.fetchDetails(owner, repo).then((result) => {
this.fetcher.fetchDetails(owner, repo).then((result) => {
const color = ColorConverter.percentageToHexColor(result.percentage);
request(`https://img.shields.io/badge/wards%20mod%20completion-${result.percentage}%25-${color}.svg`).pipe(res);
}).catch((error) => {
@ -36,42 +40,11 @@ export class AwardsRouter {
const owner = req.params.owner as string;
const repo = req.params.repo as string;
this.fetchDetails(owner, repo).then((result) => {
this.fetcher.fetchDetails(owner, repo).then((result) => {
res.status(200).json(result);
}).catch((error) => {
res.status(400).json({ message: `${repo} is not a mod!` });
});
});
}
private fetchDetails(owner: string, repo: string, path: string = ""): Promise<ComputationSummary> {
return new Promise<ComputationSummary>((resolve, reject) => {
this.repoFileFetcher.fetch({
owner: owner,
repo: repo,
path: path,
}).then((files) => {
this.referenceRepo.fetch().then((fileMap) => {
let successfulFiles: number = 0;
const missingFiles: string[] = new Array<string>();
for (const file of fileMap.values()) {
if (files.has(file.name)) {
successfulFiles++;
continue;
}
missingFiles.push(file.name);
}
resolve({
percentage: parseFloat(Math.round((successfulFiles / fileMap.size) * 100).toPrecision(2)),
numberTexturesMissing: fileMap.size - successfulFiles,
missingFiles: missingFiles,
});
});
}).catch((error) => {
reject(error);
});
});
}
}

View File

@ -2,10 +2,12 @@ import { Request, Response, Router } from "express";
import * as request from "request";
import { ReferenceRepo, RepoFileFetcher } from "../../github";
import { ColorConverter } from "../color-converter";
import { Fetcher } from "../fetcher";
export class FarmingRouter {
public router: Router;
private referenceRepo: ReferenceRepo;
private fetcher: Fetcher;
constructor(private repoFileFetcher: RepoFileFetcher) {
this.router = Router();
@ -16,6 +18,8 @@ export class FarmingRouter {
});
this.referenceRepo.fetch();
this.fetcher = new Fetcher(this.repoFileFetcher, this.referenceRepo);
this.init();
}
@ -24,7 +28,7 @@ export class FarmingRouter {
const owner = req.params.owner as string;
const repo = req.params.repo as string;
this.fetchDetails(owner, repo).then((result) => {
this.fetcher.fetchDetails(owner, repo).then((result) => {
const color = ColorConverter.percentageToHexColor(result.percentage);
request(`https://img.shields.io/badge/farming%20mod%20completion-${result.percentage}%25-${color}.svg`).pipe(res);
}).catch((error) => {
@ -36,42 +40,11 @@ export class FarmingRouter {
const owner = req.params.owner as string;
const repo = req.params.repo as string;
this.fetchDetails(owner, repo).then((result) => {
this.fetcher.fetchDetails(owner, repo).then((result) => {
res.status(200).json(result);
}).catch((error) => {
res.status(400).json({ message: `${repo} is not a mod!` });
});
});
}
private fetchDetails(owner: string, repo: string, path: string = ""): Promise<ComputationSummary> {
return new Promise<ComputationSummary>((resolve, reject) => {
this.repoFileFetcher.fetch({
owner: owner,
repo: repo,
path: path,
}).then((files) => {
this.referenceRepo.fetch().then((fileMap) => {
let successfulFiles: number = 0;
const missingFiles: string[] = new Array<string>();
for (const file of fileMap.values()) {
if (files.has(file.name)) {
successfulFiles++;
continue;
}
missingFiles.push(file.name);
}
resolve({
percentage: parseFloat(Math.round((successfulFiles / fileMap.size) * 100).toPrecision(2)),
numberTexturesMissing: fileMap.size - successfulFiles,
missingFiles: missingFiles,
});
});
}).catch((error) => {
reject(error);
});
});
}
}

View File

@ -0,0 +1,38 @@
import { ReferenceRepo, RepoFileFetcher } from "../github";
export class Fetcher {
constructor(private repoFileFetcher: RepoFileFetcher, private referenceRepo: ReferenceRepo) {
}
public fetchDetails(owner: string, repo: string, path: string = ""): Promise<ComputationSummary> {
return new Promise<ComputationSummary>((resolve, reject) => {
this.repoFileFetcher.fetch({
owner: owner,
repo: repo,
path: path,
}).then((files) => {
this.referenceRepo.fetch().then((fileMap) => {
let successfulFiles: number = 0;
const missingFiles: string[] = new Array<string>();
for (const file of fileMap.values()) {
if (files.has(file.name)) {
successfulFiles++;
continue;
}
missingFiles.push(file.name);
}
resolve({
percentage: parseFloat(Math.round((successfulFiles / fileMap.size) * 100).toPrecision(2)),
numberTexturesMissing: fileMap.size - successfulFiles,
missingFiles: missingFiles,
});
});
}).catch((error) => {
reject(error);
});
});
}
}

View File

@ -2,10 +2,12 @@ import { Request, Response, Router } from "express";
import * as request from "request";
import { ReferenceRepo, RepoFileFetcher } from "../../github";
import { ColorConverter } from "../color-converter";
import { Fetcher } from "../fetcher";
export class NetherRouter {
public router: Router;
private referenceRepo: ReferenceRepo;
private fetcher: Fetcher;
constructor(private repoFileFetcher: RepoFileFetcher) {
this.router = Router();
@ -16,6 +18,8 @@ export class NetherRouter {
});
this.referenceRepo.fetch();
this.fetcher = new Fetcher(this.repoFileFetcher, this.referenceRepo);
this.init();
}
@ -24,7 +28,7 @@ export class NetherRouter {
const owner = req.params.owner as string;
const repo = req.params.repo as string;
this.fetchDetails(owner, repo).then((result) => {
this.fetcher.fetchDetails(owner, repo).then((result) => {
const color = ColorConverter.percentageToHexColor(result.percentage);
request(`https://img.shields.io/badge/nether%20completion-${result.percentage}%25-${color}.svg`).pipe(res);
}).catch((error) => {
@ -36,42 +40,11 @@ export class NetherRouter {
const owner = req.params.owner as string;
const repo = req.params.repo as string;
this.fetchDetails(owner, repo).then((result) => {
this.fetcher.fetchDetails(owner, repo).then((result) => {
res.status(200).json(result);
}).catch((error) => {
res.status(400).json({ message: `${repo} is not a mod!` });
});
});
}
private fetchDetails(owner: string, repo: string, path: string = ""): Promise<ComputationSummary> {
return new Promise<ComputationSummary>((resolve, reject) => {
this.repoFileFetcher.fetch({
owner: owner,
repo: repo,
path: path,
}).then((files) => {
this.referenceRepo.fetch().then((fileMap) => {
let successfulFiles: number = 0;
const missingFiles: string[] = new Array<string>();
for (const file of fileMap.values()) {
if (files.has(file.name)) {
successfulFiles++;
continue;
}
missingFiles.push(file.name);
}
resolve({
percentage: parseFloat(Math.round((successfulFiles / fileMap.size) * 100).toPrecision(2)),
numberTexturesMissing: fileMap.size - successfulFiles,
missingFiles: missingFiles,
});
});
}).catch((error) => {
reject(error);
});
});
}
}

View File

@ -2,10 +2,12 @@ import { Request, Response, Router } from "express";
import * as request from "request";
import { ReferenceRepo, RepoFileFetcher } from "../../github";
import { ColorConverter } from "../color-converter";
import { Fetcher } from "../fetcher";
export class TexturePackRouter {
public router: Router;
private referenceRepo: ReferenceRepo;
private fetcher: Fetcher;
constructor(private repoFileFetcher: RepoFileFetcher) {
this.router = Router();
@ -16,6 +18,8 @@ export class TexturePackRouter {
});
this.referenceRepo.fetch();
this.fetcher = new Fetcher(this.repoFileFetcher, this.referenceRepo);
this.init();
}
@ -24,7 +28,7 @@ export class TexturePackRouter {
const owner = req.params.owner as string;
const repo = req.params.repo as string;
this.fetchDetails(owner, repo).then((result) => {
this.fetcher.fetchDetails(owner, repo).then((result) => {
const color = ColorConverter.percentageToHexColor(result.percentage);
request(`https://img.shields.io/badge/texture%20pack%20completion-${result.percentage}%25-${color}.svg`).pipe(res);
});
@ -34,40 +38,9 @@ export class TexturePackRouter {
const owner = req.params.owner as string;
const repo = req.params.repo as string;
this.fetchDetails(owner, repo).then((result) => {
this.fetcher.fetchDetails(owner, repo).then((result) => {
res.status(200).json(result);
});
});
}
private fetchDetails(owner: string, repo: string, path: string = ""): Promise<ComputationSummary> {
return new Promise<ComputationSummary>((resolve, reject) => {
this.repoFileFetcher.fetch({
owner: owner,
repo: repo,
path: path,
}).then((files) => {
this.referenceRepo.fetch().then((fileMap) => {
let successfulFiles: number = 0;
const missingFiles: string[] = new Array<string>();
for (const file of fileMap.values()) {
if (files.has(file.name)) {
successfulFiles++;
continue;
}
missingFiles.push(file.name);
}
resolve({
percentage: parseFloat(Math.round((successfulFiles / fileMap.size) * 100).toPrecision(2)),
numberTexturesMissing: fileMap.size - successfulFiles,
missingFiles: missingFiles,
});
});
}).catch((error) => {
reject(error);
});
});
}
}