Skip to content

Commit

Permalink
Support aspect ratio specification for native images
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewlane committed Sep 28, 2017
1 parent 6cf71d0 commit c368889
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 26 deletions.
69 changes: 44 additions & 25 deletions modules/appnexusAstBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ const NATIVE_MAPPING = {
cta: 'ctatext',
image: {
serverName: 'main_image',
serverParams: { required: true, sizes: [{}] }
requiredParams: { required: true },
minimumParams: { sizes: [{}] },
},
icon: {
serverName: 'icon',
serverParams: { required: true, sizes: [{}] }
requiredParams: { required: true },
minimumParams: { sizes: [{}] },
},
sponsoredBy: 'sponsored_by'
sponsoredBy: 'sponsored_by',
};
const SOURCE = 'pbjs';

Expand Down Expand Up @@ -264,28 +266,7 @@ function bidToTag(bid) {
tag.ad_types = ['native'];

if (bid.nativeParams) {
const nativeRequest = {};

// map standard prebid native asset identifier to /ut parameters
// e.g., tag specifies `body` but /ut only knows `description`
// mapping may be in form {tag: '<server name>'} or
// {tag: {serverName: '<server name>', serverParams: {...}}}
Object.keys(bid.nativeParams).forEach(key => {
// check if one of the <server name> forms is used, otherwise
// a mapping wasn't specified so pass the key straight through
const requestKey =
(NATIVE_MAPPING[key] && NATIVE_MAPPING[key].serverName) ||
NATIVE_MAPPING[key] ||
key;

// if the mapping for this identifier specifies required server
// params via the `serverParams` object, merge that in
nativeRequest[requestKey] = Object.assign({},
NATIVE_MAPPING[key] && NATIVE_MAPPING[key].serverParams,
bid.nativeParams[key]
);
});

const nativeRequest = buildNativeRequest(bid.nativeParams);
tag['native'] = {layouts: [nativeRequest]};
}
}
Expand Down Expand Up @@ -343,6 +324,44 @@ function getRtbBid(tag) {
return tag && tag.ads && tag.ads.length && tag.ads.find(ad => ad.rtb);
}

function buildNativeRequest(params) {
const request = {};

// map standard prebid native asset identifier to /ut parameters
// e.g., tag specifies `body` but /ut only knows `description`.
// mapping may be in form {tag: '<server name>'} or
// {tag: {serverName: '<server name>', requiredParams: {...}}}
Object.keys(params).forEach(key => {
// check if one of the <server name> forms is used, otherwise
// a mapping wasn't specified so pass the key straight through
const requestKey =
(NATIVE_MAPPING[key] && NATIVE_MAPPING[key].serverName) ||
NATIVE_MAPPING[key] ||
key;

// required params are always passed on request
const requiredParams = NATIVE_MAPPING[key] && NATIVE_MAPPING[key].requiredParams;
request[requestKey] = Object.assign({}, requiredParams, params[key]);

// minimum params are passed if no non-required params given on adunit
const minimumParams = NATIVE_MAPPING[key] && NATIVE_MAPPING[key].minimumParams;

if (requiredParams && minimumParams) {
// subtract required keys from adunit keys
const remaining = Object.keys(params[key]).filter(paramKey => {
return !Object.keys(requiredParams).includes(paramKey)
});

// if non are left over, the minimum params needs to be sent
if (remaining.length === 0) {
request[requestKey] = Object.assign({}, request[requestKey], minimumParams);
}
}
});

return request;
}

function outstreamRender(bid) {
// push to render queue because ANOutstreamVideo may not be loaded yet
bid.renderer.push(() => {
Expand Down
32 changes: 31 additions & 1 deletion test/spec/modules/appnexusAstBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('AppNexusAdapter', () => {
delete REQUEST.bids[0].params.nativeParams;
});

it('sets required native asset params when not provided on adunit', () => {
it('sets minimum native asset params when not provided on adunit', () => {
REQUEST.bids[0].mediaType = 'native';
REQUEST.bids[0].nativeParams = {
image: {required: true},
Expand All @@ -181,6 +181,36 @@ describe('AppNexusAdapter', () => {
delete REQUEST.bids[0].params.nativeParams;
});

it('does not overwrite native ad unit params with mimimum params', () => {
REQUEST.bids[0].mediaType = 'native';
REQUEST.bids[0].nativeParams = {
image: {
aspect_ratios: [{
min_width: 100,
ratio_width: 2,
ratio_height: 3,
}]
},
};

adapter.callBids(REQUEST);

const request = JSON.parse(requests[0].requestBody);
expect(request.tags[0].native.layouts[0]).to.deep.equal({
main_image: {
required: true,
aspect_ratios: [{
min_width: 100,
ratio_width: 2,
ratio_height: 3,
}]
},
});

delete REQUEST.bids[0].mediaType;
delete REQUEST.bids[0].params.nativeParams;
});

it('sends bid request to ENDPOINT via POST', () => {
adapter.callBids(REQUEST);
expect(requests[0].url).to.equal(ENDPOINT);
Expand Down

0 comments on commit c368889

Please sign in to comment.