Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support aspect ratio specification for native images #1634

Merged
merged 1 commit into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are trailing commas allowed now?? 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

};
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 adunitKeys = Object.keys(params[key]);
const requiredKeys = Object.keys(requiredParams);
const remaining = adunitKeys.filter(key => !requiredKeys.includes(key));

// if none 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