Skip to content

Commit

Permalink
- use abstract base of overview services to avoid duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyusung4698 committed Jul 28, 2020
1 parent 63c2dd1 commit e306a7f
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,36 @@ import { HttpClientModule } from '@angular/common/http';
import { TestBed } from '@angular/core/testing';
import { BrowserModule } from '@angular/platform-browser';
import { AssetService } from '@app/assets';
import { CurrencyOverviewHttpService } from './currency-overview-http.service';
import { TradeLeaguesHttpLeague } from '@data/poe/schema';
import { CurrencyOverviewHttpService } from './currency-overview-http.service';



fdescribe('CurrencyOverviewHttpService', () => {
describe('CurrencyOverviewHttpService', () => {
let sut: CurrencyOverviewHttpService;

beforeEach(async () => {
TestBed.configureTestingModule({
imports: [
CommonModule,
HttpClientModule,
BrowserModule
]
}).compileComponents();
sut = TestBed.inject<CurrencyOverviewHttpService>(CurrencyOverviewHttpService);
const asset = TestBed.inject<AssetService>(AssetService);
await asset.load().toPromise();
});
TestBed.configureTestingModule({
imports: [
CommonModule,
HttpClientModule,
BrowserModule
]
}).compileComponents();
sut = TestBed.inject<CurrencyOverviewHttpService>(CurrencyOverviewHttpService);
const asset = TestBed.inject<AssetService>(AssetService);
await asset.load().toPromise();
});

const leagueMap = {
[TradeLeaguesHttpLeague.Standard]: 'standard',
[TradeLeaguesHttpLeague.HardCore]: 'hardcore',
['Harvest']: 'challenge',
['Hardcore Harvest']: 'challengehc',
[TradeLeaguesHttpLeague.Standard]: 'standard',
[TradeLeaguesHttpLeague.Hardcore]: 'hardcore',
['Harvest']: 'challenge',
['Hardcore Harvest']: 'challengehc',
};

Object.keys(leagueMap).forEach(key => {
it(`getLeaguePath('${key}') should be '${leagueMap[key]}'`, () => {
const result = sut["getLeaguePath"](key);
expect(result).toBe(leagueMap[key]);
});
}
);
it(`getLeaguePath('${key}') should be '${leagueMap[key]}'`, () => {
const result = sut.getLeaguePath(key);
expect(result).toBe(leagueMap[key]);
});
});
});
23 changes: 5 additions & 18 deletions src/app/data/poe-ninja/service/currency-overview-http.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { environment } from '@env/environment';
import { Observable, of, throwError } from 'rxjs';
import { delay, flatMap, retryWhen } from 'rxjs/operators';
import { CurrencyOverviewResponse } from '../schema/currency-overview';
import { OverviewHttpService } from './overview-http.service';

