Skip to content

Commit

Permalink
modify 'featureforward' adapter to be an indepandant adapter (prebid#…
Browse files Browse the repository at this point in the history
…1288)

* changing featureforward to be an independant adapter

* initial version of featureforward adapter

* featureforward adapter auto test

* small fixes 1

* fixes 2

* fixes 3

* Create featureforwardBidAdapter.js

*  file is not needed anymore
  • Loading branch information
FeatureForwardDev authored and dluxemburg committed Jul 17, 2018
1 parent 8a155d1 commit 0e595bf
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 0 deletions.
89 changes: 89 additions & 0 deletions modules/featureforwardBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {createBid} from 'src/bidfactory';
import {addBidResponse} from 'src/bidmanager';
import {logError, getTopWindowLocation} from 'src/utils';
import {ajax} from 'src/ajax';
import {STATUS} from 'src/constants';

function FeatureForwardAdapter() {
const bidUrl = window.location.protocol + '//prmbdr.featureforward.com/newbidder/bidder1_prm.php?';
const ajaxOptions = {
method: 'POST',
withCredentials: true,
contentType: 'text/plain'
};

function _callBids(bidderRequest) {
var i = 0;
bidderRequest.bids.forEach(bidRequest => {
try {
while (bidRequest.sizes[i] !== undefined) {
var params = Object.assign({}, environment(), bidRequest.params, {'size': bidRequest.sizes[i]});
var postRequest = JSON.stringify(params);
var url = bidUrl;
i++;
ajax(url, (bidResponse) => {
bidResponseAvailable(bidRequest, bidResponse);
}, postRequest, ajaxOptions);
}
} catch (e) {
// register passback on any exceptions while attempting to fetch response.
logError('featureforward.requestBid', 'ERROR', e);
bidResponseAvailable(bidRequest);
}
});
}

function environment() {
return {
ca: 'BID',
'if': 0,
url: getTopWindowLocation().href,
refurl: referrer(),
ew: document.documentElement.clientWidth,
eh: document.documentElement.clientHeight,
ln: (navigator.language || navigator.browserLanguage || navigator.userLanguage || navigator.systemLanguage)
};
}

function referrer() {
try {
return window.top.document.referrer;
} catch (e) {
return document.referrer;
}
}

function bidResponseAvailable(bidRequest, rawResponse) {
if (rawResponse) {
var bidResponse = parse(rawResponse);
if (bidResponse) {
var bid = createBid(STATUS.GOOD, bidRequest);
bid.bidderCode = bidRequest.bidder;
bid.cpm = bidResponse.bidCpm;
bid.ad = bidResponse.html;
bid.width = bidResponse.width;
bid.height = bidResponse.height;
addBidResponse(bidRequest.placementCode, bid);
return;
}
}
var passback = createBid(STATUS.NO_BID, bidRequest);
passback.bidderCode = bidRequest.bidder;
addBidResponse(bidRequest.placementCode, passback);
}

function parse(rawResponse) {
try {
return JSON.parse(rawResponse);
} catch (ex) {
logError('featureforward.safeParse', 'ERROR', ex);
return null;
}
}

return {
callBids: _callBids
};
}

module.exports = FeatureForwardAdapter;
89 changes: 89 additions & 0 deletions src/adapters/featureforward.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {createBid} from 'src/bidfactory';
import {addBidResponse} from 'src/bidmanager';
import {logError, getTopWindowLocation} from 'src/utils';
import {ajax} from 'src/ajax';
import {STATUS} from 'src/constants';

