From 4f9d0a599f1b4c055cca3c446e1cce17aa2663f2 Mon Sep 17 00:00:00 2001
From: Jonathan Go <jgo@sonobi.com>
Date: Wed, 25 Apr 2018 15:57:22 -0400
Subject: [PATCH 1/8] added vp param to trinity request

---
 modules/sonobiBidAdapter.js                | 43 ++++++++++++++++++----
 test/spec/modules/sonobiBidAdapter_spec.js | 20 ++++++++--
 2 files changed, 51 insertions(+), 12 deletions(-)

diff --git a/modules/sonobiBidAdapter.js b/modules/sonobiBidAdapter.js
index 170228dde7a..697c6dd8d17 100644
--- a/modules/sonobiBidAdapter.js
+++ b/modules/sonobiBidAdapter.js
@@ -1,7 +1,7 @@
 import { registerBidder } from 'src/adapters/bidderFactory';
-import * as utils from 'src/utils';
+import { getTopWindowLocation, parseSizesInput } from 'src/utils';
+import * as utils from '../src/utils';
 import { BANNER, VIDEO } from '../src/mediaTypes';
-import find from 'core-js/library/fn/array/find';
 
 const BIDDER_CODE = 'sonobi';
 const STR_ENDPOINT = 'https://apex.go.sonobi.com/trinity.json';
