Skip to content

Commit

Permalink
MarsMedia adapter: Update prebid api (#3978)
Browse files Browse the repository at this point in the history
* Create Mars bid adapter

* Fix ESLint errors

* Add more tab between 86-97 for ESLint
  • Loading branch information
vladi-mmg authored and robertrmartinez committed Jul 30, 2019
1 parent f013970 commit c079efe
Show file tree
Hide file tree
Showing 3 changed files with 284 additions and 0 deletions.
130 changes: 130 additions & 0 deletions modules/marsmediaBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import * as utils from '../src/utils';
import {registerBidder} from '../src/adapters/bidderFactory';
const BIDDER_CODE = 'marsmedia';

function getDomain() {
if (!utils.inIframe()) {
return window.location.hostname
}
let origins = window.document.location.ancestorOrigins
if (origins && origins.length > 0) {
return origins[origins.length - 1]
}
}

export const spec = {
code: BIDDER_CODE,
aliases: ['mars'],
isBidRequestValid: function(bid) {
return (bid.params.publisherID !== null);
},
buildRequests: function(validBidRequests, bidderRequest) {
try {
let protocol = (window.location.protocol === 'https:');
const parse = getSize(validBidRequests[0].sizes);
const publisherId = validBidRequests[0].params.publisherID;
const payload = {
id: validBidRequests[0].bidId,
cur: ['USD'],

language: window.navigator.userLanguage || window.navigator.language,
site: {
id: publisherId,
domain: getDomain(),
page: document.URL,
ref: document.referrer,
publisher: {
id: publisherId,
domain: getDomain()
}
},
imp: [{
id: utils.getUniqueIdentifierStr(),
banner: {
w: parse.width,
h: parse.height,
secure: protocol
},
bidfloor: parseFloat(validBidRequests[0].params.floor) > 0 ? validBidRequests[0].params.floor : 0
}],
device: {
ua: navigator.userAgent
},
user: {
id: publisherId
},
publisher: {
id: publisherId,
domain: getDomain()
}
};

if (bidderRequest && bidderRequest.gdprConsent) {
payload.gdpr = {
applies: bidderRequest.gdprConsent.gdprApplies,
consent: bidderRequest.gdprConsent.consentString
};
}

return {
method: 'POST',
url: '//bid306.rtbsrv.com/bidder/?bid=3mhdom',
data: JSON.stringify(payload)
};
} catch (e) {
utils.logError(e, {validBidRequests, bidderRequest});
}
},
interpretResponse: function(serverResponse, bidRequest) {
const bidResponses = [];
let res = serverResponse.body;
if (!res) {
return []
}

for (let x = 0; x < res.seatbid.length; x++) {
var bidAd = res.seatbid[x].bid[0];

bidResponses.push({
requestId: res.id,
cpm: Number(bidAd.price),
width: bidAd.w,
height: bidAd.h,
ad: bidAd.adm,
ttl: 60,
creativeId: bidAd.cid,
netRevenue: true,
currency: 'USD'
})
}

return bidResponses;
},
getUserSyncs: function(syncOptions, serverResponses) {
return [];
}
};

function getSize(requestSizes) {
const parsed = {};
const size = utils.parseSizesInput(requestSizes)[0];

if (typeof size !== 'string') {
return parsed;
}

const parsedSize = size.toUpperCase().split('X');
const width = parseInt(parsedSize[0], 10);
if (width) {
parsed.width = width;
}

const height = parseInt(parsedSize[1], 10);
if (height) {
parsed.height = height;
}

return parsed;
}

registerBidder(spec);
32 changes: 32 additions & 0 deletions modules/marsmediaBidAdapter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Overview

```
Module Name: Mars Media Group (mars.media) Bidder Adapter
Module Type: Bidder Adapter
Maintainer: vladi@mars.media
```

# Description

Prebid adapter for Mars Media Group RTB. Requires approval and account setup.

# Test Parameters

## Web
```
var adUnits = [
{
code: 'test-div',
sizes: [[300, 250]],
bids: [
{
bidder: "marsmedia",
params: {
publisherID: 9999,
floor: 0.11
}
}
]
}
];
```
122 changes: 122 additions & 0 deletions test/spec/modules/marsmediaBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { expect } from 'chai'
import { spec, _getPlatform } from 'modules/marsmediaBidAdapter'
import { newBidder } from 'src/adapters/bidderFactory'

describe('marsmediaBidAdapter', function () {
const adapter = newBidder(spec)

let bidRequest = {
'bidId': '123',
'sizes': [[ 300, 250 ]],
'params': {
'publisherID': 9999,
'floor': 0.1
}
}

describe('codes', function () {
it('should return a bidder code of marsmedia', function () {
expect(spec.code).to.equal('marsmedia')
})
it('should alias mars', function () {
expect(spec.aliases.length > 0 && spec.aliases[0] === 'mars').to.be.true
})
})

describe('isBidRequestValid', function () {
it('should return true if all params present', function () {
expect(spec.isBidRequestValid(bidRequest)).to.be.true
})

it('should return false if any parameter missing', function () {
expect(spec.isBidRequestValid(Object.assign(bidRequest, { params: { publisherID: null } }))).to.be.false
})
})

describe('buildRequests', function () {
let req = spec.buildRequests([ bidRequest ], { refererInfo: { } })
let rdata

it('should return request object', function () {
expect(req).to.not.be.null
})

it('should build request data', function () {
expect(req.data).to.not.be.null
})

it('should include one request', function () {
rdata = JSON.parse(req.data)
expect(rdata.imp.length).to.equal(1)
})

it('should include all publisher params', function () {
let r = rdata.imp[0]
expect(r.publisherID !== null).to.be.true
})
})

describe('interpretResponse', function () {
let response;
beforeEach(function () {
response = {
body: {
'id': '37386aade21a71',
'seatbid': [{
'bid': [{
'id': '1',
'impid': '1',
'cid': '1',
'price': 0.1,
'nurl': '<!-- NURL -->',
'adm': '<!-- Creative -->',
'w': 320,
'h': 250
}]
}]
}
};
});

it('should get the correct bid response', function () {
let expectedResponse = [{
'requestId': '37386aade21a71',
'cpm': 0.1,
'width': 320,
'height': 250,
'creativeId': '1',
'currency': 'USD',
'netRevenue': true,
'ad': `<!-- Creative -->`,
'ttl': 60
}];

let result = spec.interpretResponse(response);
expect(result[0]).to.deep.equal(expectedResponse[0]);
});

it('handles empty bid response', function () {
let response = {
body: ''
};
let result = spec.interpretResponse(response);
expect(result.length).to.equal(0);
});
});

describe('getUserSyncs', function () {
/* it('should return iframe sync', function () {
let sync = spec.getUserSyncs({ iframeEnabled: true })
expect(sync.length).to.equal(1)
expect(sync[0].type === 'iframe')
expect(typeof sync[0].url === 'string')
})
it('should return pixel sync', function () {
let sync = spec.getUserSyncs({ pixelEnabled: true })
expect(sync.length).to.equal(1)
expect(sync[0].type === 'image')
expect(typeof sync[0].url === 'string')
}) */
})
})

0 comments on commit c079efe

Please sign in to comment.