function FeatureForwardAdapter() {
const bidUrl = window.location.protocol + '//prmbdr.featureforward.com/newbidder/bidder1_prm.php?';
const ajaxOptions = {
method: 'POST',
withCredentials: true,
contentType: 'text/plain'
};

function _callBids(bidderRequest) {
var i = 0;
bidderRequest.bids.forEach(bidRequest => {
try {
while (bidRequest.sizes[i] !== undefined) {
var params = Object.assign({}, environment(), bidRequest.params, {'size': bidRequest.sizes[i]});
var postRequest = JSON.stringify(params);
var url = bidUrl;
i++;
ajax(url, (bidResponse) => {
bidResponseAvailable(bidRequest, bidResponse);
}, postRequest, ajaxOptions);
}
} catch (e) {
// register passback on any exceptions while attempting to fetch response.
logError('featureforward.requestBid', 'ERROR', e);
bidResponseAvailable(bidRequest);
}
});
}

function environment() {
return {
ca: 'BID',
'if': 0,
url: getTopWindowLocation().href,
refurl: referrer(),
ew: document.documentElement.clientWidth,
eh: document.documentElement.clientHeight,
ln: (navigator.language || navigator.browserLanguage || navigator.userLanguage || navigator.systemLanguage)
};
}

function referrer() {
try {
return window.top.document.referrer;
} catch (e) {
return document.referrer;
}
}

function bidResponseAvailable(bidRequest, rawResponse) {
if (rawResponse) {
var bidResponse = parse(rawResponse);
if (bidResponse) {
var bid = createBid(STATUS.GOOD, bidRequest);
bid.bidderCode = bidRequest.bidder;
bid.cpm = bidResponse.bidCpm;
bid.ad = bidResponse.html;
bid.width = bidResponse.width;
bid.height = bidResponse.height;
addBidResponse(bidRequest.placementCode, bid);
return;
}
}
var passback = createBid(STATUS.NO_BID, bidRequest);
passback.bidderCode = bidRequest.bidder;
addBidResponse(bidRequest.placementCode, passback);
}

function parse(rawResponse) {
try {
return JSON.parse(rawResponse);
} catch (ex) {
logError('featureforward.safeParse', 'ERROR', ex);
return null;
}
}

return {
callBids: _callBids
};
}

module.exports = FeatureForwardAdapter;
87 changes: 87 additions & 0 deletions test/spec/adapters/featureforward_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {expect} from 'chai';
import FeatureForwardAdapter from 'src/adapters/featureforward.js';
import bidManager from 'src/bidmanager';
import * as ajax from 'src/ajax';
import {parse as parseURL} from 'src/url';

describe('FeatureForward Adapter Tests', () => {
let featureForwardAdapter = new FeatureForwardAdapter();
let slotConfigs;
let ajaxStub;
beforeEach(() => {
sinon.stub(bidManager, 'addBidResponse');
ajaxStub = sinon.stub(ajax, 'ajax');
slotConfigs = {
bids: [
{
sizes: [[300, 250]],
bidder: 'featureforward',
placementCode: 'test1_placement',
params: {
pubId: '001',
siteId: '111',
placementId: '1',
}
}]
};
});

afterEach(() => {
bidManager.addBidResponse.restore();
ajaxStub.restore();
});

it('Verify requests sent to FeatureForward', () => {
featureForwardAdapter.callBids(slotConfigs);
var call = ajaxStub.firstCall.args[0];
var request = JSON.parse(ajaxStub.args[0][2]);
var creds = ajaxStub.args[0][3];
expect(call).to.equal('http://prmbdr.featureforward.com/newbidder/bidder1_prm.php?');
expect(request.ca).to.equal('BID');
expect(request.pubId).to.equal('001');
expect(request.siteId).to.equal('111');
expect(request.placementId).to.equal('1');
expect(request.size[0]).to.equal(300);
expect(request.size[1]).to.equal(250);
expect(creds.method).to.equal('POST');
});

it('Verify bid', () => {
featureForwardAdapter.callBids(slotConfigs);
ajaxStub.firstCall.args[1](JSON.stringify({
html: 'FF Test Ad',
bidCpm: 0.555,
width: 300,
height: 250
}));
let bid = bidManager.addBidResponse.firstCall.args[1];
expect(bid.bidderCode).to.equal('featureforward');
expect(bid.cpm).to.equal(0.555);
expect(bid.ad).to.equal('FF Test Ad');
expect(bid.width).to.equal(300);
expect(bid.height).to.equal(250);
});

it('Verify passback', () => {
featureForwardAdapter.callBids(slotConfigs);
// trigger a mock ajax callback with no bid.
ajaxStub.firstCall.args[1](null);
let placement = bidManager.addBidResponse.firstCall.args[0];
let bid = bidManager.addBidResponse.firstCall.args[1];
expect(placement).to.equal('test1_placement');
expect(bid.bidderCode).to.equal('featureforward');
expect(bid).to.not.have.property('ad');
expect(bid).to.not.have.property('cpm');
});

it('Verify passback when ajax call fails', () => {
ajaxStub.throws();
featureForwardAdapter.callBids(slotConfigs);
let placement = bidManager.addBidResponse.firstCall.args[0];
let bid = bidManager.addBidResponse.firstCall.args[1];
expect(placement).to.equal('test1_placement');
expect(bid.bidderCode).to.equal('featureforward');
expect(bid).to.not.have.property('ad');
expect(bid).to.not.have.property('cpm');
});
});

0 comments on commit 0e595bf

Please sign in to comment.