From e174e2735453a1ddab3a3ab374af5df6a63771bf Mon Sep 17 00:00:00 2001 From: Serhii Kozlov Date: Tue, 20 Apr 2021 14:34:15 +0300 Subject: [PATCH] New Bid Adapter: Shinez (#6597) * Shinze adapter version 1.0.0 * map for arrays replaced with forEach, fpd temporarily removed * switched to only public interface testing * Usage of URL removed --- modules/shinezBidAdapter.js | 83 +++++++++++ modules/shinezBidAdapter.md | 33 +++++ test/spec/modules/shinezBidAdapter_spec.js | 152 +++++++++++++++++++++ 3 files changed, 268 insertions(+) create mode 100644 modules/shinezBidAdapter.js create mode 100644 modules/shinezBidAdapter.md create mode 100644 test/spec/modules/shinezBidAdapter_spec.js diff --git a/modules/shinezBidAdapter.js b/modules/shinezBidAdapter.js new file mode 100644 index 00000000000..d5734d23fdc --- /dev/null +++ b/modules/shinezBidAdapter.js @@ -0,0 +1,83 @@ +import { registerBidder } from '../src/adapters/bidderFactory.js'; +import { BANNER } from '../src/mediaTypes.js'; + +const BIDDER_CODE = 'shinez'; +const BIDDER_SHORT_CODE = 'shz'; +const ADAPTER_VERSION = '1.0.0'; + +const TARGET_URL = 'https://shinez-ssp.shinez.workers.dev/prebid'; + +export const spec = { + code: BIDDER_CODE, + version: ADAPTER_VERSION, + aliases: { + code: BIDDER_SHORT_CODE + }, + supportedMediaTypes: [ BANNER ], + isBidRequestValid: isBidRequestValid, + buildRequests: buildRequests, + interpretResponse: interpretResponse, +}; + +export const internal = { + TARGET_URL +} + +function isBidRequestValid(bid) { + return !!(bid && bid.params && + bid.params.placementId && typeof bid.params.placementId === 'string' && + (bid.params.unit == null || (typeof bid.params.unit === 'string' && bid.params.unit.length > 0)) + ); +} + +function buildRequests(validBidRequests, bidderRequest) { + const utcOffset = (new Date()).getTimezoneOffset(); + const data = []; + validBidRequests.forEach(function(bidRequest) { + data.push(_buildServerBidRequest(bidRequest, bidderRequest, utcOffset)); + }); + const request = { + method: 'POST', + url: TARGET_URL, + data: data + }; + return request; +} + +function interpretResponse(serverResponse, request) { + const bids = []; + serverResponse.body.forEach(function(serverBid) { + bids.push(_convertServerBid(serverBid)); + }); + return bids; +} + +function _buildServerBidRequest(bidRequest, bidderRequest, utcOffset) { + return { + bidId: bidRequest.bidId, + transactionId: bidRequest.transactionId, + crumbs: bidRequest.crumbs, + mediaTypes: bidRequest.mediaTypes, + refererInfo: bidderRequest.refererInfo, + placementId: bidRequest.params.placementId, + utcOffset: utcOffset, + adUnitCode: bidRequest.adUnitCode, + unit: bidRequest.params.unit + } +} + +function _convertServerBid(response) { + return { + requestId: response.bidId, + cpm: response.cpm, + currency: response.currency, + width: response.width, + height: response.height, + ad: response.ad, + ttl: response.ttl, + creativeId: response.creativeId, + netRevenue: response.netRevenue + }; +} + +registerBidder(spec); diff --git a/modules/shinezBidAdapter.md b/modules/shinezBidAdapter.md new file mode 100644 index 00000000000..e040cfbf36b --- /dev/null +++ b/modules/shinezBidAdapter.md @@ -0,0 +1,33 @@ +# Overview + +``` +Module Name: Shinez Bidder Adapter +Module Type: Bidder Adapter +Maintainer: tech-team@shinez.io +``` + +# Description + +Connects to shinez.io demand sources. + +The Shinez adapter requires setup and approval from the Shinez team. +Please reach out to tech-team@shinez.io for more information. + +# Test Parameters + +```javascript +var adUnits = [{ + code: "test-div", + mediaTypes: { + banner: { + sizes: [[300, 250]] + } + }, + bids: [{ + bidder: "shinez", + params: { + placementId: "00654321" + } + }] +}]; +``` \ No newline at end of file diff --git a/test/spec/modules/shinezBidAdapter_spec.js b/test/spec/modules/shinezBidAdapter_spec.js new file mode 100644 index 00000000000..cc3c2451c5d --- /dev/null +++ b/test/spec/modules/shinezBidAdapter_spec.js @@ -0,0 +1,152 @@ +import { expect } from 'chai'; +import sinon from 'sinon'; +import { spec, internal } from 'modules/shinezBidAdapter.js'; + +describe('shinezBidAdapter', () => { + let sandbox; + beforeEach(() => { + sandbox = sinon.sandbox.create(); + }); + afterEach(() => { + sandbox.restore(); + }); + describe('isBidRequestValid', () => { + const cases = [ + [ + 'should return false when placementId is missing', + { + params: {}, + }, + false, + ], + [ + 'should return false when placementId has wrong type', + { + params: { + placementId: 123, + }, + }, + false, + ], + [ + 'should return false when unit has wrong type', + { + params: { + placementId: '00654321', + unit: 150, + }, + }, + false, + ], + [ + 'should return true when required params found and valid', + { + params: { + placementId: '00654321', + }, + }, + true, + ], + [ + 'should return true when all params found and valid', + { + params: { + placementId: '00654321', + unit: '__header-bid-1', + }, + }, + true, + ], + ]; + cases.map(([description, request, expected]) => { + it(description, () => { + const result = spec.isBidRequestValid(request); + expect(result).to.be.equal(expected); + }); + }); + }); + describe('buildRequests', () => { + it('should build server request correctly', () => { + const utcOffset = 300; + const validBidRequests = [ + { + params: { + placementId: '00654321', + unit: 'header-bid-tag-1-shinez', + }, + crumbs: { + pubcid: 'c8584a82-bec3-4347-8d3e-e7612438a161', + }, + mediaTypes: { + banner: { + sizes: [[300, 250]], + }, + }, + adUnitCode: 'header-bid-tag-1', + transactionId: '665760dc-a249-4be7-ae86-91f417b2c65d', + }, + ]; + const bidderRequest = { + refererInfo: { + referer: 'http://site-with-ads.com', + }, + }; + sandbox.stub(Date.prototype, 'getTimezoneOffset').returns(utcOffset); + const result = spec.buildRequests(validBidRequests, bidderRequest); + const expectedData = [ + { + bidId: validBidRequests[0].bidId, + transactionId: validBidRequests[0].transactionId, + crumbs: validBidRequests[0].crumbs, + mediaTypes: validBidRequests[0].mediaTypes, + refererInfo: bidderRequest.refererInfo, + adUnitCode: validBidRequests[0].adUnitCode, + utcOffset: utcOffset, + placementId: validBidRequests[0].params.placementId, + unit: validBidRequests[0].params.unit, + }, + ]; + expect(result.method, "request should be POST'ed").equal('POST'); + expect(result.url, 'request should be send to correct url').equal( + internal.TARGET_URL + ); + expect(result.data, 'request should have correct payload').to.deep.equal( + expectedData + ); + }); + }); + describe('interpretResponse', () => { + it('should interpret bid responses correctly', () => { + const response = { + body: [ + { + bidId: '2ece6496f4d0c9', + cpm: 0.03, + currency: 'USD', + width: 300, + height: 250, + ad: `

The Ad

`, + ttl: 60, + creativeId: 'V8qlA6guwm', + netRevenue: true, + }, + ], + }; + const bids = [ + { + requestId: response.body[0].bidId, + cpm: response.body[0].cpm, + currency: response.body[0].currency, + width: response.body[0].width, + height: response.body[0].height, + ad: response.body[0].ad, + ttl: response.body[0].ttl, + creativeId: response.body[0].creativeId, + netRevenue: response.body[0].netRevenue, + }, + ]; + const result = spec.interpretResponse(response); + expect(result).to.deep.equal(bids); + }); + }); +});