@@ -46,9 +46,10 @@ export const spec = {
 
     const payload = {
       'key_maker': JSON.stringify(data),
-      'ref': utils.getTopWindowLocation().host,
+      'ref': getTopWindowLocation().host,
       's': utils.generateUUID(),
       'pv': PAGEVIEW_ID,
+      'vp': _getPlatform()
     };
 
     if (validBidRequests[0].params.hfa) {
@@ -80,7 +81,7 @@ export const spec = {
 
     Object.keys(bidResponse.slots).forEach(slot => {
       const bidId = _getBidIdFromTrinityKey(slot);
-      const bidRequest = find(bidderRequests, bidReqest => bidReqest.bidId === bidId);
+      const bidRequest = bidderRequests.find(bidReqest => bidReqest.bidId === bidId);
       const videoMediaType = utils.deepAccess(bidRequest, 'mediaTypes.video');
       const mediaType = bidRequest.mediaType || (videoMediaType ? 'video' : null);
       const createCreative = _creative(mediaType);
@@ -138,9 +139,9 @@ export const spec = {
 
 function _validateSize (bid) {
   if (bid.params.sizes) {
-    return utils.parseSizesInput(bid.params.sizes).join(',');
+    return parseSizesInput(bid.params.sizes).join(',');
   }
-  return utils.parseSizesInput(bid.sizes).join(',');
+  return parseSizesInput(bid.sizes).join(',');
 }
 
 function _validateSlot (bid) {
@@ -161,16 +162,42 @@ const _creative = (mediaType) => (sbi_dc, sbi_aid) => {
   if (mediaType === 'video') {
     return _videoCreative(sbi_dc, sbi_aid)
   }
-  const src = 'https://' + sbi_dc + 'apex.go.sonobi.com/sbi.js?aid=' + sbi_aid + '&as=null' + '&ref=' + utils.getTopWindowLocation().host;
+  const src = 'https://' + sbi_dc + 'apex.go.sonobi.com/sbi.js?aid=' + sbi_aid + '&as=null' + '&ref=' + getTopWindowLocation().host;
   return '<script type="text/javascript" src="' + src + '"></script>';
 }
 
 function _videoCreative(sbi_dc, sbi_aid) {
-  return `https://${sbi_dc}apex.go.sonobi.com/vast.xml?vid=${sbi_aid}&ref=${utils.getTopWindowLocation().host}`
+  return `https://${sbi_dc}apex.go.sonobi.com/vast.xml?vid=${sbi_aid}&ref=${getTopWindowLocation().host}`
 }
 
 function _getBidIdFromTrinityKey (key) {
   return key.split('|').slice(-1)[0]
 }
 
+/**
+ * @param context - the window to determine the innerWidth from. This is purely for test purposes as it should always be the current window
+ */
+export const _isInbounds = (context = window) => (lowerBound = 0, upperBound = Number.MAX_SAFE_INTEGER) => context.innerWidth >= lowerBound && context.innerWidth < upperBound;
+
+/**
+ * @param context - the window to determine the innerWidth from. This is purely for test purposes as it should always be the current window
+ */
+export function _getPlatform(context = window) {
+  const isInBounds = _isInbounds(context);
+  const MOBILE_VIEWPORT = {
+    lt: 768
+  };
+  const TABLET_VIEWPORT = {
+    lt: 992,
+    ge: 768
+  };
+  if (isInBounds(0, MOBILE_VIEWPORT.lt)) {
+    return 'mobile'
+  }
+  if (isInBounds(TABLET_VIEWPORT.ge, TABLET_VIEWPORT.lt)) {
+    return 'tablet'
+  }
+  return 'desktop';
+}
+
 registerBidder(spec);
diff --git a/test/spec/modules/sonobiBidAdapter_spec.js b/test/spec/modules/sonobiBidAdapter_spec.js
index 4fd5c13e65c..241c12a564f 100644
--- a/test/spec/modules/sonobiBidAdapter_spec.js
+++ b/test/spec/modules/sonobiBidAdapter_spec.js
@@ -1,5 +1,5 @@
 import { expect } from 'chai'
-import { spec } from 'modules/sonobiBidAdapter'
+import { spec, _getPlatform } from 'modules/sonobiBidAdapter'
 import { newBidder } from 'src/adapters/bidderFactory'
 
 describe('SonobiBidAdapter', () => {
@@ -139,6 +139,7 @@ describe('SonobiBidAdapter', () => {
       expect(bidRequests.data.pv).to.equal(bidRequestsPageViewID.data.pv)
       expect(bidRequests.data.hfa).to.not.exist
       expect(bidRequests.bidderRequests).to.eql(bidRequest);
+      expect(bidRequests.data.vp).to.equal('tablet');
     })
 
     it('should return a properly formatted request with hfa', () => {
@@ -159,7 +160,7 @@ describe('SonobiBidAdapter', () => {
       'url': 'https://apex.go.sonobi.com/trinity.json',
       'withCredentials': true,
       'data': {
-        'key_maker': '{"30b31c1838de1f":"1a2b3c4d5e6f1a2b3c4d|300x250,300x600|f=1.25","/7780971/sparks_prebid_LB|30b31c1838de1e":"300x250,300x600"}', 'ref': 'localhost:9876', 's': '2474372d-c0ff-4f46-aef4-a173058403d9', 'pv': 'c9cfc207-cd83-4a01-b591-8bb29389d4b0'
+        'key_maker': '{"30b31c1838de1f":"1a2b3c4d5e6f1a2b3c4d|300x250,300x600|f=1.25","/7780971/sparks_prebid_LB|30b31c1838de1e":"300x250,300x600"}', 'ref': 'localhost:9877', 's': '2474372d-c0ff-4f46-aef4-a173058403d9', 'pv': 'c9cfc207-cd83-4a01-b591-8bb29389d4b0'
       },
       'bidderRequests': [
         {
@@ -232,7 +233,7 @@ describe('SonobiBidAdapter', () => {
         'cpm': 1.07,
         'width': 300,
         'height': 600,
-        'ad': '<script type="text/javascript" src="https://mco-1-apex.go.sonobi.com/sbi.js?aid=30292e432662bd5f86d90774b944b039&as=null&ref=localhost:9876"></script>',
+        'ad': '<script type="text/javascript" src="https://mco-1-apex.go.sonobi.com/sbi.js?aid=30292e432662bd5f86d90774b944b039&as=null&ref=localhost:9877"></script>',
         'ttl': 500,
         'creativeId': '30292e432662bd5f86d90774b944b039',
         'netRevenue': true,
@@ -243,7 +244,7 @@ describe('SonobiBidAdapter', () => {
         'cpm': 1.25,
         'width': 300,
         'height': 250,
-        'ad': 'https://mco-1-apex.go.sonobi.com/vast.xml?vid=30292e432662bd5f86d90774b944b038&ref=localhost:9876',
+        'ad': 'https://mco-1-apex.go.sonobi.com/vast.xml?vid=30292e432662bd5f86d90774b944b038&ref=localhost:9877',
         'ttl': 500,
         'creativeId': '30292e432662bd5f86d90774b944b038',
         'netRevenue': true,
@@ -287,4 +288,15 @@ describe('SonobiBidAdapter', () => {
       expect(spec.getUserSyncs({ pixelEnabled: false }, bidResponse)).to.have.length(0);
     })
   })
+  describe('_getPlatform', () => {
+    it('should return mobile', () => {
+      expect(_getPlatform({innerWidth: 767})).to.equal('mobile')
+    })
+    it('should return tablet', () => {
+      expect(_getPlatform({innerWidth: 800})).to.equal('tablet')
+    })
+    it('should return desktop', () => {
+      expect(_getPlatform({innerWidth: 1000})).to.equal('desktop')
+    })
+  })
 })

From d23114c59769fc3be73c28cea8d191e553b1d316 Mon Sep 17 00:00:00 2001
From: Jonathan Go <jgo@sonobi.com>
Date: Thu, 26 Apr 2018 13:20:19 -0400
Subject: [PATCH 2/8] added lib_name and lib_v to trinity

---
 modules/sonobiBidAdapter.js | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/modules/sonobiBidAdapter.js b/modules/sonobiBidAdapter.js
index 170228dde7a..3fc59659072 100644
--- a/modules/sonobiBidAdapter.js
+++ b/modules/sonobiBidAdapter.js
@@ -49,6 +49,8 @@ export const spec = {
       'ref': utils.getTopWindowLocation().host,
       's': utils.generateUUID(),
       'pv': PAGEVIEW_ID,
+      'lib_name': 'prebid',
+      'lib_v': window.pbjs.version
     };
 
     if (validBidRequests[0].params.hfa) {

From ff5fabf22fe941cdce9b0982e207e7be9d59dd84 Mon Sep 17 00:00:00 2001
From: Jonathan Go <jgo@sonobi.com>
Date: Thu, 26 Apr 2018 14:04:05 -0400
Subject: [PATCH 3/8] return null from buildRequests if there is no keymakers

---
 modules/sonobiBidAdapter.js | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/modules/sonobiBidAdapter.js b/modules/sonobiBidAdapter.js
index 170228dde7a..376e2a0776e 100644
--- a/modules/sonobiBidAdapter.js
+++ b/modules/sonobiBidAdapter.js
@@ -55,6 +55,11 @@ export const spec = {
       payload.hfa = validBidRequests[0].params.hfa;
     }
 
+    // If there is no key_maker data, then dont make the request.
+    if (utils.isEmpty(data)) {
+      return null;
+    }
+
     return {
       method: 'GET',
       url: STR_ENDPOINT,

From ac246fd392f863ad63945f93ddfcc2f36544d3ac Mon Sep 17 00:00:00 2001
From: Jonathan Go <jgo@sonobi.com>
Date: Thu, 26 Apr 2018 14:08:59 -0400
Subject: [PATCH 4/8] added test case for empty keymaker

---
 test/spec/modules/sonobiBidAdapter_spec.js | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/test/spec/modules/sonobiBidAdapter_spec.js b/test/spec/modules/sonobiBidAdapter_spec.js
index 4fd5c13e65c..1d5eebc1719 100644
--- a/test/spec/modules/sonobiBidAdapter_spec.js
+++ b/test/spec/modules/sonobiBidAdapter_spec.js
@@ -151,6 +151,11 @@ describe('SonobiBidAdapter', () => {
       expect(bidRequests.data.s).not.to.be.empty
       expect(bidRequests.data.hfa).to.equal('hfakey')
     })
+    it('should return null if there is nothing to bid on', () => {
+      const bidRequests = spec.buildRequests([{params: {}}])
+      expect(bidRequests).to.equal(null);
+
+    })
   })
 
   describe('.interpretResponse', () => {

From ef36be22317db1c939b32b0f3cfd873001e2f6d4 Mon Sep 17 00:00:00 2001
From: Jonathan Go <jgo@sonobi.com>
Date: Thu, 26 Apr 2018 16:45:43 -0400
Subject: [PATCH 5/8] only importing functions we need from utils

---
 modules/sonobiBidAdapter.js | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/modules/sonobiBidAdapter.js b/modules/sonobiBidAdapter.js
index 697c6dd8d17..04d28375a96 100644
--- a/modules/sonobiBidAdapter.js
+++ b/modules/sonobiBidAdapter.js
@@ -1,11 +1,10 @@
 import { registerBidder } from 'src/adapters/bidderFactory';
-import { getTopWindowLocation, parseSizesInput } from 'src/utils';
-import * as utils from '../src/utils';
+import { getTopWindowLocation, parseSizesInput, logError, generateUUID, deepAccess } from '../src/utils';
 import { BANNER, VIDEO } from '../src/mediaTypes';
 
 const BIDDER_CODE = 'sonobi';
 const STR_ENDPOINT = 'https://apex.go.sonobi.com/trinity.json';
-const PAGEVIEW_ID = utils.generateUUID();
+const PAGEVIEW_ID = generateUUID();
 
 export const spec = {
   code: BIDDER_CODE,
@@ -37,7 +36,7 @@ export const spec = {
           [bid.bidId]: `${slotIdentifier}|${_validateSize(bid)}${_validateFloor(bid)}`
         }
       } else {
-        utils.logError(`The ad unit code or Sonobi Placement id for slot ${bid.bidId} is invalid`);
+        logError(`The ad unit code or Sonobi Placement id for slot ${bid.bidId} is invalid`);
       }
     });
 
@@ -47,7 +46,7 @@ export const spec = {
     const payload = {
       'key_maker': JSON.stringify(data),
       'ref': getTopWindowLocation().host,
-      's': utils.generateUUID(),
+      's': generateUUID(),
       'pv': PAGEVIEW_ID,
       'vp': _getPlatform()
     };
@@ -82,7 +81,7 @@ export const spec = {
     Object.keys(bidResponse.slots).forEach(slot => {
       const bidId = _getBidIdFromTrinityKey(slot);
       const bidRequest = bidderRequests.find(bidReqest => bidReqest.bidId === bidId);
-      const videoMediaType = utils.deepAccess(bidRequest, 'mediaTypes.video');
+      const videoMediaType = deepAccess(bidRequest, 'mediaTypes.video');
       const mediaType = bidRequest.mediaType || (videoMediaType ? 'video' : null);
       const createCreative = _creative(mediaType);
       const bid = bidResponse.slots[slot];

From 85c999477ba7d862e0df8a08d4e9ca366e67a02d Mon Sep 17 00:00:00 2001
From: Jonathan Go <jgo@sonobi.com>
Date: Fri, 27 Apr 2018 10:44:06 -0400
Subject: [PATCH 6/8] changed window.pbjs.version to use the gulp repalced
 macro .version$

---
 modules/sonobiBidAdapter.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/modules/sonobiBidAdapter.js b/modules/sonobiBidAdapter.js
index 3fc59659072..d1b3f01f15d 100644
--- a/modules/sonobiBidAdapter.js
+++ b/modules/sonobiBidAdapter.js
@@ -50,7 +50,7 @@ export const spec = {
       's': utils.generateUUID(),
       'pv': PAGEVIEW_ID,
       'lib_name': 'prebid',
-      'lib_v': window.pbjs.version
+      'lib_v': '$prebid.version$'
     };
 
     if (validBidRequests[0].params.hfa) {

From 6c3e3a82867f3cc456fd1ed93360aacdfa608f90 Mon Sep 17 00:00:00 2001
From: Jonathan Go <jgo@sonobi.com>
Date: Fri, 27 Apr 2018 14:55:59 -0400
Subject: [PATCH 7/8] fixed issue where isEmpty was being called from old utils
 var. Changed test port expectations to the original 9876 port

---
 modules/sonobiBidAdapter.js                | 4 ++--
 test/spec/modules/sonobiBidAdapter_spec.js | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/modules/sonobiBidAdapter.js b/modules/sonobiBidAdapter.js
index 29122dee5dc..dd684bf8767 100644
--- a/modules/sonobiBidAdapter.js
+++ b/modules/sonobiBidAdapter.js
@@ -1,5 +1,5 @@
 import { registerBidder } from 'src/adapters/bidderFactory';
-import { getTopWindowLocation, parseSizesInput, logError, generateUUID, deepAccess } from '../src/utils';
+import { getTopWindowLocation, parseSizesInput, logError, generateUUID, deepAccess, isEmpty } from '../src/utils';
 import { BANNER, VIDEO } from '../src/mediaTypes';
 
 const BIDDER_CODE = 'sonobi';
@@ -58,7 +58,7 @@ export const spec = {
     }
 
     // If there is no key_maker data, then dont make the request.
-    if (utils.isEmpty(data)) {
+    if (isEmpty(data)) {
       return null;
     }
 
diff --git a/test/spec/modules/sonobiBidAdapter_spec.js b/test/spec/modules/sonobiBidAdapter_spec.js
index 25e19582424..2069564bf01 100644
--- a/test/spec/modules/sonobiBidAdapter_spec.js
+++ b/test/spec/modules/sonobiBidAdapter_spec.js
@@ -165,7 +165,7 @@ describe('SonobiBidAdapter', () => {
       'url': 'https://apex.go.sonobi.com/trinity.json',
       'withCredentials': true,
       'data': {
-        'key_maker': '{"30b31c1838de1f":"1a2b3c4d5e6f1a2b3c4d|300x250,300x600|f=1.25","/7780971/sparks_prebid_LB|30b31c1838de1e":"300x250,300x600"}', 'ref': 'localhost:9877', 's': '2474372d-c0ff-4f46-aef4-a173058403d9', 'pv': 'c9cfc207-cd83-4a01-b591-8bb29389d4b0'
+        'key_maker': '{"30b31c1838de1f":"1a2b3c4d5e6f1a2b3c4d|300x250,300x600|f=1.25","/7780971/sparks_prebid_LB|30b31c1838de1e":"300x250,300x600"}', 'ref': 'localhost:9876', 's': '2474372d-c0ff-4f46-aef4-a173058403d9', 'pv': 'c9cfc207-cd83-4a01-b591-8bb29389d4b0'
       },
       'bidderRequests': [
         {
@@ -238,7 +238,7 @@ describe('SonobiBidAdapter', () => {
         'cpm': 1.07,
         'width': 300,
         'height': 600,
-        'ad': '<script type="text/javascript" src="https://mco-1-apex.go.sonobi.com/sbi.js?aid=30292e432662bd5f86d90774b944b039&as=null&ref=localhost:9877"></script>',
+        'ad': '<script type="text/javascript" src="https://mco-1-apex.go.sonobi.com/sbi.js?aid=30292e432662bd5f86d90774b944b039&as=null&ref=localhost:9876"></script>',
         'ttl': 500,
         'creativeId': '30292e432662bd5f86d90774b944b039',
         'netRevenue': true,
@@ -249,7 +249,7 @@ describe('SonobiBidAdapter', () => {
         'cpm': 1.25,
         'width': 300,
         'height': 250,
-        'ad': 'https://mco-1-apex.go.sonobi.com/vast.xml?vid=30292e432662bd5f86d90774b944b038&ref=localhost:9877',
+        'ad': 'https://mco-1-apex.go.sonobi.com/vast.xml?vid=30292e432662bd5f86d90774b944b038&ref=localhost:9876',
         'ttl': 500,
         'creativeId': '30292e432662bd5f86d90774b944b038',
         'netRevenue': true,

From cf45587dae1ab7c3ad640a023f76794fdbd65690 Mon Sep 17 00:00:00 2001
From: Jonathan Go <jgo@sonobi.com>
Date: Fri, 27 Apr 2018 15:01:58 -0400
Subject: [PATCH 8/8] fixed lint issue

---
 test/spec/modules/sonobiBidAdapter_spec.js | 1 -
 1 file changed, 1 deletion(-)

diff --git a/test/spec/modules/sonobiBidAdapter_spec.js b/test/spec/modules/sonobiBidAdapter_spec.js
index 2069564bf01..874c92e518e 100644
--- a/test/spec/modules/sonobiBidAdapter_spec.js
+++ b/test/spec/modules/sonobiBidAdapter_spec.js
@@ -155,7 +155,6 @@ describe('SonobiBidAdapter', () => {
     it('should return null if there is nothing to bid on', () => {
       const bidRequests = spec.buildRequests([{params: {}}])
       expect(bidRequests).to.equal(null);
-
     })
   })