-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added bidglass adapter + test * PR Review Updates: - Added formal params to getUserSyncs function definition - getUserSyncs now always returns an array - Improved unit test coverage * PR Review Updates: - Removed unused methods: getUserSyncs, onTimeout, onBidWon, onSetTargeting - Removed getUserSyncs unit test - Removed "dead code" - Removed some unnecessary comments - Fixed usage of parseInt
- Loading branch information
1 parent
b6aaf64
commit 9d2f06c
Showing
3 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import * as utils from 'src/utils'; | ||
// import {config} from 'src/config'; | ||
import {registerBidder} from 'src/adapters/bidderFactory'; | ||
|
||
const BIDDER_CODE = 'bidglass'; | ||
|
||
export const spec = { | ||
code: BIDDER_CODE, | ||
aliases: ['bg'], // short code | ||
/** | ||
* Determines whether or not the given bid request is valid. | ||
* | ||
* @param {BidRequest} bid The bid params to validate. | ||
* @return boolean True if this is a valid bid, and false otherwise. | ||
*/ | ||
isBidRequestValid: function(bid) { | ||
return !!(bid.params.adUnitId && !isNaN(parseFloat(bid.params.adUnitId)) && isFinite(bid.params.adUnitId)); | ||
}, | ||
/** | ||
* Make a server request from the list of BidRequests. | ||
* | ||
* @param {validBidRequests[]} - an array of bids | ||
* @return ServerRequest Info describing the request to the server. | ||
*/ | ||
buildRequests: function(validBidRequests, bidderRequest) { | ||
/* | ||
Sample array entry for validBidRequests[]: | ||
[{ | ||
"bidder": "bidglass", | ||
"bidId": "51ef8751f9aead", | ||
"params": { | ||
"adUnitId": 11, | ||
... | ||
}, | ||
"adUnitCode": "div-gpt-ad-1460505748561-0", | ||
"transactionId": "d7b773de-ceaa-484d-89ca-d9f51b8d61ec", | ||
"sizes": [[320,50],[300,250],[300,600]], | ||
"bidderRequestId": "418b37f85e772c", | ||
"auctionId": "18fd8b8b0bd757", | ||
"bidRequestsCount": 1 | ||
}] | ||
*/ | ||
|
||
let imps = []; | ||
let getReferer = function() { | ||
return window === window.top ? window.location.href : window.parent === window.top ? document.referrer : null; | ||
}; | ||
let getOrigins = function() { | ||
var ori = [window.location.protocol + '//' + window.location.hostname]; | ||
|
||
if (window.location.ancestorOrigins) { | ||
for (var i = 0; i < window.location.ancestorOrigins.length; i++) { | ||
ori.push(window.location.ancestorOrigins[i]); | ||
} | ||
} else if (window !== window.top) { | ||
// Derive the parent origin | ||
var parts = document.referrer.split('/'); | ||
|
||
ori.push(parts[0] + '//' + parts[2]); | ||
|
||
if (window.parent !== window.top) { | ||
// Additional unknown origins exist | ||
ori.push('null'); | ||
} | ||
} | ||
|
||
return ori; | ||
}; | ||
|
||
utils._each(validBidRequests, function(bid) { | ||
bid.sizes = ((utils.isArray(bid.sizes) && utils.isArray(bid.sizes[0])) ? bid.sizes : [bid.sizes]); | ||
bid.sizes = bid.sizes.filter(size => utils.isArray(size)); | ||
|
||
// Stuff to send: [bid id, sizes, adUnitId] | ||
imps.push({ | ||
bidId: bid.bidId, | ||
sizes: bid.sizes, | ||
adUnitId: utils.getBidIdParameter('adUnitId', bid.params) | ||
}); | ||
}); | ||
|
||
// Stuff to send: page URL | ||
const bidReq = { | ||
reqId: utils.getUniqueIdentifierStr(), | ||
imps: imps, | ||
ref: getReferer(), | ||
ori: getOrigins() | ||
}; | ||
|
||
let url = 'https://bid.glass/ad/hb.php?' + | ||
`src=$$REPO_AND_VERSION$$`; | ||
|
||
return { | ||
method: 'POST', | ||
url: url, | ||
data: JSON.stringify(bidReq), | ||
options: { | ||
contentType: 'text/plain', | ||
withCredentials: false | ||
} | ||
} | ||
}, | ||
|
||
/** | ||
* Unpack the response from the server into a list of bids. | ||
* | ||
* @param {ServerResponse} serverResponse A successful response from the server. | ||
* @return {Bid[]} An array of bids which were nested inside the server. | ||
*/ | ||
interpretResponse: function(serverResponse) { | ||
const bidResponses = []; | ||
|
||
utils._each(serverResponse.body.bidResponses, function(bid) { | ||
bidResponses.push({ | ||
requestId: bid.requestId, | ||
cpm: parseFloat(bid.cpm), | ||
width: parseInt(bid.width, 10), | ||
height: parseInt(bid.height, 10), | ||
creativeId: bid.creativeId, | ||
dealId: bid.dealId || null, | ||
currency: bid.currency || 'USD', | ||
mediaType: bid.mediaType || 'banner', | ||
netRevenue: true, | ||
ttl: bid.ttl || 10, | ||
ad: bid.ad | ||
}); | ||
}); | ||
|
||
return bidResponses; | ||
} | ||
|
||
} | ||
|
||
registerBidder(spec); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Overview | ||
|
||
``` | ||
Module Name: Bid Glass Bid Adapter | ||
Module Type: Bidder Adapter | ||
Maintainer: [email protected] | ||
``` | ||
|
||
# Description | ||
|
||
Connects to Bid Glass and allows bids on ad units to compete within prebid. | ||
|
||
# Sample Ad Unit: For Publishers | ||
``` | ||
var adUnits = [{ | ||
code: 'bg-test-rectangle', | ||
sizes: [[300, 250]], | ||
bids: [{ | ||
bidder: 'bidglass', | ||
params: { | ||
adUnitId: '-1' | ||
} | ||
}] | ||
},{ | ||
code: 'bg-test-leaderboard', | ||
sizes: [[728, 90]], | ||
bids: [{ | ||
bidder: 'bidglass', | ||
params: { | ||
adUnitId: '-1' | ||
} | ||
}] | ||
}] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { expect } from 'chai'; | ||
import { spec } from 'modules/bidglassBidAdapter'; | ||
import { newBidder } from 'src/adapters/bidderFactory'; | ||
|
||
describe('Bid Glass Adapter', function () { | ||
const adapter = newBidder(spec); | ||
|
||
describe('isBidRequestValid', function () { | ||
let bid = { | ||
'bidder': 'bidglass', | ||
'params': { | ||
'adUnitId': '3' | ||
}, | ||
'adUnitCode': 'bidglass-testunit', | ||
'sizes': [[300, 250], [300, 600]], | ||
'bidId': '30b31c1838de1e', | ||
'bidderRequestId': '22edbae2733bf6', | ||
'auctionId': '1d1a030790a475', | ||
}; | ||
|
||
it('should return true when required params found', function () { | ||
expect(spec.isBidRequestValid(bid)).to.equal(true); | ||
}); | ||
|
||
it('should return false when required params are not passed', function () { | ||
let bid = Object.assign({}, bid); | ||
delete bid.params; | ||
bid.params = {}; | ||
expect(spec.isBidRequestValid(bid)).to.equal(false); | ||
}); | ||
}); | ||
|
||
describe('buildRequests', function () { | ||
const bidRequests = [{ | ||
'bidder': 'bidglass', | ||
'params': { | ||
'adUnitId': '3' | ||
}, | ||
'adUnitCode': 'bidglass-testunit', | ||
'sizes': [[300, 250], [300, 600]], | ||
'bidId': '30b31c1838de1e', | ||
'bidderRequestId': '22edbae2733bf6', | ||
'auctionId': '1d1a030790a475', | ||
}]; | ||
|
||
const request = spec.buildRequests(bidRequests); | ||
|
||
it('sends bid request to our endpoint via POST', function () { | ||
expect(request.method).to.equal('POST'); | ||
}); | ||
|
||
it('sets withCredentials to false', function () { | ||
expect(request.options.withCredentials).to.equal(false); | ||
}); | ||
}); | ||
|
||
describe('interpretResponse', function () { | ||
let response; | ||
beforeEach(function () { | ||
response = { | ||
body: { | ||
'bidResponses': [{ | ||
'ad': '<!-- Creative -->', | ||
'cpm': '0.01', | ||
'creativeId': '-1', | ||
'width': '300', | ||
'height': '250', | ||
'requestId': '30b31c1838de1e' | ||
}] | ||
} | ||
}; | ||
}); | ||
|
||
it('should get the correct bid response', function () { | ||
let expectedResponse = [{ | ||
'requestId': '30b31c1838de1e', | ||
'cpm': 0.01, | ||
'width': 300, | ||
'height': 250, | ||
'creativeId': '-1', | ||
'dealId': null, | ||
'currency': 'USD', | ||
'mediaType': 'banner', | ||
'netRevenue': true, | ||
'ttl': 10, | ||
'ad': '<!-- Creative -->' | ||
}]; | ||
|
||
let result = spec.interpretResponse(response); | ||
expect(Object.keys(result[0])).to.deep.equal(Object.keys(expectedResponse[0])); | ||
}); | ||
|
||
it('handles empty bid response', function () { | ||
let response = { | ||
body: { | ||
'bidResponses': [] | ||
} | ||
}; | ||
let result = spec.interpretResponse(response); | ||
expect(result.length).to.equal(0); | ||
}); | ||
}); | ||
}); |