Skip to content

Commit

Permalink
Replace usePrebidCache with cache:url and remove default (#1904)
Browse files Browse the repository at this point in the history
* replace usePrebidCache with video:cacheUrl and remove default

* remove newConfig in dfpAdServerVideo_spec that isn't doing anything

* change video.cacheUrl to cache.url

* update to cache.url in auction and remove from adapter
  • Loading branch information
snapwich authored and matthewlane committed Dec 6, 2017
1 parent aa7178b commit d33c64d
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 130 deletions.
2 changes: 1 addition & 1 deletion modules/dfpAdServerVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function buildUrlFromAdserverUrlComponents(components, bid) {
* @return {string | undefined} The encoded vast url if it exists, or undefined
*/
function getDescriptionUrl(bid, components, prop) {
if (config.getConfig('usePrebidCache')) { return; }
if (config.getConfig('cache.url')) { return; }

if (!deepAccess(components, `${prop}.description_url`)) {
const vastUrl = bid && bid.vastUrl;
Expand Down
4 changes: 2 additions & 2 deletions src/auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels})
});
request.doneCbCallCount += 1;
// In case of mediaType video and prebidCache enabled, call bidsBackHandler after cache is stored.
if ((request.bids.filter(videoAdUnit).length == 0) || (request.bids.filter(videoAdUnit).length > 0 && !config.getConfig('usePrebidCache'))) {
if ((request.bids.filter(videoAdUnit).length == 0) || (request.bids.filter(videoAdUnit).length > 0 && !config.getConfig('cache.url'))) {
bidsBackAll()
}
}, 1);
Expand Down Expand Up @@ -238,7 +238,7 @@ export const addBidResponse = createHook('asyncSeries', function(adUnitCode, bid

// Video bids may fail if the cache is down, or there's trouble on the network.
function tryAddVideoBid(bidResponse) {
if (config.getConfig('usePrebidCache')) {
if (config.getConfig('cache.url')) {
store([bidResponse], function(error, cacheIds) {
if (error) {
utils.logWarn(`Failed to save to the video cache: ${error}. Video bid must be discarded.`);
Expand Down
223 changes: 114 additions & 109 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,126 +43,128 @@ const ALL_TOPICS = '*';
/**
* @typedef {object} PrebidConfig
*
* @property {bool} usePrebidCache True if we should use prebid-cache to store video bids before adding
* bids to the auction, and false otherwise. **NOTE** This must be true if you want to use the
* dfpAdServerVideo module.
* @property {string} cache.url Set a url if we should use prebid-cache to store video bids before adding
* bids to the auction. **NOTE** This must be set if you want to use the dfpAdServerVideo module.
*/

export function newConfig() {
let listeners = [];

let defaults = {};

let config = {
// `debug` is equivalent to legacy `pbjs.logging` property
_debug: DEFAULT_DEBUG,
get debug() {
return this._debug;
},
set debug(val) {
this._debug = val;
},

// default timeout for all bids
_bidderTimeout: DEFAULT_BIDDER_TIMEOUT,
get bidderTimeout() {
return this._bidderTimeout;
},
set bidderTimeout(val) {
this._bidderTimeout = val;
},

// domain where prebid is running for cross domain iframe communication
_publisherDomain: DEFAULT_PUBLISHER_DOMAIN,
get publisherDomain() {
return this._publisherDomain;
},
set publisherDomain(val) {
this._publisherDomain = val;
},

// delay to request cookie sync to stay out of critical path
_cookieSyncDelay: DEFAULT_COOKIESYNC_DELAY,
get cookieSyncDelay() {
return $$PREBID_GLOBAL$$.cookieSyncDelay || this._cookieSyncDelay;
},
set cookieSyncDelay(val) {
this._cookieSyncDelay = val;
},

// calls existing function which may be moved after deprecation
_priceGranularity: GRANULARITY_OPTIONS.MEDIUM,
set priceGranularity(val) {
if (validatePriceGranularity(val)) {
if (typeof val === 'string') {
this._priceGranularity = (hasGranularity(val)) ? val : GRANULARITY_OPTIONS.MEDIUM;
} else if (typeof val === 'object') {
this._customPriceBucket = val;
this._priceGranularity = GRANULARITY_OPTIONS.CUSTOM;
utils.logMessage('Using custom price granularity');
let defaults;
let config;

function resetConfig() {
defaults = {};
config = {
// `debug` is equivalent to legacy `pbjs.logging` property
_debug: DEFAULT_DEBUG,
get debug() {
return this._debug;
},
set debug(val) {
this._debug = val;
},

// default timeout for all bids
_bidderTimeout: DEFAULT_BIDDER_TIMEOUT,
get bidderTimeout() {
return this._bidderTimeout;
},
set bidderTimeout(val) {
this._bidderTimeout = val;
},

// domain where prebid is running for cross domain iframe communication
_publisherDomain: DEFAULT_PUBLISHER_DOMAIN,
get publisherDomain() {
return this._publisherDomain;
},
set publisherDomain(val) {
this._publisherDomain = val;
},

// delay to request cookie sync to stay out of critical path
_cookieSyncDelay: DEFAULT_COOKIESYNC_DELAY,
get cookieSyncDelay() {
return $$PREBID_GLOBAL$$.cookieSyncDelay || this._cookieSyncDelay;
},
set cookieSyncDelay(val) {
this._cookieSyncDelay = val;
},

// calls existing function which may be moved after deprecation
_priceGranularity: GRANULARITY_OPTIONS.MEDIUM,
set priceGranularity(val) {
if (validatePriceGranularity(val)) {
if (typeof val === 'string') {
this._priceGranularity = (hasGranularity(val)) ? val : GRANULARITY_OPTIONS.MEDIUM;
} else if (typeof val === 'object') {
this._customPriceBucket = val;
this._priceGranularity = GRANULARITY_OPTIONS.CUSTOM;
utils.logMessage('Using custom price granularity');
}
}
}
},
get priceGranularity() {
return this._priceGranularity;
},

_customPriceBucket: {},
get customPriceBucket() {
return this._customPriceBucket;
},

_sendAllBids: DEFAULT_ENABLE_SEND_ALL_BIDS,
get enableSendAllBids() {
return this._sendAllBids;
},
set enableSendAllBids(val) {
this._sendAllBids = val;
},

_bidderSequence: DEFAULT_BIDDER_SEQUENCE,
get bidderSequence() {
return this._bidderSequence;
},
set bidderSequence(val) {
if (VALID_ORDERS[val]) {
this._bidderSequence = val;
} else {
utils.logWarn(`Invalid order: ${val}. Bidder Sequence was not set.`);
}
},

// timeout buffer to adjust for bidder CDN latency
_timoutBuffer: DEFAULT_TIMEOUTBUFFER,
get timeoutBuffer() {
return this._timoutBuffer;
},
set timeoutBuffer(val) {
this._timoutBuffer = val;
},
},
get priceGranularity() {
return this._priceGranularity;
},

_customPriceBucket: {},
get customPriceBucket() {
return this._customPriceBucket;
},

_sendAllBids: DEFAULT_ENABLE_SEND_ALL_BIDS,
get enableSendAllBids() {
return this._sendAllBids;
},
set enableSendAllBids(val) {
this._sendAllBids = val;
},

_bidderSequence: DEFAULT_BIDDER_SEQUENCE,
get bidderSequence() {
return this._bidderSequence;
},
set bidderSequence(val) {
if (VALID_ORDERS[val]) {
this._bidderSequence = val;
} else {
utils.logWarn(`Invalid order: ${val}. Bidder Sequence was not set.`);
}
},

};
// timeout buffer to adjust for bidder CDN latency
_timoutBuffer: DEFAULT_TIMEOUTBUFFER,
get timeoutBuffer() {
return this._timoutBuffer;
},
set timeoutBuffer(val) {
this._timoutBuffer = val;
},

function hasGranularity(val) {
return find(Object.keys(GRANULARITY_OPTIONS), option => val === GRANULARITY_OPTIONS[option]);
}
};

function validatePriceGranularity(val) {
if (!val) {
utils.logError('Prebid Error: no value passed to `setPriceGranularity()`');
return false;
function hasGranularity(val) {
return find(Object.keys(GRANULARITY_OPTIONS), option => val === GRANULARITY_OPTIONS[option]);
}
if (typeof val === 'string') {
if (!hasGranularity(val)) {
utils.logWarn('Prebid Warning: setPriceGranularity was called with invalid setting, using `medium` as default.');
}
} else if (typeof val === 'object') {
if (!isValidPriceConfig(val)) {
utils.logError('Invalid custom price value passed to `setPriceGranularity()`');

function validatePriceGranularity(val) {
if (!val) {
utils.logError('Prebid Error: no value passed to `setPriceGranularity()`');
return false;
}
if (typeof val === 'string') {
if (!hasGranularity(val)) {
utils.logWarn('Prebid Warning: setPriceGranularity was called with invalid setting, using `medium` as default.');
}
} else if (typeof val === 'object') {
if (!isValidPriceConfig(val)) {
utils.logError('Invalid custom price value passed to `setPriceGranularity()`');
return false;
}
}
return true;
}
return true;
}

/*
Expand Down Expand Up @@ -286,10 +288,13 @@ export function newConfig() {
.forEach(listener => listener.callback(options));
}

resetConfig();

return {
getConfig,
setConfig,
setDefaults
setDefaults,
resetConfig
};
}

Expand Down
8 changes: 4 additions & 4 deletions src/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ export function isValidVideoBid(bid, bidRequests) {
// if context not defined assume default 'instream' for video bids
// instream bids require a vast url or vast xml content
if (!bidRequest || (videoMediaType && context !== OUTSTREAM)) {
// xml-only video bids require prebid-cache to be enabled
if (!config.getConfig('usePrebidCache') && bid.vastXml && !bid.vastUrl) {
// xml-only video bids require a prebid cache url
if (!config.getConfig('cache.url') && bid.vastXml && !bid.vastUrl) {
logError(`
This bid contains only vastXml and will not work when prebid-cache is disabled.
Try enabling prebid-cache with pbjs.setConfig({ usePrebidCache: true });
This bid contains only vastXml and will not work when a prebid cache url is not specified.
Try enabling prebid cache with pbjs.setConfig({ cache: {url: "..."} });
`);
return false;
}
Expand Down
7 changes: 3 additions & 4 deletions src/videoCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
*/

import { ajax } from './ajax';

const BASE_URL = 'https://prebid.adnxs.com/pbc/v1/cache'
import { config } from '../src/config';

/**
* @typedef {object} CacheableUrlBid
Expand Down Expand Up @@ -119,12 +118,12 @@ export function store(bids, done) {
puts: bids.map(toStorageRequest)
};

ajax(BASE_URL, shimStorageCallback(done), JSON.stringify(requestData), {
ajax(config.getConfig('cache.url'), shimStorageCallback(done), JSON.stringify(requestData), {
contentType: 'text/plain',
withCredentials: true
});
}

export function getCacheUrl(id) {
return `${BASE_URL}?uuid=${id}`;
return `${config.getConfig('cache.url')}?uuid=${id}`;
}
4 changes: 0 additions & 4 deletions test/spec/modules/dfpAdServerVideo_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import parse from 'url-parse';
import buildDfpVideoUrl from 'modules/dfpAdServerVideo';
import { parseQS } from 'src/url';
import adUnit from 'test/fixtures/video/adUnit';
import { newConfig } from 'src/config';

const bid = {
videoCacheKey: 'abc',
Expand Down Expand Up @@ -131,9 +130,6 @@ describe('The DFP video support module', () => {
});

it('should not overwrite an existing description_url for object input and cache disabled', () => {
const config = newConfig();
config.setConfig({ usePrebidCache: true });

const bidCopy = Object.assign({}, bid);
bidCopy.vastUrl = 'vastUrl.example';

Expand Down
23 changes: 22 additions & 1 deletion test/spec/videoCache_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'mocha';
import chai from 'chai';
import { getCacheUrl, store } from 'src/videoCache';
import { config } from 'src/config';

const should = chai.should();

Expand Down Expand Up @@ -48,9 +49,17 @@ describe('The video cache', () => {
xhr = sinon.useFakeXMLHttpRequest();
requests = [];
xhr.onCreate = (request) => requests.push(request);
config.setConfig({
cache: {
url: 'https://prebid.adnxs.com/pbc/v1/cache'
}
})
});

afterEach(() => xhr.restore());
afterEach(() => {
xhr.restore();
config.resetConfig();
});

it('should execute the callback with a successful result when store() is called', () => {
const uuid = 'c488b101-af3e-4a99-b538-00423e5a3371';
Expand Down Expand Up @@ -128,6 +137,18 @@ describe('The video cache', () => {
});

describe('The getCache function', () => {
beforeEach(() => {
config.setConfig({
cache: {
url: 'https://prebid.adnxs.com/pbc/v1/cache'
}
})
});

afterEach(() => {
config.resetConfig();
});

it('should return the expected URL', () => {
const uuid = 'c488b101-af3e-4a99-b538-00423e5a3371';
const url = getCacheUrl(uuid);
Expand Down
Loading

0 comments on commit d33c64d

Please sign in to comment.