Skip to content

Commit 6311c46

Browse files
SublimeLeohellsingblackfgcloutier
authored
SublimeBidAdapter: Update to version 0.5.1 (#4977)
* add prebid version of adapter * Feature/update sublime adapter (#21) * Update sublimeBidAdapter to 0.5.1 * Add tests for private functions * Remove window.sublime * Update pixel name for bid event * Remove pixels on non-event and add onBidWon (#22) * add prebid version of adapter * Feature/update sublime adapter (#21) * Update sublimeBidAdapter to 0.5.1 * Add tests for private functions * Remove window.sublime * Update pixel name for bid event * Remove pixels on non-event and add onBidWon * Incremente version of sublimeBidAdapter * Renamed pixel for timeout and introduce gvlid * Remove unnecessary params for sendEvent Co-Authored-By: fgcloutier <[email protected]> Co-authored-by: Gaby <[email protected]> Co-authored-by: fgcloutier <[email protected]> * Remove trailing-space * Fix version in tests Co-authored-by: Gaby <[email protected]> Co-authored-by: fgcloutier <[email protected]>
1 parent f0a8abd commit 6311c46

File tree

2 files changed

+223
-114
lines changed

2 files changed

+223
-114
lines changed

modules/sublimeBidAdapter.js

Lines changed: 195 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,218 @@
11
import { registerBidder } from '../src/adapters/bidderFactory.js';
22
import { config } from '../src/config.js';
3+
import * as utils from '../src/utils.js';
4+
import * as url from '../src/url.js';
35

46
const BIDDER_CODE = 'sublime';
7+
const BIDDER_GVLID = 114;
58
const DEFAULT_BID_HOST = 'pbjs.sskzlabs.com';
69
const DEFAULT_CURRENCY = 'EUR';
710
const DEFAULT_PROTOCOL = 'https';
811
const DEFAULT_TTL = 600;
9-
const SUBLIME_VERSION = '0.4.0';
12+
const SUBLIME_ANTENNA = 'antenna.ayads.co';
13+
const SUBLIME_VERSION = '0.5.2';
14+
15+
/**
16+
* Debug log message
17+
* @param {String} msg
18+
* @param {Object=} obj
19+
*/
20+
export function log(msg, obj) {
21+
utils.logInfo('SublimeBidAdapter - ' + msg, obj);
22+
}
23+
24+
// Default state
25+
export const state = {
26+
zoneId: '',
27+
transactionId: ''
28+
};
1029

11-
export const spec = {
12-
code: BIDDER_CODE,
13-
gvlid: 114,
14-
aliases: [],
30+
/**
31+
* Set a new state
32+
* @param {Object} value
33+
*/
34+
export function setState(value) {
35+
Object.assign(state, value);
36+
log('State has been updated :', state);
37+
}
38+
39+
/**
40+
* Send pixel to our debug endpoint
41+
* @param {string} eventName - Event name that will be send in the e= query string
42+
*/
43+
export function sendEvent(eventName) {
44+
const ts = Date.now();
45+
const eventObject = {
46+
t: ts,
47+
tse: ts,
48+
z: state.zoneId,
49+
e: eventName,
50+
src: 'pa',
51+
puid: state.transactionId,
52+
trId: state.transactionId,
53+
ver: SUBLIME_VERSION,
54+
};
55+
56+
log('Sending pixel for event: ' + eventName, eventObject);
57+
58+
const queryString = url.formatQS(eventObject);
59+
utils.triggerPixel('https://' + SUBLIME_ANTENNA + '/?' + queryString);
60+
}
61+
62+
/**
63+
* Determines whether or not the given bid request is valid.
64+
*
65+
* @param {BidRequest} bid The bid params to validate.
66+
* @return {Boolean} True if this is a valid bid, and false otherwise.
67+
*/
68+
function isBidRequestValid(bid) {
69+
return !!Number(bid.params.zoneId);
70+
}
71+
72+
/**
73+
* Make a server request from the list of BidRequests.
74+
*
75+
* @param {BidRequest[]} validBidRequests - An array of bids
76+
* @param {Object} bidderRequest - Info describing the request to the server.
77+
* @return {ServerRequest|ServerRequest[]} - Info describing the request to the server.
78+
*/
79+
function buildRequests(validBidRequests, bidderRequest) {
80+
const commonPayload = {
81+
pbav: SUBLIME_VERSION,
82+
// Current Prebid params
83+
prebidVersion: '$prebid.version$',
84+
currencyCode: config.getConfig('currency.adServerCurrency') || DEFAULT_CURRENCY,
85+
timeout: (typeof bidderRequest === 'object' && !!bidderRequest) ? bidderRequest.timeout : config.getConfig('bidderTimeout'),
86+
};
87+
88+
// RefererInfo
89+
if (bidderRequest && bidderRequest.refererInfo) {
90+
commonPayload.referer = bidderRequest.refererInfo.referer;
91+
commonPayload.numIframes = bidderRequest.refererInfo.numIframes;
92+
}
93+
// GDPR handling
94+
if (bidderRequest && bidderRequest.gdprConsent) {
95+
commonPayload.gdprConsent = bidderRequest.gdprConsent.consentString;
96+
commonPayload.gdpr = bidderRequest.gdprConsent.gdprApplies; // we're handling the undefined case server side
97+
}
98+
99+
return validBidRequests.map(bid => {
100+
const bidHost = bid.params.bidHost || DEFAULT_BID_HOST;
101+
const protocol = bid.params.protocol || DEFAULT_PROTOCOL;
102+
103+
setState({
104+
transactionId: bid.transactionId,
105+
zoneId: bid.params.zoneId,
106+
debug: bid.params.debug || false,
107+
});
15108

16-
/**
17-
* Determines whether or not the given bid request is valid.
18-
*
19-
* @param {BidRequest} bid The bid params to validate.
20-
* @return boolean True if this is a valid bid, and false otherwise.
21-
*/
22-
isBidRequestValid: (bid) => {
23-
return !!bid.params.zoneId;
24-
},
25-
26-
/**
27-
* Make a server request from the list of BidRequests.
28-
*
29-
* @param {BidRequest[]} validBidRequests An array of bids
30-
* @param {Object} bidderRequest - Info describing the request to the server.
31-
* @return ServerRequest Info describing the request to the server.
32-
*/
33-
buildRequests: (validBidRequests, bidderRequest) => {
34-
let commonPayload = {
35-
sublimeVersion: SUBLIME_VERSION,
36-
// Current Prebid params
37-
prebidVersion: '$prebid.version$',
38-
currencyCode: config.getConfig('currency.adServerCurrency') || DEFAULT_CURRENCY,
39-
timeout: config.getConfig('bidderTimeout'),
109+
const bidPayload = {
110+
adUnitCode: bid.adUnitCode,
111+
auctionId: bid.auctionId,
112+
bidder: bid.bidder,
113+
bidderRequestId: bid.bidderRequestId,
114+
bidRequestsCount: bid.bidRequestsCount,
115+
requestId: bid.bidId,
116+
sizes: bid.sizes.map(size => ({
117+
w: size[0],
118+
h: size[1],
119+
})),
120+
transactionId: bid.transactionId,
121+
zoneId: bid.params.zoneId,
40122
};
41123

42-
// RefererInfo
43-
if (bidderRequest && bidderRequest.refererInfo) {
44-
commonPayload.referer = bidderRequest.refererInfo.referer;
45-
commonPayload.numIframes = bidderRequest.refererInfo.numIframes;
124+
const payload = Object.assign({}, commonPayload, bidPayload);
125+
126+
return {
127+
method: 'POST',
128+
url: protocol + '://' + bidHost + '/bid',
129+
data: payload,
130+
options: {
131+
contentType: 'application/json',
132+
withCredentials: true
133+
},
46134
}
47-
// GDPR handling
48-
if (bidderRequest && bidderRequest.gdprConsent) {
49-
commonPayload.gdprConsent = bidderRequest.gdprConsent.consentString;
50-
commonPayload.gdpr = bidderRequest.gdprConsent.gdprApplies; // we're handling the undefined case server side
135+
});
136+
}
137+
138+
/**
139+
* Unpack the response from the server into a list of bids.
140+
*
141+
* @param {*} serverResponse A successful response from the server.
142+
* @param {*} bidRequest An object with bid request informations
143+
* @return {Bid[]} An array of bids which were nested inside the server.
144+
*/
145+
function interpretResponse(serverResponse, bidRequest) {
146+
const bidResponses = [];
147+
const response = serverResponse.body;
148+
149+
if (response) {
150+
if (response.timeout || !response.ad || /<!--\s+No\s+ad\s+-->/gmi.test(response.ad)) {
151+
return bidResponses;
51152
}
52153

53-
return validBidRequests.map(bid => {
54-
let bidPayload = {
55-
adUnitCode: bid.adUnitCode,
56-
auctionId: bid.auctionId,
57-
bidder: bid.bidder,
58-
bidderRequestId: bid.bidderRequestId,
59-
bidRequestsCount: bid.bidRequestsCount,
60-
requestId: bid.bidId,
61-
sizes: bid.sizes.map(size => ({
62-
w: size[0],
63-
h: size[1],
64-
})),
65-
transactionId: bid.transactionId,
66-
zoneId: bid.params.zoneId,
67-
};
154+
// Setting our returned sizes object to default values
155+
let returnedSizes = {
156+
width: 1800,
157+
height: 1000
158+
};
68159

69-
let protocol = bid.params.protocol || DEFAULT_PROTOCOL;
70-
let bidHost = bid.params.bidHost || DEFAULT_BID_HOST;
71-
let payload = Object.assign({}, commonPayload, bidPayload);
72-
73-
return {
74-
method: 'POST',
75-
url: protocol + '://' + bidHost + '/bid',
76-
data: payload,
77-
options: {
78-
contentType: 'application/json',
79-
withCredentials: true
80-
},
81-
};
82-
});
83-
},
84-
85-
/**
86-
* Unpack the response from the server into a list of bids.
87-
*
88-
* @param {*} serverResponse A successful response from the server.
89-
* @param {*} bidRequest An object with bid request informations
90-
* @return {Bid[]} An array of bids which were nested inside the server.
91-
*/
92-
interpretResponse: (serverResponse, bidRequest) => {
93-
const bidResponses = [];
94-
const response = serverResponse.body;
95-
96-
if (response) {
97-
if (response.timeout || !response.ad || response.ad.match(/<!-- No ad -->/gmi)) {
98-
return bidResponses;
99-
}
100-
101-
// Setting our returned sizes object to default values
102-
let returnedSizes = {
103-
width: 1800,
104-
height: 1000
160+
// Verifying Banner sizes
161+
if (bidRequest && bidRequest.data && bidRequest.data.w === 1 && bidRequest.data.h === 1) {
162+
// If banner sizes are 1x1 we set our default size object to 1x1
163+
returnedSizes = {
164+
width: 1,
165+
height: 1
105166
};
167+
}
106168

107-
// Verifying Banner sizes
108-
if (bidRequest && bidRequest.data && bidRequest.data.w === 1 && bidRequest.data.h === 1) {
109-
// If banner sizes are 1x1 we set our default size object to 1x1
110-
returnedSizes = {
111-
width: 1,
112-
height: 1
113-
};
114-
}
115-
116-
const bidResponse = {
117-
requestId: response.requestId || '',
118-
cpm: response.cpm || 0,
119-
width: response.width || returnedSizes.width,
120-
height: response.height || returnedSizes.height,
121-
creativeId: response.creativeId || 1,
122-
dealId: response.dealId || 1,
123-
currency: response.currency || DEFAULT_CURRENCY,
124-
netRevenue: response.netRevenue || true,
125-
ttl: response.ttl || DEFAULT_TTL,
126-
ad: response.ad,
127-
};
169+
const bidResponse = {
170+
requestId: response.requestId || '',
171+
cpm: response.cpm || 0,
172+
width: response.width || returnedSizes.width,
173+
height: response.height || returnedSizes.height,
174+
creativeId: response.creativeId || 1,
175+
dealId: response.dealId || 1,
176+
currency: response.currency || DEFAULT_CURRENCY,
177+
netRevenue: response.netRevenue || true,
178+
ttl: response.ttl || DEFAULT_TTL,
179+
ad: response.ad,
180+
pbav: SUBLIME_VERSION
181+
};
128182

129-
bidResponses.push(bidResponse);
130-
}
183+
bidResponses.push(bidResponse);
184+
}
185+
186+
return bidResponses;
187+
}
188+
189+
/**
190+
* Send pixel when bidWon event is triggered
191+
* @param {Object} timeoutData
192+
*/
193+
function onBidWon(bid) {
194+
log('Bid won', bid);
195+
sendEvent('bidwon');
196+
}
197+
198+
/**
199+
* Send debug when we timeout
200+
* @param {Object} timeoutData
201+
*/
202+
function onTimeout(timeoutData) {
203+
log('Timeout from adapter', timeoutData);
204+
sendEvent('bidtimeout');
205+
}
131206

132-
return bidResponses;
133-
},
207+
export const spec = {
208+
code: BIDDER_CODE,
209+
gvlid: BIDDER_GVLID,
210+
aliases: [],
211+
isBidRequestValid: isBidRequestValid,
212+
buildRequests: buildRequests,
213+
interpretResponse: interpretResponse,
214+
onBidWon: onBidWon,
215+
onTimeout: onTimeout,
134216
};
135217

136218
registerBidder(spec);

test/spec/modules/sublimeBidAdapter_spec.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
import { expect } from 'chai';
2-
import { spec } from 'modules/sublimeBidAdapter.js';
2+
import { spec, sendEvent, log, setState, state } from 'modules/sublimeBidAdapter.js';
33
import { newBidder } from 'src/adapters/bidderFactory.js';
44

5+
let utils = require('src/utils');
6+
57
describe('Sublime Adapter', function() {
68
const adapter = newBidder(spec);
79

10+
describe('sendEvent', function() {
11+
let sandbox;
12+
13+
beforeEach(function () {
14+
sandbox = sinon.sandbox.create();
15+
});
16+
17+
it('should trigger pixel', function () {
18+
sandbox.spy(utils, 'triggerPixel');
19+
sendEvent('test', true);
20+
expect(utils.triggerPixel.called).to.equal(true);
21+
});
22+
23+
afterEach(function () {
24+
sandbox.restore();
25+
});
26+
})
27+
828
describe('inherited functions', function() {
929
it('exists and is a function', function() {
1030
expect(adapter.callBids).to.exist.and.to.be.a('function');
@@ -129,6 +149,7 @@ describe('Sublime Adapter', function() {
129149
currency: 'USD',
130150
netRevenue: true,
131151
ttl: 600,
152+
pbav: '0.5.2',
132153
ad: '',
133154
},
134155
];
@@ -170,6 +191,7 @@ describe('Sublime Adapter', function() {
170191
netRevenue: true,
171192
ttl: 600,
172193
ad: '<!-- Creative -->',
194+
pbav: '0.5.2',
173195
};
174196

175197
expect(result[0]).to.deep.equal(expectedResponse);
@@ -219,6 +241,7 @@ describe('Sublime Adapter', function() {
219241
netRevenue: true,
220242
ttl: 600,
221243
ad: '<!-- ad -->',
244+
pbav: '0.5.2',
222245
};
223246

224247
expect(result[0]).to.deep.equal(expectedResponse);
@@ -250,6 +273,10 @@ describe('Sublime Adapter', function() {
250273
let expectedResponse = [];
251274

252275
expect(result).to.deep.equal(expectedResponse);
276+
277+
describe('On bid Time out', function () {
278+
spec.onTimeout(result);
279+
});
253280
});
254281
});
255282
});

0 commit comments

Comments
 (0)