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

Add HPMD Network bid adapter #3764

Merged
merged 1 commit into from
May 2, 2019
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
96 changes: 96 additions & 0 deletions modules/hpmdnetworkBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { registerBidder } from 'src/adapters/bidderFactory';
import { BANNER } from 'src/mediaTypes';

const BIDDER_CODE = 'hpmdnetwork';
const BIDDER_CODE_ALIAS = 'hpmd';
const HPMDNETWORK_HOST = '//banner.hpmdnetwork.ru/bidder/request';
const DEFAULT_TTL = 300;
const DEFAULT_CURRENCY = 'RUB';

export const spec = {
code: BIDDER_CODE,
aliases: [ BIDDER_CODE_ALIAS ],
supportedMediaTypes: [ BANNER ],
isBidRequestValid: isBidRequestValid,
buildRequests: buildRequests,
interpretResponse: interpretResponse,
};

function isBidRequestValid(bid) {
const { placementId } = bid.params;
return !!placementId;
}

function buildRequests(validBidRequests, bidderRequest) {
const payload = {};
payload.places = [];

validBidRequests.forEach((bidRequest) => {
const place = {
id: bidRequest.bidId,
placementId: bidRequest.params.placementId + '',
};
payload.places.push(place);
});

payload.url = bidderRequest.refererInfo.referer;
payload.settings = { currency: DEFAULT_CURRENCY };

return {
method: 'POST',
url: HPMDNETWORK_HOST,
data: payload,
};
}

function interpretResponse(serverResponse) {
const { body } = serverResponse;
const bidResponses = [];

if (body.bids) {
body.bids.forEach((bid) => {
const size = getCreativeSize(bid);
const bidResponse = {
requestId: bid.id,
cpm: bid.cpm,
ad: wrapDisplayUrl(bid.displayUrl),
width: size.width,
height: size.height,
creativeId: bid.creativeId || generateRandomInt(),
currency: bid.currency || DEFAULT_CURRENCY,
netRevenue: true,
ttl: bid.ttl || DEFAULT_TTL,
};

bidResponses.push(bidResponse);
});
}

return bidResponses;
}

function wrapDisplayUrl(displayUrl) {
return `<html><head></head><body style="margin:0;width:0;height:0;"><script async src="${displayUrl}"></script></body></html>`;
}

function getCreativeSize(creativeSize) {
const size = {
width: 1,
height: 1,
};

if (!!creativeSize.width && creativeSize.width !== -1) {
size.width = creativeSize.width;
}
if (!!creativeSize.height && creativeSize.height !== -1) {
size.height = creativeSize.height;
}

return size;
}

function generateRandomInt() {
return Math.random().toString(16).substring(2);
}

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

Module Name: HPMD Network Bidder Adapter

Module Type: Bidder Adapter

Maintainer: a.fominov@hpmdnetwork.ru

# Description

You can use this adapter to get a bid from HPMD Network.

About us : https://www.hpmdnetwork.ru/


# Test Parameters
```javascript
var adUnits = [
{
code: 'test-div',
bids: [
{
bidder: "hpmdnetwork",
params: {
placementId: "123"
}
}
]
}
];
```

148 changes: 148 additions & 0 deletions test/spec/modules/hpmdnetworkBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { expect } from 'chai';
import { spec } from 'modules/hpmdnetworkBidAdapter';

describe('HPMDNetwork Adapter', function() {
describe('isBidRequestValid', function () {
it('should return true when required params found', function () {
const validBid = {
bidder: 'hpmdnetwork',
params: {
placementId: '1'
}
};

expect(spec.isBidRequestValid(validBid)).to.equal(true);
});

it('should return false for when required params are not passed', function () {
const invalidBid = {
bidder: 'hpmdnetwork',
params: {}
};

expect(spec.isBidRequestValid(invalidBid)).to.equal(false);
});
});

describe('buildRequests', function () {
const bidRequests = [
{
bidId: 'bid1',
bidder: 'hpmdnetwork',
params: {
placementId: '1'
}
},
{
bidId: 'bid2',
bidder: 'hpmdnetwork',
params: {
placementId: '2',
}
}
];
const bidderRequest = {
refererInfo: {
referer: 'https://example.com?foo=bar'
}
};

const bidRequest = spec.buildRequests(bidRequests, bidderRequest);

it('should build single POST request for multiple bids', function() {
expect(bidRequest.method).to.equal('POST');
expect(bidRequest.url).to.equal('//banner.hpmdnetwork.ru/bidder/request');
expect(bidRequest.data).to.be.an('object');
expect(bidRequest.data.places).to.be.an('array');
expect(bidRequest.data.places).to.have.lengthOf(2);
});

it('should pass bid parameters', function() {
const place1 = bidRequest.data.places[0];
const place2 = bidRequest.data.places[1];

expect(place1.placementId).to.equal('1');
expect(place2.placementId).to.equal('2');
expect(place1.id).to.equal('bid1');
expect(place2.id).to.equal('bid2');
});

it('should pass site parameters', function() {
const url = bidRequest.data.url;

expect(url).to.be.an('String');
expect(url).to.equal('https://example.com?foo=bar');
});

it('should pass settings', function() {
const settings = bidRequest.data.settings;

expect(settings).to.be.an('object');
expect(settings.currency).to.equal('RUB');
});
});

describe('interpretResponse', function () {
const serverResponse = {
body: {
'bids':
[
{
'cpm': 20,
'currency': 'RUB',
'displayUrl': '//banner.hpmdnetwork.ru/bidder/display?dbid=0&vbid=168',
'id': '1',
'creativeId': '11111',
},
{
'cpm': 30,
'currency': 'RUB',
'displayUrl': '//banner.hpmdnetwork.ru/bidder/display?dbid=0&vbid=170',
'id': '2',
'creativeId': '22222',
'width': 300,
'height': 250,
},
]
}
};

const bids = spec.interpretResponse(serverResponse);

it('should return empty array for response with no bids', function() {
const emptyBids = spec.interpretResponse({ body: {} });

expect(emptyBids).to.have.lengthOf(0);
});

it('should parse all bids from response', function() {
expect(bids).to.have.lengthOf(2);
});

it('should parse bid without sizes', function() {
expect(bids[0].requestId).to.equal('1');
expect(bids[0].cpm).to.equal(20);
expect(bids[0].width).to.equal(1);
expect(bids[0].height).to.equal(1);
expect(bids[0].ttl).to.equal(300);
expect(bids[0].currency).to.equal('RUB');
expect(bids[0]).to.have.property('creativeId');
expect(bids[0].creativeId).to.equal('11111');
expect(bids[0].netRevenue).to.equal(true);
expect(bids[0].ad).to.include('<script async src="//banner.hpmdnetwork.ru/bidder/display?dbid=0&vbid=168"></script>');
});

it('should parse bid with sizes', function() {
expect(bids[1].requestId).to.equal('2');
expect(bids[1].cpm).to.equal(30);
expect(bids[1].width).to.equal(300);
expect(bids[1].height).to.equal(250);
expect(bids[1].ttl).to.equal(300);
expect(bids[1].currency).to.equal('RUB');
expect(bids[1]).to.have.property('creativeId');
expect(bids[1].creativeId).to.equal('22222');
expect(bids[1].netRevenue).to.equal(true);
expect(bids[1].ad).to.include('<script async src="//banner.hpmdnetwork.ru/bidder/display?dbid=0&vbid=170"></script>');
});
});
});