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 peak adapter #2040

Merged
merged 1 commit into from
Feb 5, 2018
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
97 changes: 97 additions & 0 deletions modules/peak226BidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { registerBidder } from 'src/adapters/bidderFactory';
import { BANNER } from 'src/mediaTypes';
import { getTopWindowUrl, logWarn } from 'src/utils';

const BIDDER_CODE = 'peak226';
const URL = '//a.ad216.com/header_bid';

export const spec = {
code: BIDDER_CODE,

supportedMediaTypes: [BANNER],

isBidRequestValid: function (bid) {
const { params } = bid;

return !!params.uid;
},

buildRequests: function (validBidRequests) {
const bidsMap = validBidRequests.reduce((res, bid) => {
const { uid } = bid.params;

res[uid] = res[uid] || [];
res[uid].push(bid);

return res;
}, {});

return {
method: 'GET',
url:
URL +
toQueryString({
u: getTopWindowUrl(),
auids: Object.keys(bidsMap).join(',')
}),
bidsMap
};
},

interpretResponse: function (serverResponse, { bidsMap }) {
const response = serverResponse.body;
const bidResponses = [];

if (!response) {
logWarn(`No response from ${spec.code} bidder`);

return bidResponses;
}

if (!response.seatbid || !response.seatbid.length) {
logWarn(`No seatbid in response from ${spec.code} bidder`);

return bidResponses;
}

response.seatbid.forEach((seatbid, i) => {
if (!seatbid.bid || !seatbid.bid.length) {
logWarn(`No bid in seatbid[${i}] response from ${spec.code} bidder`);
return;
}
seatbid.bid.forEach(responseBid => {
const requestBids = bidsMap[responseBid.auid];

requestBids.forEach(requestBid => {
bidResponses.push({
requestId: requestBid.bidId,
bidderCode: spec.code,
width: responseBid.w,
height: responseBid.h,
mediaType: BANNER,
creativeId: responseBid.auid,
ad: responseBid.adm,
cpm: responseBid.price,
currency: 'USD',
netRevenue: true,
ttl: 360
});
});
});
});

return bidResponses;
}
};

function toQueryString(obj) {
return Object.keys(obj).reduce(
(str, key, i) =>
typeof obj[key] === 'undefined' || obj[key] === ''
? str
: `${str}${str ? '&' : '?'}${key}=${encodeURIComponent(obj[key])}`,
''
);
}

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

```
Module Name: Peak226 Bidder Adapter
Module Type: Bidder Adapter
Maintainer: support@edge226.com
```

# Description

Module that connects to Peak226's demand sources

# Test Parameters

```
var adUnits = [
{
code: "test-div",
sizes: [[300, 250]],
mediaType: "banner",
bids: [
{
bidder: "peak226",
params: {
uid: 76131369
}
}
]
}
];
```
114 changes: 114 additions & 0 deletions test/spec/modules/peak226BidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { expect } from 'chai';
import { spec } from 'modules/peak226BidAdapter';
import { newBidder } from 'src/adapters/bidderFactory';

const URL = 'a.ad216.com/header_bid';

describe('PeakAdapter', () => {
const adapter = newBidder(spec);

describe('isBidRequestValid', () => {
it('should return true when required params found', () => {
const bid = {
params: {
uid: 123
}
};

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

it('should return false when required params are not passed', () => {
const bid = {
params: {}
};

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

xdescribe('buildRequests', () => {
const bidRequests = [
{
params: {
uid: '1234'
}
}
];

it('sends bid request to URL via GET', () => {
const request = spec.buildRequests(bidRequests);

expect(request.url).to.equal(`${URL}?uids=1234`);
expect(request.method).to.equal('GET');
});
});

describe('interpretResponse', () => {
it('should handle empty response', () => {
let bids = spec.interpretResponse(
{},
{
bidsMap: {}
}
);

expect(bids).to.be.lengthOf(0);
});

it('should handle no seatbid returned', () => {
let response = {};

let bids = spec.interpretResponse(
{ body: response },
{
bidsMap: {}
}
);

expect(bids).to.be.lengthOf(0);
});

it('should handle empty seatbid returned', () => {
let response = { seatbid: [] };

let bids = spec.interpretResponse(
{ body: response },
{
bidsMap: {}
}
);

expect(bids).to.be.lengthOf(0);
});

it('should handle seatbid returned bids', () => {
const bidsMap = { 1: [{ bidId: 11 }] };
const bid = {
price: 0.2,
auid: 1,
h: 250,
w: 300,
adm: 'content'
};
const response = {
seatbid: [
{
seat: 'foo',
bid: [bid]
}
]
};

let bids = spec.interpretResponse({ body: response }, { bidsMap });

expect(bids).to.be.lengthOf(1);

expect(bids[0].cpm).to.equal(bid.price);
expect(bids[0].width).to.equal(bid.w);
expect(bids[0].height).to.equal(bid.h);
expect(bids[0].ad).to.equal(bid.adm);
expect(bids[0].bidderCode).to.equal(spec.code);
});
});
});