Skip to content

Commit

Permalink
fix currency to use setConfig and change some variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
snapwich committed Aug 8, 2017
1 parent d30b610 commit 5dfe5d9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
21 changes: 12 additions & 9 deletions modules/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { STATUS } from 'src/constants';
import { ajax } from 'src/ajax';
import * as utils from 'src/utils';
import bidmanager from 'src/bidmanager';
import { config } from 'src/config';

$$PREBID_GLOBAL$$.currency = setConfig;

Expand All @@ -20,10 +21,10 @@ var originalBidResponse;

export var currencySupportEnabled = false;
export var currencyRates = {};
var currencyOverrides = {};
var bidderCurrencyDefault = {};

export function setConfig(config) {
var url = DEFAULT_CURRENCY_RATE_URL;
let url = DEFAULT_CURRENCY_RATE_URL;
if (typeof config.adServerCurrency === 'string') {
utils.logInfo('enabling currency support', arguments);

Expand All @@ -38,10 +39,11 @@ export function setConfig(config) {
utils.logInfo('disabling currency support');
resetCurrency();
}
if (typeof config.currencyOverrides === 'object') {
currencyOverrides = config.currencyOverrides;
if (typeof config.bidderCurrencyDefault === 'object') {
bidderCurrencyDefault = config.bidderCurrencyDefault;
}
}
config.getConfig('currency', config => setConfig(config.currency));

function initCurrency(url) {
conversionCache = {};
Expand Down Expand Up @@ -81,7 +83,7 @@ function resetCurrency() {
currencySupportEnabled = false;
currencyRatesLoaded = false;
currencyRates = {};
currencyOverrides = {};
bidderCurrencyDefault = {};
}

export function addBidResponseDecorator(fn) {
Expand All @@ -91,8 +93,8 @@ export function addBidResponseDecorator(fn) {
}

let bidder = bid.bidderCode || bid.bidder;
if (currencyOverrides[bidder]) {
let override = currencyOverrides[bidder];
if (bidderCurrencyDefault[bidder]) {
let override = bidderCurrencyDefault[bidder];
if (bid.currency && override !== bid.currency) {
utils.logWarn(`Currency override '${bidder}: ${override}' ignored. adapter specified '${bid.currency}'`);
} else {
Expand Down Expand Up @@ -151,6 +153,7 @@ function wrapFunction(fn, context, params) {

function getCurrencyConversion(fromCurrency) {
var conversionRate = null;
var rates;

if (fromCurrency in conversionCache) {
conversionRate = conversionCache[fromCurrency];
Expand All @@ -168,7 +171,7 @@ function getCurrencyConversion(fromCurrency) {

if (fromCurrency in currencyRates.conversions) {
// using direct conversion rate from fromCurrency to toCurrency
var rates = currencyRates.conversions[fromCurrency];
rates = currencyRates.conversions[fromCurrency];
if (!(toCurrency in rates)) {
// bid should fail, currency is not supported
throw new Error('Specified adServerCurrency in config \'' + toCurrency + '\' not found in the currency rates file');
Expand All @@ -177,7 +180,7 @@ function getCurrencyConversion(fromCurrency) {
utils.logInfo('getCurrencyConversion using direct ' + fromCurrency + ' to ' + toCurrency + ' conversionRate ' + conversionRate);
} else if (toCurrency in currencyRates.conversions) {
// using reciprocal of conversion rate from toCurrency to fromCurrency
var rates = currencyRates.conversions[toCurrency];
rates = currencyRates.conversions[toCurrency];
if (!(fromCurrency in rates)) {
// bid should fail, currency is not supported
throw new Error('Specified fromCurrency \'' + fromCurrency + '\' not found in the currency rates file');
Expand Down
18 changes: 13 additions & 5 deletions src/bidmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {getPriceBucketString} from './cpmBucketManager';
import {NATIVE_KEYS, nativeBidIsValid} from './native';
import { store } from './videoCache';
import { Renderer } from 'src/Renderer';
import { config } from './config';

var CONSTANTS = require('./constants.json');
var AUCTION_END = CONSTANTS.EVENTS.AUCTION_END;
Expand All @@ -11,14 +12,21 @@ var events = require('./events');

var externalCallbacks = {byAdUnit: [], all: [], oneTime: null, timer: false};
var _granularity = CONSTANTS.GRANULARITY_OPTIONS.MEDIUM;
var _currencyMultiplier = 1;
let _customPriceBucket;
let _granularityMultiplier = 1;
var defaultBidderSettingsMap = {};

exports.setCustomPriceBucket = function(customConfig) {
_customPriceBucket = customConfig;
};

config.getConfig('currency', config => {
const value = parseFloat(typeof config === 'object' && config.granularityMultiplier);
if (value) {
_granularityMultiplier = value;
}
});

/**
* Returns a list of bidders that we haven't received a response yet
* @return {array} [description]
Expand Down Expand Up @@ -157,7 +165,7 @@ exports.addBidResponse = function (adUnitCode, bid) {
bid.renderer.setRender(adUnitRenderer.render);
}

const priceStringsObj = getPriceBucketString(bid.cpm, _customPriceBucket, _currencyMultiplier);
const priceStringsObj = getPriceBucketString(bid.cpm, _customPriceBucket, _granularityMultiplier);
bid.pbLg = priceStringsObj.low;
bid.pbMg = priceStringsObj.med;
bid.pbHg = priceStringsObj.high;
Expand Down Expand Up @@ -289,7 +297,7 @@ function setKeys(keyValues, bidderSettings, custBidObj) {
return keyValues;
}

exports.setPriceGranularity = function setPriceGranularity(granularity, currencyMultiplier) {
exports.setPriceGranularity = function setPriceGranularity(granularity, granularityMultiplier) {
var granularityOptions = CONSTANTS.GRANULARITY_OPTIONS;
if (Object.keys(granularityOptions).filter(option => granularity === granularityOptions[option])) {
_granularity = granularity;
Expand All @@ -299,8 +307,8 @@ exports.setPriceGranularity = function setPriceGranularity(granularity, currency
_granularity = CONSTANTS.GRANULARITY_OPTIONS.MEDIUM;
}

if (typeof currencyMultiplier !== 'undefined') {
_currencyMultiplier = currencyMultiplier;
if (typeof granularityMultiplier !== 'undefined') {
_granularityMultiplier = granularityMultiplier;
}
};

Expand Down
28 changes: 14 additions & 14 deletions src/cpmBucketManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,23 @@ const _autoPriceConfig = {
}]
};

function getPriceBucketString(cpm, customConfig, currencyMultiplier = 1) {
function getPriceBucketString(cpm, customConfig, granularityMultiplier = 1) {
let cpmFloat = parseFloat(cpm);
if (isNaN(cpmFloat)) {
cpmFloat = '';
}

return {
low: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _lgPriceConfig, currencyMultiplier),
med: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _mgPriceConfig, currencyMultiplier),
high: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _hgPriceConfig, currencyMultiplier),
auto: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _autoPriceConfig, currencyMultiplier),
dense: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _densePriceConfig, currencyMultiplier),
custom: (cpmFloat === '') ? '' : getCpmStringValue(cpm, customConfig, currencyMultiplier)
low: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _lgPriceConfig, granularityMultiplier),
med: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _mgPriceConfig, granularityMultiplier),
high: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _hgPriceConfig, granularityMultiplier),
auto: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _autoPriceConfig, granularityMultiplier),
dense: (cpmFloat === '') ? '' : getCpmStringValue(cpm, _densePriceConfig, granularityMultiplier),
custom: (cpmFloat === '') ? '' : getCpmStringValue(cpm, customConfig, granularityMultiplier)
};
}

function getCpmStringValue(cpm, config, currencyMultiplier = 1) {
function getCpmStringValue(cpm, config, granularityMultiplier) {
let cpmStr = '';
if (!isValidPriceConfig(config)) {
return cpmStr;
Expand All @@ -85,15 +85,15 @@ function getCpmStringValue(cpm, config, currencyMultiplier = 1) {
'max': 0,
});
let bucket = config.buckets.find(bucket => {
if (cpm > cap.max * currencyMultiplier) {
if (cpm > cap.max * granularityMultiplier) {
const precision = bucket.precision || _defaultPrecision;
cpmStr = (bucket.max * currencyMultiplier).toFixed(precision);
} else if (cpm <= bucket.max * currencyMultiplier && cpm >= bucket.min * currencyMultiplier) {
cpmStr = (bucket.max * granularityMultiplier).toFixed(precision);
} else if (cpm <= bucket.max * granularityMultiplier && cpm >= bucket.min * granularityMultiplier) {
return bucket;
}
});
if (bucket) {
cpmStr = getCpmTarget(cpm, bucket.increment, bucket.precision, currencyMultiplier);
cpmStr = getCpmTarget(cpm, bucket.increment, bucket.precision, granularityMultiplier);
}
return cpmStr;
}
Expand All @@ -111,11 +111,11 @@ function isValidPriceConfig(config) {
return isValid;
}

function getCpmTarget(cpm, increment, precision, currencyMultiplier = 1) {
function getCpmTarget(cpm, increment, precision, granularityMultiplier) {
if (!precision) {
precision = _defaultPrecision;
}
let bucketSize = 1 / increment * currencyMultiplier;
let bucketSize = 1 / increment * granularityMultiplier;
return (Math.floor(cpm * bucketSize) / bucketSize).toFixed(precision);
}

Expand Down
6 changes: 3 additions & 3 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,22 +641,22 @@ $$PREBID_GLOBAL$$.aliasBidder = function (bidderCode, alias) {
* { "buckets" : [{"min" : 0,"max" : 20,"increment" : 0.1,"cap" : true}]};
* See http://prebid.org/dev-docs/publisher-api-reference.html#module_pbjs.setPriceGranularity for more details
*/
$$PREBID_GLOBAL$$.setPriceGranularity = function (granularity, currencyMultiplier) {
$$PREBID_GLOBAL$$.setPriceGranularity = function (granularity, granularityMultiplier) {
utils.logInfo('Invoking $$PREBID_GLOBAL$$.setPriceGranularity', arguments);
if (!granularity) {
utils.logError('Prebid Error: no value passed to `setPriceGranularity()`');
return;
}
if (typeof granularity === 'string') {
bidmanager.setPriceGranularity(granularity, currencyMultiplier);
bidmanager.setPriceGranularity(granularity, granularityMultiplier);
}
else if (typeof granularity === 'object') {
if (!isValidPriceConfig(granularity)) {
utils.logError('Invalid custom price value passed to `setPriceGranularity()`');
return;
}
bidmanager.setCustomPriceBucket(granularity);
bidmanager.setPriceGranularity(CONSTANTS.GRANULARITY_OPTIONS.CUSTOM, currencyMultiplier);
bidmanager.setPriceGranularity(CONSTANTS.GRANULARITY_OPTIONS.CUSTOM, granularityMultiplier);
utils.logMessage('Using custom price granularity');
}
};
Expand Down
4 changes: 2 additions & 2 deletions test/spec/modules/currency_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('currency', function () {

setConfig({
adServerCurrency: 'GBP',
currencyOverrides: {
bidderCurrencyDefault: {
rubicon: 'GBP'
}
});
Expand All @@ -66,7 +66,7 @@ describe('currency', function () {

setConfig({
adServerCurrency: 'JPY',
currencyOverrides: {
bidderCurrencyDefault: {
rubicon: 'GBP'
}
});
Expand Down

0 comments on commit 5dfe5d9

Please sign in to comment.