Skip to content

Commit

Permalink
changing default currency file (#3414)
Browse files Browse the repository at this point in the history
* changing default currency file

* using defaultRate in the case of rate file not returning

* Adding date macro

* added unit tests for date macro support in currency file url, and small fix to date macro in currency.js

* replaced string concatenations with string literals

* removed unnecessary parens
  • Loading branch information
bretg authored and mkendall07 committed Jan 9, 2019
1 parent 90656ee commit 4b53954
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
29 changes: 25 additions & 4 deletions modules/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as utils from 'src/utils';
import { config } from 'src/config';
import { hooks } from 'src/hook.js';

const DEFAULT_CURRENCY_RATE_URL = 'https://currency.prebid.org/latest.json';
const DEFAULT_CURRENCY_RATE_URL = 'https://cdn.jsdelivr.net/gh/prebid/currency-file@1/latest.json?date=$$TODAY$$';
const CURRENCY_RATE_PRECISION = 4;

var bidResponseQueue = [];
Expand Down Expand Up @@ -34,7 +34,7 @@ var defaultRates;
* {
* rubicon: 'USD'
* }
* @param {string} [config.conversionRateFile = 'http://currency.prebid.org/latest.json']
* @param {string} [config.conversionRateFile = 'URL pointing to conversion file']
* Optional path to a file containing currency conversion data. Prebid.org hosts a file that is used as the default,
* if not specified.
* @param {object} [config.rates]
Expand All @@ -60,6 +60,10 @@ export function setConfig(config) {

if (typeof config.defaultRates === 'object') {
defaultRates = config.defaultRates;

// set up the default rates to be used if the rate file doesn't get loaded in time
currencyRates.conversions = defaultRates;
currencyRatesLoaded = true;
}

if (typeof config.adServerCurrency === 'string') {
Expand All @@ -70,6 +74,25 @@ export function setConfig(config) {
utils.logInfo('currency using override conversionRateFile:', config.conversionRateFile);
url = config.conversionRateFile;
}

// see if the url contains a date macro
// this is a workaround to the fact that jsdelivr doesn't currently support setting a 24-hour HTTP cache header
// So this is an approach to let the browser cache a copy of the file each day
// We should remove the macro once the CDN support a day-level HTTP cache setting
const macroLocation = url.indexOf('$$TODAY$$');
if (macroLocation !== -1) {
// get the date to resolve the macro
const d = new Date();
let month = `${d.getMonth() + 1}`;
let day = `${d.getDate()}`;
if (month.length < 2) month = `0${month}`;
if (day.length < 2) day = `0${day}`;
const todaysDate = `${d.getFullYear()}${month}${day}`;

// replace $$TODAY$$ with todaysDate
url = `${url.substring(0, macroLocation)}${todaysDate}${url.substring(macroLocation + 9, url.length)}`;
}

initCurrency(url);
} else {
// currency support is disabled, setting defaults
Expand All @@ -84,8 +107,6 @@ config.getConfig('currency', config => setConfig(config.currency));

function errorSettingsRates(msg) {
if (defaultRates) {
currencyRates.conversions = defaultRates;
currencyRatesLoaded = true;
utils.logWarn(msg);
utils.logWarn('Currency failed loading rates, falling back to currency.defaultRates');
} else {
Expand Down
46 changes: 46 additions & 0 deletions test/spec/modules/currency_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var expect = require('chai').expect;

describe('currency', function () {
let fakeCurrencyFileServer;
let sandbox;
let clock;

let fn = sinon.spy();
let hookFn = createHook('asyncSeries', fn, 'addBidResponse');
Expand All @@ -30,6 +32,16 @@ describe('currency', function () {
});

describe('setConfig', function () {
beforeEach(function() {
sandbox = sinon.sandbox.create();
clock = sinon.useFakeTimers(1047010195974);
});

afterEach(function () {
sandbox.restore();
clock.restore();
});

it('results in currencySupportEnabled = false when currency not configured', function () {
setConfig({});
expect(currencySupportEnabled).to.equal(false);
Expand All @@ -42,6 +54,40 @@ describe('currency', function () {
expect(currencyRates.dataAsOf).to.equal('2017-04-25');
expect(currencySupportEnabled).to.equal(true);
});

it('date macro token $$TODAY$$ is replaced by current date (formatted as yyyymmdd)', function () {
// RESET to request currency file (specifically url value for this test)
setConfig({ 'adServerCurrency': undefined });

// date macro should replace $$TODAY$$ with date when DEFAULT_CURRENCY_RATE_URL is used
setConfig({ 'adServerCurrency': 'JPY' });
fakeCurrencyFileServer.respond();
expect(fakeCurrencyFileServer.requests[0].url).to.equal('https://cdn.jsdelivr.net/gh/prebid/currency-file@1/latest.json?date=20030306');

// date macro should not modify 'conversionRateFile' if TOKEN is not found
setConfig({
'adServerCurrency': 'JPY',
'conversionRateFile': 'http://test.net/currency.json?date=foobar'
});
fakeCurrencyFileServer.respond();
expect(fakeCurrencyFileServer.requests[1].url).to.equal('http://test.net/currency.json?date=foobar');

// date macro should replace $$TODAY$$ with date for 'conversionRateFile' is configured
setConfig({
'adServerCurrency': 'JPY',
'conversionRateFile': 'http://test.net/currency.json?date=$$TODAY$$'
});
fakeCurrencyFileServer.respond();
expect(fakeCurrencyFileServer.requests[2].url).to.equal('http://test.net/currency.json?date=20030306');

// MULTIPLE TOKENS used in a url is not supported. Only the TOKEN at left-most position is REPLACED
setConfig({
'adServerCurrency': 'JPY',
'conversionRateFile': 'http://test.net/$$TODAY$$/currency.json?date=$$TODAY$$'
});
fakeCurrencyFileServer.respond();
expect(fakeCurrencyFileServer.requests[3].url).to.equal('http://test.net/20030306/currency.json?date=$$TODAY$$');
});
});

describe('bidder override', function () {
Expand Down

0 comments on commit 4b53954

Please sign in to comment.