From 0561222e8b20411d91bc4c275973fe4252b2ef3d Mon Sep 17 00:00:00 2001 From: Jurij Sinickij Date: Tue, 30 Jul 2019 19:20:44 +0300 Subject: [PATCH] adformOpenRTB adapter: size targeting using aspect ratios (#4019) * adformOpenRTB adatper: size targeting using aspect ratios * avoid throwing errors while parsing aspect_ratios --- modules/adformOpenRTBBidAdapter.js | 27 +++++- .../modules/adformOpenRTBBidAdapter_spec.js | 93 ++++++++++++++++++- 2 files changed, 115 insertions(+), 5 deletions(-) diff --git a/modules/adformOpenRTBBidAdapter.js b/modules/adformOpenRTBBidAdapter.js index e40de8ad04d..0f69ccd6262 100644 --- a/modules/adformOpenRTBBidAdapter.js +++ b/modules/adformOpenRTBBidAdapter.js @@ -69,11 +69,28 @@ export const spec = { }; if (props) { asset.id = props.id; + let wmin, hmin, w, h; + let aRatios = bidParams.aspect_ratios; + + if (aRatios && aRatios[0]) { + aRatios = aRatios[0]; + wmin = aRatios.min_width || 0; + hmin = aRatios.ratio_height * wmin / aRatios.ratio_width | 0; + } + + if (bidParams.sizes) { + const sizes = flatten(bidParams.sizes); + w = sizes[0]; + h = sizes[1]; + } + asset[props.name] = { len: bidParams.len, - wmin: bidParams.sizes && bidParams.sizes[0], - hmin: bidParams.sizes && bidParams.sizes[1], - type: props.type + type: props.type, + wmin, + hmin, + w, + h }; return asset; @@ -175,3 +192,7 @@ function setOnAny(collection, key) { } } } + +function flatten(arr) { + return [].concat(...arr); +} diff --git a/test/spec/modules/adformOpenRTBBidAdapter_spec.js b/test/spec/modules/adformOpenRTBBidAdapter_spec.js index f6c3098f3b6..cafbe4eff15 100644 --- a/test/spec/modules/adformOpenRTBBidAdapter_spec.js +++ b/test/spec/modules/adformOpenRTBBidAdapter_spec.js @@ -272,13 +272,102 @@ describe('AdformOpenRTB adapter', function () { let assets = JSON.parse(spec.buildRequests(validBidRequests, { refererInfo: { referer: 'page' } }).data).imp[0].native.request.assets; assert.ok(assets[0].title); assert.equal(assets[0].title.len, 140); - assert.deepEqual(assets[1].img, { type: 3, wmin: 150, hmin: 50 }); - assert.deepEqual(assets[2].img, { type: 1, wmin: 50, hmin: 50 }); + assert.deepEqual(assets[1].img, { type: 3, w: 150, h: 50 }); + assert.deepEqual(assets[2].img, { type: 1, w: 50, h: 50 }); assert.deepEqual(assets[3].data, { type: 2, len: 140 }); assert.deepEqual(assets[4].data, { type: 1 }); assert.deepEqual(assets[5].data, { type: 12 }); assert.ok(!assets[6]); }); + + describe('icon/image sizing', function () { + it('should flatten sizes and utilise first pair', function () { + const validBidRequests = [{ + bidId: 'bidId', + params: { siteId: 'siteId', mid: 1000 }, + nativeParams: { + image: { + sizes: [[200, 300], [100, 200]] + }, + } + }]; + + let assets = JSON.parse(spec.buildRequests(validBidRequests, { refererInfo: { referer: 'page' } }).data).imp[0].native.request.assets; + assert.ok(assets[0].img); + assert.equal(assets[0].img.w, 200); + assert.equal(assets[0].img.h, 300); + }); + }); + + it('should utilise aspect_ratios', function () { + const validBidRequests = [{ + bidId: 'bidId', + params: { siteId: 'siteId', mid: 1000 }, + nativeParams: { + image: { + aspect_ratios: [{ + min_width: 100, + ratio_height: 3, + ratio_width: 1 + }] + }, + icon: { + aspect_ratios: [{ + min_width: 10, + ratio_height: 5, + ratio_width: 2 + }] + } + } + }]; + + let assets = JSON.parse(spec.buildRequests(validBidRequests, { refererInfo: { referer: 'page' } }).data).imp[0].native.request.assets; + assert.ok(assets[0].img); + assert.equal(assets[0].img.wmin, 100); + assert.equal(assets[0].img.hmin, 300); + + assert.ok(assets[1].img); + assert.equal(assets[1].img.wmin, 10); + assert.equal(assets[1].img.hmin, 25); + }); + + it('should not throw error if aspect_ratios config is not defined', function () { + const validBidRequests = [{ + bidId: 'bidId', + params: { siteId: 'siteId', mid: 1000 }, + nativeParams: { + image: { + aspect_ratios: [] + }, + icon: { + aspect_ratios: [] + } + } + }]; + + assert.doesNotThrow(() => spec.buildRequests(validBidRequests, { refererInfo: { referer: 'page' } })); + }); + }); + + it('should expect any dimensions if min_width not passed', function () { + const validBidRequests = [{ + bidId: 'bidId', + params: { siteId: 'siteId', mid: 1000 }, + nativeParams: { + image: { + aspect_ratios: [{ + ratio_height: 3, + ratio_width: 1 + }] + } + } + }]; + + let assets = JSON.parse(spec.buildRequests(validBidRequests, { refererInfo: { referer: 'page' } }).data).imp[0].native.request.assets; + assert.ok(assets[0].img); + assert.equal(assets[0].img.wmin, 0); + assert.equal(assets[0].img.hmin, 0); + assert.ok(!assets[1]); }); }); });