Skip to content

Commit

Permalink
Pass keywords parameter to bid request (#524)
Browse files Browse the repository at this point in the history
Fixes #523
  • Loading branch information
matthewlane authored and protonate committed Aug 20, 2016
1 parent f9945c5 commit 922c6e1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/adapters/appnexusAst.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ function AppnexusAstAdapter() {
tag.allow_smaller_sizes = bid.params.allowSmallerSizes || false;
tag.prebid = true;
tag.disable_psa = true;
if (!utils.isEmpty(bid.params.keywords)) {
tag.keywords = getKeywords(bid.params.keywords);
}

return tag;
});
Expand Down Expand Up @@ -99,6 +102,29 @@ function AppnexusAstAdapter() {
}
}

/* Turn keywords parameter into ut-compatible format */
function getKeywords(keywords) {
let arrs = [];

utils._each(keywords, (v, k) => {
if (utils.isArray(v)) {
let values = [];
utils._each(v, (val) => {
val = utils.getValueString('keywords.' + k, val);
if (val) {values.push(val);}
});
v = values;
} else {
v = utils.getValueString('keywords.' + k, v);
if (utils.isStr(v)) {v = [v];}
else {return;} // unsuported types - don't send a key
}
arrs.push({key: k, value: v});
});

return arrs;
}

/* Turn bid request sizes into ut-compatible format */
function getSizes(requestSizes) {
let sizes = [];
Expand Down
18 changes: 18 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var _loggingChecked = false;
var t_Arr = 'Array';
var t_Str = 'String';
var t_Fn = 'Function';
var t_Numb = 'Number';
var toString = Object.prototype.toString;
let infoLogger = null;
try {
Expand Down Expand Up @@ -332,6 +333,10 @@ exports.isArray = function (object) {
return this.isA(object, t_Arr);
};

exports.isNumber = function(object) {
return this.isA(object, t_Numb);
};

/**
* Return if the object is "empty";
* this includes falsey, no keys, or no items at indices
Expand Down Expand Up @@ -469,6 +474,19 @@ exports.getIframeDocument = function (iframe) {
return doc;
};

exports.getValueString = function(param, val, defaultValue) {
if (val === undefined || val === null) {
return defaultValue;
}
if (this.isStr(val) ) {
return val;
}
if (this.isNumber(val)) {
return val.toString();
}
this.logWarn('Unsuported type for param: ' + param + ' required type: String');
};

export function uniques(value, index, arry) {
return arry.indexOf(value) === index;
}
Expand Down
35 changes: 34 additions & 1 deletion test/spec/adapters/appnexusAst_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('AppNexusAdapter', () => {
it('requires member && invCode', () => {
let backup = REQUEST.bids[0].params;
REQUEST.bids[0].params = {member : 1234};
adapter.callBids(REQUEST );
adapter.callBids(REQUEST);
expect(requests).to.be.empty;
REQUEST.bids[0].params = backup;
});
Expand All @@ -102,6 +102,39 @@ describe('AppNexusAdapter', () => {
expect(requests[0].method).to.equal('POST');
});

it('converts keyword params to proper form and attaches to request', () => {
REQUEST.bids[0].params.keywords = {
single: 'val',
singleArr: ['val'],
singleArrNum: [5],
multiValMixed: ['value1', 2, 'value3'],
singleValNum: 123,
badValue: {'foo': 'bar'} // should be dropped
};

adapter.callBids(REQUEST);

const request = JSON.parse(requests[0].requestBody).tags[0];
expect(request.keywords).to.deep.equal([{
"key": "single",
"value": ["val"]
}, {
"key": "singleArr",
"value": ["val"]
}, {
"key": "singleArrNum",
"value": ["5"]
}, {
"key": "multiValMixed",
"value": ["value1", "2", "value3"]
}, {
"key": "singleValNum",
"value": ["123"]
}]);

delete REQUEST.bids[0].params.keywords;
});

});

describe('response handler', () => {
Expand Down

0 comments on commit 922c6e1

Please sign in to comment.