Skip to content

Commit

Permalink
Changing base must bust cache for OER
Browse files Browse the repository at this point in the history
  • Loading branch information
Cayle Sharrock committed Oct 27, 2017
1 parent 11ce3c5 commit a50edb3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/FXService/providers/OpenExchangeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default class OpenExchangeProvider extends FXProvider {
private pending: Promise<Response> = null;
private cacheDuration: number;
private cacheTimer: Timer = null;
private base: string = null;

constructor(config: OpenExchangeConfig) {
super(config);
Expand Down Expand Up @@ -71,10 +72,11 @@ export default class OpenExchangeProvider extends FXProvider {
base: pair.from,
app_id: this.apiKey
};
if (this.pending === null) {
if (this.needsRequest(pair.from)) {
this.pending = request.get(API_URL + '/latest.json')
.accept('application/json')
.query(query);
this.base = pair.from;
}
return this.pending.then((res: Response) => {
const result = res.body;
Expand All @@ -96,6 +98,12 @@ export default class OpenExchangeProvider extends FXProvider {
});
});
}

private needsRequest(from: string): boolean {
return (this.pending === null) ||
(this.base === null) ||
(this.base !== from);
}
}

function isSupported(pair: CurrencyPair): boolean {
Expand Down
15 changes: 15 additions & 0 deletions test/FXService/OpenExchangeRateTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,19 @@ describe('OpenExchangeProvider', () => {
assert.ok(err instanceof EFXRateUnavailable);
});
});

it('busts cache for base changes', () => {
nock('https://openexchangerates.org/api')
.get('/latest.json')
.query({ app_id: 'key', base: 'GBP' })
.reply(200, {
base: 'GBP',
rates: { USD: 1.25 }
});
return provider.fetchCurrentRate({ from: 'GBP', to: 'USD' }).then((result: FXObject) => {
assert.equal(result.rate.toNumber(), 1.25);
assert.equal(result.from, 'GBP');
assert.equal(result.to, 'USD');
});
});
});

0 comments on commit a50edb3

Please sign in to comment.