Skip to content

Commit

Permalink
Rz/ajax (#5) (#1042)
Browse files Browse the repository at this point in the history
update sharethrough adapter to use ajax for bid requests
  • Loading branch information
rizhang authored and Nate Cozi committed Mar 21, 2017
1 parent 8c873ef commit fb094cd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 40 deletions.
38 changes: 7 additions & 31 deletions src/adapters/sharethrough.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,46 @@
var utils = require('../utils.js');
var bidmanager = require('../bidmanager.js');
var bidfactory = require('../bidfactory.js');
var ajax = require('../ajax.js').ajax;

const STR_BIDDER_CODE = "sharethrough";
const STR_VERSION = "0.1.0"; //Need to check analytics too for version
const STR_VERSION = "1.1.0";

var SharethroughAdapter = function SharethroughAdapter() {

const str = {};
str.STR_BTLR_HOST = document.location.protocol + "//btlr.sharethrough.com";
str.STR_BEACON_HOST = document.location.protocol + "//b.sharethrough.com/butler?";
str.placementCodeSet = {};
str.ajax = ajax;

function _callBids(params) {
const bids = params.bids;

addEventListener("message", _receiveMessage, false);

// cycle through bids
for (let i = 0; i < bids.length; i += 1) {
const bidRequest = bids[i];
str.placementCodeSet[bidRequest.placementCode] = bidRequest;
const scriptUrl = _buildSharethroughCall(bidRequest);
str.loadIFrame(scriptUrl);
str.ajax(scriptUrl, $$PREBID_GLOBAL$$.strcallback);
}
}

function _buildSharethroughCall(bid) {
const testPkey = 'test';
const pkey = utils.getBidIdParameter('pkey', bid.params);

let host = str.STR_BTLR_HOST;

let url = host + "/header-bid/v1?";
url = utils.tryAppendQueryString(url, 'bidId', bid.bidId);

if(pkey !== testPkey) {
url = utils.tryAppendQueryString(url, 'placement_key', pkey);
url = utils.tryAppendQueryString(url, 'ijson', '$$PREBID_GLOBAL$$.strcallback');
url = appendEnvFields(url);
} else {
url = url.substring(0, url.length - 1);
}
url = utils.tryAppendQueryString(url, 'placement_key', pkey);
url = appendEnvFields(url);

return url;
}

str.loadIFrame = function(url) {
const iframe = document.createElement("iframe");
iframe.src = url;
iframe.style.cssText = 'display:none;';

document.body.appendChild(iframe);
};

function _receiveMessage(event) {
if(event.origin === str.STR_BTLR_HOST) {
try {
$$PREBID_GLOBAL$$.strcallback(JSON.parse(event.data).response);
} catch(e) {
console.log(e);
}
}
}

$$PREBID_GLOBAL$$.strcallback = function(bidResponse) {
bidResponse = JSON.parse(bidResponse);
const bidId = bidResponse.bidId;
const bidObj = utils.getBidRequest(bidId);
try {
Expand Down
18 changes: 9 additions & 9 deletions test/spec/adapters/sharethrough_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@ describe('sharethrough adapter', () => {
let secondBidUrl;

beforeEach(() => {
sandbox.spy(adapter.str, 'loadIFrame');
sandbox.spy(adapter.str, 'ajax');
});

it('should call loadIFrame on the adloader for each bid', () => {
it('should call ajax to make a request for each bid', () => {

adapter.callBids(bidderRequest);

firstBidUrl = adapter.str.loadIFrame.firstCall.args[0];
secondBidUrl = adapter.str.loadIFrame.secondCall.args[0];
firstBidUrl = adapter.str.ajax.firstCall.args[0];
secondBidUrl = adapter.str.ajax.secondCall.args[0];

sinon.assert.calledTwice(adapter.str.loadIFrame);
sinon.assert.calledTwice(adapter.str.ajax);

expect(firstBidUrl).to.contain(adapter.str.STR_BTLR_HOST + '/header-bid/v1?bidId=bidId1&placement_key=aaaa1111&ijson=pbjs.strcallback&hbVersion=%24prebid.version%24&strVersion=0.1.0&hbSource=prebid&');
expect(secondBidUrl).to.contain(adapter.str.STR_BTLR_HOST + '/header-bid/v1?bidId=bidId2&placement_key=bbbb2222&ijson=pbjs.strcallback&hbVersion=%24prebid.version%24&strVersion=0.1.0&hbSource=prebid&');
expect(firstBidUrl).to.contain(adapter.str.STR_BTLR_HOST + '/header-bid/v1?bidId=bidId1&placement_key=aaaa1111&hbVersion=%24prebid.version%24&strVersion=1.1.0&hbSource=prebid&');
expect(secondBidUrl).to.contain(adapter.str.STR_BTLR_HOST + '/header-bid/v1?bidId=bidId2&placement_key=bbbb2222&hbVersion=%24prebid.version%24&strVersion=1.1.0&hbSource=prebid&');
});

});
Expand Down Expand Up @@ -117,8 +117,8 @@ describe('sharethrough adapter', () => {
"stxUserId": ""
};

pbjs.strcallback(bidderReponse1);
pbjs.strcallback(bidderReponse2);
pbjs.strcallback(JSON.stringify(bidderReponse1));
pbjs.strcallback(JSON.stringify(bidderReponse2));

firstBid = bidManager.addBidResponse.firstCall.args[1];
secondBid = bidManager.addBidResponse.secondCall.args[1];
Expand Down

0 comments on commit fb094cd

Please sign in to comment.