export enum CurrencyOverviewType {
Currency = 'Currency',
Expand All @@ -22,11 +23,13 @@ const RETRY_DELAY = 100;
@Injectable({
providedIn: 'root'
})
export class CurrencyOverviewHttpService {
export class CurrencyOverviewHttpService extends OverviewHttpService {
private readonly baseUrl: string;

constructor(
private readonly httpClient: HttpClient) {
private readonly httpClient: HttpClient
) {
super();
this.baseUrl = `${environment.poeNinja.baseUrl}/api/data/currencyoverview`;
}

Expand Down Expand Up @@ -65,20 +68,4 @@ export class CurrencyOverviewHttpService {
private getUrl(leagueId: string, type: CurrencyOverviewType): string {
return `${this.baseUrl}?league=${encodeURIComponent(leagueId)}&type=${encodeURIComponent(type)}&language=en`;
}

private getLeaguePath(leagueId: string): string {
switch (leagueId) {
case TradeLeaguesHttpLeague.Standard:
return 'standard';
case TradeLeaguesHttpLeague.HardCore:
return 'hardcore';
}

const exp = new RegExp(`${TradeLeaguesHttpLeague.HardCore} .*`);
const regexResult = exp.exec(leagueId);
if (regexResult !== null) {
return 'challengehc';
}
return 'challenge';
}
}
47 changes: 22 additions & 25 deletions src/app/data/poe-ninja/service/item-overview-http.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,36 @@ import { HttpClientModule } from '@angular/common/http';
import { TestBed } from '@angular/core/testing';
import { BrowserModule } from '@angular/platform-browser';
import { AssetService } from '@app/assets';
import { ItemOverviewHttpService } from './item-overview-http.service';
import { TradeLeaguesHttpLeague } from '@data/poe/schema';
import { ItemOverviewHttpService } from './item-overview-http.service';



fdescribe('ItemOverviewHttpService', () => {
describe('ItemOverviewHttpService', () => {
let sut: ItemOverviewHttpService;

beforeEach(async () => {
TestBed.configureTestingModule({
imports: [
CommonModule,
HttpClientModule,
BrowserModule
]
}).compileComponents();
sut = TestBed.inject<ItemOverviewHttpService>(ItemOverviewHttpService);
const asset = TestBed.inject<AssetService>(AssetService);
await asset.load().toPromise();
});
TestBed.configureTestingModule({
imports: [
CommonModule,
HttpClientModule,
BrowserModule
]
}).compileComponents();
sut = TestBed.inject<ItemOverviewHttpService>(ItemOverviewHttpService);
const asset = TestBed.inject<AssetService>(AssetService);
await asset.load().toPromise();
});

const leagueMap = {
[TradeLeaguesHttpLeague.Standard]: 'standard',
[TradeLeaguesHttpLeague.HardCore]: 'hardcore',
['Harvest']: 'challenge',
['Hardcore Harvest']: 'challengehc',
[TradeLeaguesHttpLeague.Standard]: 'standard',
[TradeLeaguesHttpLeague.Hardcore]: 'hardcore',
['Harvest']: 'challenge',
['Hardcore Harvest']: 'challengehc',
};

Object.keys(leagueMap).forEach(key => {
it(`getLeaguePath('${key}') should be '${leagueMap[key]}'`, () => {
const result = sut["getLeaguePath"](key);
expect(result).toBe(leagueMap[key]);
});
}
);
it(`getLeaguePath('${key}') should be '${leagueMap[key]}'`, () => {
const result = sut.getLeaguePath(key);
expect(result).toBe(leagueMap[key]);
});
});
});
23 changes: 5 additions & 18 deletions src/app/data/poe-ninja/service/item-overview-http.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { environment } from '@env/environment';
import { Observable, of, throwError } from 'rxjs';
import { delay, flatMap, retryWhen } from 'rxjs/operators';
import { ItemOverviewResponse } from '../schema/item-overview';
import { OverviewHttpService } from './overview-http.service';

export enum ItemOverviewType {
Prophecy = 'Prophecy',
Expand Down Expand Up @@ -52,11 +53,13 @@ const RETRY_DELAY = 100;
@Injectable({
providedIn: 'root'
})
export class ItemOverviewHttpService {
export class ItemOverviewHttpService extends OverviewHttpService {
private readonly baseUrl: string;

constructor(
private readonly httpClient: HttpClient) {
private readonly httpClient: HttpClient
) {
super();
this.baseUrl = `${environment.poeNinja.baseUrl}/api/data/itemoverview`;
}

Expand Down Expand Up @@ -95,20 +98,4 @@ export class ItemOverviewHttpService {
private getUrl(leagueId: string, type: ItemOverviewType): string {
return `${this.baseUrl}?league=${encodeURIComponent(leagueId)}&type=${encodeURIComponent(type)}&language=en`;
}

private getLeaguePath(leagueId: string): string {
switch (leagueId) {
case TradeLeaguesHttpLeague.Standard:
return 'standard';
case TradeLeaguesHttpLeague.HardCore:
return 'hardcore';
}

const exp = new RegExp(`${TradeLeaguesHttpLeague.HardCore} .*`);
const regexResult = exp.exec(leagueId);
if (regexResult !== null) {
return 'challengehc';
}
return 'challenge';
}
}
18 changes: 18 additions & 0 deletions src/app/data/poe-ninja/service/overview-http.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TradeLeaguesHttpLeague } from '@data/poe/schema';

export abstract class OverviewHttpService {
protected getLeaguePath(leagueId: string): string {
switch (leagueId) {
case TradeLeaguesHttpLeague.Standard:
return 'standard';
case TradeLeaguesHttpLeague.Hardcore:
return 'hardcore';
default:
const exp = new RegExp(`${TradeLeaguesHttpLeague.Hardcore} .*`);
if (exp.exec(leagueId)) {
return 'challengehc';
}
return 'challenge';
}
}
}
2 changes: 1 addition & 1 deletion src/app/data/poe/schema/trade-leagues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TradeHttpResponse } from './trade';

export enum TradeLeaguesHttpLeague {
Standard = 'Standard',
HardCore = 'Hardcore',
Hardcore = 'Hardcore',
}

export interface TradeLeaguesHttpResult {
Expand Down
11 changes: 5 additions & 6 deletions src/app/shared/module/poe/price/item-price-rates.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ export class ItemPriceRatesProvider {
case ItemCategory.GemActivegem:
case ItemCategory.GemSupportGem:
case ItemCategory.GemSupportGemplus: {
const key = `${leagueId}_${ItemCategory.Gem}`;
return this.fetch(key, () => this.fetchItem(leagueId, ItemOverviewType.SkillGem));
}
const key = `${leagueId}_${ItemCategory.Gem}`;
return this.fetch(key, () => this.fetchItem(leagueId, ItemOverviewType.SkillGem));
}
case ItemCategory.MapScarab:
case ItemCategory.Leaguestone:
case ItemCategory.MonsterSample:
Expand Down Expand Up @@ -170,7 +170,7 @@ export class ItemPriceRatesProvider {
change: sparkLine.totalChange,
history: sparkLine.data,
chaosAmount: line.chaosEquivalent,
url: response.url + this.getUrlSuffix(line.currencyTypeName)
url: `${response.url}${this.getUrlSuffix(line.currencyTypeName)}`
};
return rate;
})
Expand Down Expand Up @@ -199,7 +199,7 @@ export class ItemPriceRatesProvider {
change: sparkLine.totalChange,
history: sparkLine.data,
chaosAmount: line.chaosValue,
url: response.url + this.getUrlSuffix(line.name)
url: `${response.url}${this.getUrlSuffix(line.name)}`
};
return rate;
})
Expand All @@ -211,5 +211,4 @@ export class ItemPriceRatesProvider {
private getUrlSuffix(name: string): string {
return `?name=${encodeURIComponent(name)}`;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { AssetService } from '@app/assets';
import { TradeChatParserService } from '.';
import { TradeParserType } from './trade-chat';

fdescribe('TradeChatParserService', () => {
describe('TradeChatParserService', () => {
let sut: TradeChatParserService;
beforeEach(async () => {
TestBed.configureTestingModule({
Expand Down

0 comments on commit e306a7f

Please sign in to comment.