Skip to content

Commit

Permalink
Use changes in filter bids PR
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewlane committed Nov 1, 2016
1 parent 72819c6 commit 18b3861
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 44 deletions.
55 changes: 23 additions & 32 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,33 +154,24 @@ function getPresetTargeting() {
}
}

function getWinningBids(code) {
const getWinningBidForAdUnit = (adUnitCode) => {
const adUnits = $$PREBID_GLOBAL$$._bidsReceived
.filter(bid => bid.adUnitCode === adUnitCode ? bid : null);

if (adUnits.length) {
return adUnits.reduce(getHighestCpm, {
adUnitCode: adUnitCode,
cpm: 0,
adserverTargeting: {},
timeToRespond: 0
});
}
};

if (code) {
return getWinningBidForAdUnit(code);
} else {
return $$PREBID_GLOBAL$$._bidsReceived
.map(bid => bid.adUnitCode)
.filter(uniques)
.map(getWinningBidForAdUnit);
}
function getWinningBids(adUnitCodes) {
return $$PREBID_GLOBAL$$._bidsReceived
.filter(adUnitsFilter.bind(this, adUnitCodes))
.map(bid => bid.adUnitCode)
.filter(uniques)
.map(adUnitCode => $$PREBID_GLOBAL$$._bidsReceived
.filter(bid => bid.adUnitCode === adUnitCode ? bid : null)
.reduce(getHighestCpm,
{
adUnitCode: adUnitCode,
cpm: 0,
adserverTargeting: {},
timeToRespond: 0
}));
}

function getWinningBidTargeting() {
let winners = getWinningBids();
function getWinningBidTargeting(adUnitCodes) {
let winners = getWinningBids(adUnitCodes);

// winning bids with deals need an hb_deal targeting key
winners
Expand Down Expand Up @@ -860,14 +851,14 @@ $$PREBID_GLOBAL$$.setBidderSequence = function (order) {
* Get array of highest cpm bids for all adUnits, or highest cpm bid
* object for the given adUnit
* @param {string} optional adUnitCode ad unit code
* @return {array|object} array or object containing highest cpm bid(s)
* @return {array} array containing highest cpm bid object(s)
*/
$$PREBID_GLOBAL$$.getHighestCpmBid = function (adUnitCode) {
if (adUnitCode) {
return getWinningBids(adUnitCode);
} else {
return getWinningBids();
}
$$PREBID_GLOBAL$$.getHighestCpmBids = function (adUnitCode) {
const adUnitCodes = adUnitCode && adUnitCode.length ?
[adUnitCode] :
$$PREBID_GLOBAL$$._adUnitCodes;

return getWinningBids(adUnitCodes);
};

processQue();
21 changes: 9 additions & 12 deletions test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1263,25 +1263,22 @@ describe('Unit: Prebid Module', function () {
});

describe('getHighestCpm', () => {
it('returns an array of winning bids for each adUnit', () => {
const highestCpmBids = $$PREBID_GLOBAL$$.getHighestCpmBid();

it('returns an array of winning bid objects for each adUnit', () => {
const highestCpmBids = $$PREBID_GLOBAL$$.getHighestCpmBids();
expect(highestCpmBids.length).to.equal(2);
expect(highestCpmBids[0]).to.deep.equal($$PREBID_GLOBAL$$._bidsReceived[1]);
expect(highestCpmBids[1]).to.deep.equal($$PREBID_GLOBAL$$._bidsReceived[2]);
});

it('returns the highest bid for the given adUnitCode', () => {
const highestCpmBid = $$PREBID_GLOBAL$$.getHighestCpmBid('/19968336/header-bid-tag-0');

expect(highestCpmBid).to.be.an('object');
expect(highestCpmBid).to.deep.equal($$PREBID_GLOBAL$$._bidsReceived[1]);
it('returns an array containing the highest bid object for the given adUnitCode', () => {
const highestCpmBids = $$PREBID_GLOBAL$$.getHighestCpmBids('/19968336/header-bid-tag-0');
expect(highestCpmBids.length).to.equal(1);
expect(highestCpmBids[0]).to.deep.equal($$PREBID_GLOBAL$$._bidsReceived[1]);
});

it('returns nothing when the given adUnit is invalid', () => {
const shouldBeUndefined = $$PREBID_GLOBAL$$.getHighestCpmBid('Stallone');

expect(shouldBeUndefined).to.be.undefined;
it('returns an empty array when the given adUnit is not found', () => {
const highestCpmBids = $$PREBID_GLOBAL$$.getHighestCpmBids('/stallone');
expect(highestCpmBids.length).to.equal(0);
});
});

Expand Down

0 comments on commit 18b3861

Please sign in to comment.