From 4dfc6843f603573d7628af86fe3f93eebbc71920 Mon Sep 17 00:00:00 2001 From: Bret Gorsline Date: Wed, 2 Jan 2019 16:13:53 -0500 Subject: [PATCH 1/6] changing default currency file --- modules/currency.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/currency.js b/modules/currency.js index f66c33bbed8..bbe2fba885d 100644 --- a/modules/currency.js +++ b/modules/currency.js @@ -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'; const CURRENCY_RATE_PRECISION = 4; var bidResponseQueue = []; @@ -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] From d8df9e06c96b4db434ed2d46d70c54b89c819e1a Mon Sep 17 00:00:00 2001 From: Bret Gorsline Date: Wed, 2 Jan 2019 16:49:07 -0500 Subject: [PATCH 2/6] using defaultRate in the case of rate file not returning --- modules/currency.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/currency.js b/modules/currency.js index bbe2fba885d..fa6d58b039f 100644 --- a/modules/currency.js +++ b/modules/currency.js @@ -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') { @@ -84,8 +88,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 { From 0fd09eac50b5da08348cb6a12779fafd668802c7 Mon Sep 17 00:00:00 2001 From: bretg Date: Mon, 7 Jan 2019 18:51:49 -0500 Subject: [PATCH 3/6] Adding date macro --- modules/currency.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/modules/currency.js b/modules/currency.js index fa6d58b039f..88f03dd98fb 100644 --- a/modules/currency.js +++ b/modules/currency.js @@ -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://cdn.jsdelivr.net/gh/prebid/currency-file@1/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 = []; @@ -74,6 +74,26 @@ 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>=0) { + // get the date to resolve the macro + var d = new Date(), + month = '' + (d.getMonth() + 1), + day = '' + d.getDate(), + year = d.getFullYear(); + if (month.length < 2) month = '0' + month; + if (day.length < 2) day = '0' + day; + const todaysDate = [year, month, day].join(''); + + // 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 From 19569092b870539df9d0ca128b3e94bfe693d6e6 Mon Sep 17 00:00:00 2001 From: Isaac Dettman Date: Tue, 8 Jan 2019 22:33:04 -0800 Subject: [PATCH 4/6] added unit tests for date macro support in currency file url, and small fix to date macro in currency.js --- modules/currency.js | 21 +++++++------- test/spec/modules/currency_spec.js | 46 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/modules/currency.js b/modules/currency.js index 88f03dd98fb..79a64d58ab7 100644 --- a/modules/currency.js +++ b/modules/currency.js @@ -74,26 +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>=0) { + const macroLocation = url.indexOf('$$TODAY$$'); + if (macroLocation !== -1) { // get the date to resolve the macro - var d = new Date(), - month = '' + (d.getMonth() + 1), - day = '' + d.getDate(), - year = d.getFullYear(); + 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 = [year, month, day].join(''); - + const todaysDate = [d.getFullYear(), month, day].join(''); + // replace $$TODAY$$ with todaysDate - url=url.substring(0,macroLocation)+todaysDate+url.substring(macroLocation+9,url.length); + url = url.substring(0, macroLocation) + todaysDate + url.substring(macroLocation + 9, url.length); } - + initCurrency(url); } else { // currency support is disabled, setting defaults diff --git a/test/spec/modules/currency_spec.js b/test/spec/modules/currency_spec.js index e96b15d11e9..64c966bdf18 100644 --- a/test/spec/modules/currency_spec.js +++ b/test/spec/modules/currency_spec.js @@ -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'); @@ -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); @@ -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 () { From 7b32efec4c62636f776ebade66d05a06f31c2288 Mon Sep 17 00:00:00 2001 From: Isaac Dettman Date: Wed, 9 Jan 2019 08:49:02 -0800 Subject: [PATCH 5/6] replaced string concatenations with string literals --- modules/currency.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/currency.js b/modules/currency.js index 79a64d58ab7..7e6bc764351 100644 --- a/modules/currency.js +++ b/modules/currency.js @@ -83,14 +83,14 @@ export function setConfig(config) { 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].join(''); + 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); + url = `${url.substring(0, macroLocation)}${todaysDate}${url.substring(macroLocation + 9, url.length)}`; } initCurrency(url); From 3147473f8d95174caae794e970e208a4628313a7 Mon Sep 17 00:00:00 2001 From: Isaac Dettman Date: Wed, 9 Jan 2019 08:54:20 -0800 Subject: [PATCH 6/6] removed unnecessary parens --- modules/currency.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/currency.js b/modules/currency.js index 7e6bc764351..fbb7a0cf30d 100644 --- a/modules/currency.js +++ b/modules/currency.js @@ -83,7 +83,7 @@ export function setConfig(config) { if (macroLocation !== -1) { // get the date to resolve the macro const d = new Date(); - let month = `${(d.getMonth() + 1)}`; + let month = `${d.getMonth() + 1}`; let day = `${d.getDate()}`; if (month.length < 2) month = `0${month}`; if (day.length < 2) day = `0${day}`;