Skip to content

Commit

Permalink
use json response instead of jsonp for video and some minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fang Bai committed Oct 23, 2017
1 parent 3a97055 commit 3ee0168
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 52 deletions.
62 changes: 30 additions & 32 deletions modules/openxBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ export const spec = {
}

let requests = [];
let banner_requests = [];
let video_requests = [];
let bids_banner = bids.filter(function(bid) { return bid.mediaType !== 'video'; });
let bids_video = bids.filter(function(bid) { return bid.mediaType === 'video'; });
let bannerRequests = [];
let videoRequests = [];
let bannerBids = bids.filter(function(bid) { return bid.mediaType === BANNER; });
let videoBids = bids.filter(function(bid) { return bid.mediaType === VIDEO; });

// build banner requests
if (bids_banner.length !== 0) {
let delDomain = bids_banner[0].params.delDomain;
let configuredBc = bids_banner[0].params.bc;
if (bannerBids.length !== 0) {
let delDomain = bannerBids[0].params.delDomain;
let configuredBc = bannerBids[0].params.bc;
let bc = configuredBc || `${BIDDER_CONFIG}_${BIDDER_VERSION}`;
banner_requests = [ buildOXRequest(bids_banner, {
bannerRequests = [ buildOXRequest(bannerBids, {
ju: currentURL,
jr: currentURL,
ch: document.charSet || document.characterSet,
Expand All @@ -53,40 +53,41 @@ export const spec = {
},
delDomain)];
}

// build video requests
video_requests = buildOXVideoRequest(bids_video);
requests = banner_requests.concat(video_requests);
// return requests;
if (videoBids.length !== 0) {
videoRequests = buildOXVideoRequest(videoBids);
}

requests = bannerRequests.concat(videoRequests);
return requests;
},
interpretResponse: function(oxResponseObj, bidRequest) {
let bidResponses = [];
let mediaType = 'banner';
let mediaType = BANNER;
if (bidRequest && bidRequest.payload) {
if (bidRequest.payload.bids) {
mediaType = bidRequest.payload.bids[0].mediaType;
} else if (bidRequest.payload.bid) {
mediaType = bidRequest.payload.bid.mediaType;
}
}
if (mediaType === 'video') {
const parts = oxResponseObj.match(/^[a-z0-9]+\(([^\)]+)\)/i);
let response = !parts || parts.length != 2 ? undefined : JSON.parse(parts[1]);
if (response && response.pixels) {
userSync.registerSync('iframe', 'openx', response.pixels);
}
bidResponses = createVideoBidResponses(response, bidRequest.payload);
} else {
let adUnits = oxResponseObj.ads.ad;
if (oxResponseObj.ads && oxResponseObj.ads.pixels) {
userSync.registerSync('iframe', 'openx', oxResponseObj.ads.pixels);
}
if (!adUnits) {
adUnits = [];

if (mediaType === VIDEO) {
if (oxResponseObj && oxResponseObj.pixels) {
userSync.registerSync('iframe', 'openx', oxResponseObj.pixels);
}
bidResponses = createBidResponses(adUnits, bidRequest.payload);
bidResponses = createVideoBidResponses(oxResponseObj, bidRequest.payload);
return bidResponses;
}

let adUnits = oxResponseObj.ads.ad;
if (oxResponseObj.ads && oxResponseObj.ads.pixels) {
userSync.registerSync('iframe', 'openx', oxResponseObj.ads.pixels);
}
if (!adUnits) {
adUnits = [];
}
bidResponses = createBidResponses(adUnits, bidRequest.payload);
return bidResponses;
}
};
Expand Down Expand Up @@ -263,9 +264,6 @@ function buildOXRequest(bids, oxParams, delDomain) {
}

function buildOXVideoRequest(bids) {
if (bids.length === 0) {
return [];
}
return bids.map(function(bid) {
let url = 'http://' + bid.params.delDomain + '/v/1.0/avjp';
let oxVideoParams = generateVideoParameters(bid);
Expand Down Expand Up @@ -311,7 +309,7 @@ function createVideoBidResponses(response, {bid, startTime}) {
bidResponse.currency = 'USD';

if (response.pub_rev) {
bidResponse.cpm = Number(response.pub_rev);
bidResponse.cpm = Number(response.pub_rev) / 1000;
} else {
bidResponse.cpm = 0;
}
Expand Down
48 changes: 28 additions & 20 deletions test/spec/modules/openxBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ describe('OpenxAdapter', () => {
'delDomain': 'test-del-domain'
},
'adUnitCode': 'adunit-code',
'mediaType': 'banner',
'sizes': [[300, 250], [300, 600]],
'bidId': '30b31c1838de1e',
'bidderRequestId': '22edbae2733bf6',
'auctionId': '1d1a030790a475',
};

let bid_video = {
'bidder': 'openxvideo',
let videoBid = {
'bidder': 'openx',
'params': {
'unit': '12345678',
'delDomain': 'test-del-domain',
Expand All @@ -40,44 +41,46 @@ describe('OpenxAdapter', () => {
}
},
'adUnitCode': 'adunit-code',
'mediaType': 'video',
'sizes': [640, 480],
'bidId': '30b31c1838de1e',
'bidderRequestId': '22edbae2733bf6',
'auctionId': '1d1a030790a475',
'transactionId': '4008d88a-8137-410b-aa35-fbfdabcb478e'
};

it('bid: should return true when required params found', () => {
it('should return true when required params found for a banner ad', () => {
expect(spec.isBidRequestValid(bid)).to.equal(true);
});

it('bid: should return false when required params are not passed', () => {
it('should return false when required params are not passed for a banner ad', () => {
let bid = Object.assign({}, bid);
delete bid.params;
bid.params = {};
expect(spec.isBidRequestValid(bid)).to.equal(false);
});

it('bid_video: should return true when required params found', () => {
expect(spec.isBidRequestValid(bid_video)).to.equal(true);
it('should return true when required params found for a video ad', () => {
expect(spec.isBidRequestValid(videoBid)).to.equal(true);
});

it('bid_video: should return false when required params are not passed', () => {
let bid_video = Object.assign({}, bid_video);
delete bid_video.params;
bid_video.params = {};
expect(spec.isBidRequestValid(bid_video)).to.equal(false);
it('should return false when required params are not passed for a video ad', () => {
let videoBid = Object.assign({}, videoBid);
delete videoBid.params;
videoBid.params = {};
expect(spec.isBidRequestValid(videoBid)).to.equal(false);
});
});

describe('buildRequests for non-video', () => {
describe('buildRequests for banner ads', () => {
let bidRequests = [{
'bidder': 'openx',
'params': {
'unit': '12345678',
'delDomain': 'test-del-domain'
},
'adUnitCode': 'adunit-code',
'mediaType': 'banner',
'sizes': [[300, 250], [300, 600]],
'bidId': '30b31c1838de1e',
'bidderRequestId': '22edbae2733bf6',
Expand Down Expand Up @@ -160,7 +163,7 @@ describe('OpenxAdapter', () => {

describe('buildRequests for video', () => {
let bidRequests = [{
'bidder': 'openxvideo',
'bidder': 'openx',
'mediaType': 'video',
'params': {
'unit': '12345678',
Expand Down Expand Up @@ -198,14 +201,15 @@ describe('OpenxAdapter', () => {
});
});

describe('interpretResponse for non-video', () => {
describe('interpretResponse for banner ads', () => {
let bids = [{
'bidder': 'openx',
'params': {
'unit': '12345678',
'delDomain': 'test-del-domain'
},
'adUnitCode': 'adunit-code',
'mediaType': 'banner',
'sizes': [[300, 250], [300, 600]],
'bidId': '30b31c1838de1e',
'bidderRequestId': '22edbae2733bf6',
Expand Down Expand Up @@ -290,9 +294,9 @@ describe('OpenxAdapter', () => {
});
});

describe('interpretResponse for video', () => {
describe('interpretResponse for video ads', () => {
let bids = [{
'bidder': 'openxvideo',
'bidder': 'openx',
'mediaType': 'video',
'params': {
'unit': '12345678',
Expand All @@ -316,14 +320,18 @@ describe('OpenxAdapter', () => {
data: {},
payload: {'bid': bids[0], 'startTime': new Date()}
};
let bidResponse = 'json({"cache_key":"test_cache_key", "pub_rev":"1",' +
' "per_colo_domain":"http://delivery-us-west-1.openx.net", "ph":"7a3b9374-7986-4a41-a79d-034193518aee"})';
let bidResponse = {
'cache_key': 'test_cache_key',
'pub_rev': '1',
'per_colo_domain': 'http://delivery-us-west-1.openx.net',
'ph': '7a3b9374-7986-4a41-a79d-034193518aee'
};

it('should return correct bid response', () => {
let expectedResponse = [
{
'requestId': '30b31c1838de1e',
'bidderCode': '"openxvideo"',
'bidderCode': 'openx',
'cpm': 1,
'width': '640',
'height': '480',
Expand All @@ -343,7 +351,7 @@ describe('OpenxAdapter', () => {
});

it('handles nobid responses', () => {
bidResponse = 'json({"cache_key":"", "pub_rev":"", "per_colo_domain":"", "ph":""})';
bidResponse = {'cache_key': '', 'pub_rev': '', 'per_colo_domain': '', 'ph': ''};
let result = spec.interpretResponse(bidResponse, bidRequest);
expect(result.length).to.equal(0);
});
Expand Down

0 comments on commit 3ee0168

Please sign in to comment.