Skip to content

Commit

Permalink
Platformio Adapter (#1465)
Browse files Browse the repository at this point in the history
* Add PlatformioBidAdapter

* Update platformioBidAdapter.js

* Add files via upload

* Update hello_world.html

* Update platformioBidAdapter.js

* Update platformioBidAdapter_spec.js

* Update hello_world.html

* Update platformioBidAdapter_spec.js

* Update platformioBidAdapter.js

* Update hello_world.html

* Add files via upload
  • Loading branch information
varashellov authored and Nate Cozi committed Aug 14, 2017
1 parent 4dd8896 commit 311721a
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 0 deletions.
62 changes: 62 additions & 0 deletions modules/platformioBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var bidfactory = require('src/bidfactory.js');
var bidmanager = require('src/bidmanager.js');
var adloader = require('src/adloader.js');
var utils = require('src/utils.js');
var CONSTANTS = require('src/constants.json');
var adaptermanager = require('src/adaptermanager');

var PlatformIOAdapter = function PlatformIOAdapter() {
function _callBids(params) {
var bidURL;
var bids = params.bids || [];
var requestURL = window.location.protocol + '//adx1js.s3.amazonaws.com/pb_ortb.js?cb=' + new Date().getTime() + '&ver=1&';

for (var i = 0; i < bids.length; i++) {
var requestParams = {};
var bid = bids[i];

requestParams.pub_id = bid.params.pubId;
requestParams.site_id = bid.params.siteId;

var parseSized = utils.parseSizesInput(bid.sizes);
var arrSize = parseSized[0].split('x');

requestParams.width = arrSize[0];
requestParams.height = arrSize[1];
requestParams.callback = '$$PREBID_GLOBAL$$._doPlatformIOCallback';
requestParams.callback_uid = bid.bidId;
bidURL = requestURL + utils.parseQueryStringParameters(requestParams);

utils.logMessage('PlatformIO.prebid, Bid ID: ' + bid.bidId + ', Pub ID: ' + bid.params.pubId);
adloader.loadScript(bidURL);
}
}

$$PREBID_GLOBAL$$._doPlatformIOCallback = function (response) {
var bidObject;
var bidRequest;
var callbackID;
callbackID = response.callback_uid;
bidRequest = utils.getBidRequest(callbackID);
if (response.cpm > 0) {
bidObject = bidfactory.createBid(CONSTANTS.STATUS.GOOD, bidRequest);
bidObject.bidderCode = 'platformio';
bidObject.cpm = response.cpm;
bidObject.ad = response.tag;
bidObject.width = response.width;
bidObject.height = response.height;
} else {
bidObject = bidfactory.createBid(CONSTANTS.STATUS.NO_BID, bidRequest);
bidObject.bidderCode = 'platformio';
utils.logMessage('No Bid response from Platformio request: ' + callbackID);
}
bidmanager.addBidResponse(bidRequest.placementCode, bidObject);
};

return {
callBids: _callBids
};
};
adaptermanager.registerBidAdapter(new PlatformIOAdapter(), 'platformio');

module.exports = PlatformIOAdapter;
156 changes: 156 additions & 0 deletions test/spec/modules/platformioBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
describe('platformio adapter tests', function () {
var expect = require('chai').expect;
var urlParse = require('url-parse');
var querystringify = require('querystringify');
var adapter = require('modules/platformioBidAdapter');
var adLoader = require('src/adloader');
var bidmanager = require('src/bidmanager');

var stubLoadScript;

beforeEach(function () {
stubLoadScript = sinon.stub(adLoader, 'loadScript');
});

afterEach(function () {
stubLoadScript.restore();
});

describe('creation of bid url', function () {
if (typeof ($$PREBID_GLOBAL$$._bidsReceived) === 'undefined') {
$$PREBID_GLOBAL$$._bidsReceived = [];
}
if (typeof ($$PREBID_GLOBAL$$._bidsRequested) === 'undefined') {
$$PREBID_GLOBAL$$._bidsRequested = [];
}

it('bid request for single placement', function () {
var params = {
bids: [{
placementCode: '/19968336/header-bid-tag-0',
sizes: [[300, 250]],
bidId: 'bid1111',
bidder: 'platformio',
params: { pubId: '37054', siteId: '123' }
}]
};

adapter().callBids(params);

var bidUrl = stubLoadScript.getCall(0).args[0];

sinon.assert.calledOnce(stubLoadScript);

var parsedBidUrl = urlParse(bidUrl);
var parsedBidUrlQueryString = querystringify.parse(parsedBidUrl.query);
expect(parsedBidUrlQueryString).to.have.property('pub_id').and.to.equal('37054');
expect(parsedBidUrlQueryString).to.have.property('site_id').and.to.equal('123');
expect(parsedBidUrlQueryString).to.have.property('width').and.to.equal('300');
expect(parsedBidUrlQueryString).to.have.property('height').and.to.equal('250');
});
});

describe('handling bid response', function () {
it('should return complete bid response', function() {
var stubAddBidResponse = sinon.stub(bidmanager, 'addBidResponse');

var params = {
bids: [{
placementCode: '/19968336/header-bid-tag-0',
sizes: [[300, 250]],
bidId: 'bid1111',
bidder: 'platformio',
params: { pubId: '37054', siteId: '123' }
}]
};

var response = {
cpm: 1,
width: 300,
height: 250,
callback_uid: 'bid1111',
tag: '<script>document.write("campaign banner");<\/script>'
};

adapter().callBids(params);

var adUnits = [];
var unit = {};
unit.bids = params.bids;
unit.code = '/123456/header-bid-tag-1';
unit.sizes = [[300, 250]];
adUnits.push(unit);

if (typeof ($$PREBID_GLOBAL$$._bidsRequested) === 'undefined') {
$$PREBID_GLOBAL$$._bidsRequested = [params];
} else {
$$PREBID_GLOBAL$$._bidsRequested.push(params);
}

$$PREBID_GLOBAL$$.adUnits = adUnits;

$$PREBID_GLOBAL$$._doPlatformIOCallback(response);

var bidPlacementCode1 = stubAddBidResponse.getCall(0).args[0];
var bidObject1 = stubAddBidResponse.getCall(0).args[1];

expect(bidPlacementCode1).to.equal('/19968336/header-bid-tag-0');
expect(bidObject1.bidderCode).to.equal('platformio');
expect(bidObject1.cpm).to.equal(1);
expect(bidObject1.width).to.equal(300);
expect(bidObject1.height).to.equal(250);
expect(bidObject1.ad).to.have.length.above(1);

stubAddBidResponse.restore();
});

it('should return no bid response', function() {
var stubAddBidResponse = sinon.stub(bidmanager, 'addBidResponse');

var params = {
bids: [{
placementCode: '/19968336/header-bid-tag-0',
sizes: [[300, 250]],
bidId: 'bid1111',
bidder: 'platformio',
params: { pubId: '37054', siteId: '123' }
}]
};

var response = {
cpm: 0,
width: 300,
height: 250,
callback_uid: 'bid1111',
tag: '<script>document.write("default banner");<\/script>'
};

adapter().callBids(params);

var adUnits = [];
var unit = {};
unit.bids = params.bids;
unit.code = '/123456/header-bid-tag-1';
unit.sizes = [[300, 250]];
adUnits.push(unit);

if (typeof ($$PREBID_GLOBAL$$._bidsRequested) === 'undefined') {
$$PREBID_GLOBAL$$._bidsRequested = [params];
} else {
$$PREBID_GLOBAL$$._bidsRequested.push(params);
}

$$PREBID_GLOBAL$$.adUnits = adUnits;

$$PREBID_GLOBAL$$._doPlatformIOCallback(response);

var bidPlacementCode1 = stubAddBidResponse.getCall(0).args[0];
var bidObject1 = stubAddBidResponse.getCall(0).args[1];

expect(bidPlacementCode1).to.equal('/19968336/header-bid-tag-0');
expect(bidObject1.bidderCode).to.equal('platformio');

stubAddBidResponse.restore();
});
});
});

0 comments on commit 311721a

Please sign in to comment.