From bfff0e75536246ead0e057844da8a4fa68038043 Mon Sep 17 00:00:00 2001 From: omnifer Date: Wed, 17 Apr 2019 12:39:13 +0300 Subject: [PATCH] Add HPMD Network bid adapter --- modules/hpmdnetworkBidAdapter.js | 96 ++++++++++++ modules/hpmdnetworkBidAdapter.md | 32 ++++ .../modules/hpmdnetworkBidAdapter_spec.js | 148 ++++++++++++++++++ 3 files changed, 276 insertions(+) create mode 100644 modules/hpmdnetworkBidAdapter.js create mode 100644 modules/hpmdnetworkBidAdapter.md create mode 100644 test/spec/modules/hpmdnetworkBidAdapter_spec.js diff --git a/modules/hpmdnetworkBidAdapter.js b/modules/hpmdnetworkBidAdapter.js new file mode 100644 index 00000000000..ad17caba7bc --- /dev/null +++ b/modules/hpmdnetworkBidAdapter.js @@ -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 ``; +} + +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); diff --git a/modules/hpmdnetworkBidAdapter.md b/modules/hpmdnetworkBidAdapter.md new file mode 100644 index 00000000000..b7ac51a9311 --- /dev/null +++ b/modules/hpmdnetworkBidAdapter.md @@ -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" + } + } + ] + } + ]; +``` + diff --git a/test/spec/modules/hpmdnetworkBidAdapter_spec.js b/test/spec/modules/hpmdnetworkBidAdapter_spec.js new file mode 100644 index 00000000000..37ec44f07c4 --- /dev/null +++ b/test/spec/modules/hpmdnetworkBidAdapter_spec.js @@ -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(''); + }); + + 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(''); + }); + }); +});