Skip to content

Commit

Permalink
Real Time Data Module: Mobian Brand Safety (prebid#11798)
Browse files Browse the repository at this point in the history
* add mobian brand safety variables

* directly set site.ext.data in a getBidRequestData function
  • Loading branch information
ehb-mtk authored and DecayConstant committed Jul 18, 2024
1 parent 50f5f28 commit c746637
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
52 changes: 52 additions & 0 deletions modules/mobianRtdProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { submodule } from '../src/hook.js';
import { ajaxBuilder } from '../src/ajax.js';
export const MOBIAN_URL = 'http://impact-analytics-staging.themobian.com';

export const mobianBrandSafetySubmodule = {
name: 'mobianBrandSafety',
gvlid: null,
init: init,
getBidRequestData: getBidRequestData
};

function init() {
return true;
}
function getBidRequestData(bidReqConfig, callback, config) {
const { site: ortb2Site } = bidReqConfig.ortb2Fragments.global;
const pageUrl = encodeURIComponent(getPageUrl());
const requestUrl = `${MOBIAN_URL}?url=${pageUrl}`;

const ajax = ajaxBuilder();

return new Promise((resolve) => {
ajax(requestUrl, {
success: function(response) {
const risks = ['garm_high_risk', 'garm_medium_risk', 'garm_low_risk', 'garm_no_risk'];
const riskLevels = ['high_risk', 'medium_risk', 'low_risk', 'no_risk'];

let mobianGarmRisk = 'unknown';
for (let i = 0; i < risks.length; i++) {
if (response[risks[i]]) {
mobianGarmRisk = riskLevels[i];
break;
}
}
const risk = {
'mobianGarmRisk': mobianGarmRisk
};
resolve(risk);
ortb2Site.ext.data['mobian'] = risk
},
error: function () {
resolve({});
}
});
});
}

function getPageUrl() {
return window.location.href;
}

submodule('realTimeData', mobianBrandSafetySubmodule);
11 changes: 11 additions & 0 deletions modules/mobianRtdProvider.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Overview

Module Name: Mobian Rtd Provider
Module Type: Rtd Provider
Maintainer: rich.rodriguez@themobian.com

# Description

RTD provider for themobian Brand Safety determinations. Publishers
should use this to get Mobian's GARM Risk evaluations for
a URL.
107 changes: 107 additions & 0 deletions test/spec/modules/mobianRtdProvider_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { expect } from 'chai';
import sinon from 'sinon';
import { mobianBrandSafetySubmodule, MOBIAN_URL } from 'modules/mobianRtdProvider.js';
import * as ajax from 'src/ajax.js';

describe('Mobian RTD Submodule', function () {
let ajaxStub;
let bidReqConfig;

beforeEach(function () {
bidReqConfig = {
ortb2Fragments: {
global: {
site: {
ext: {
data: {}
}
}
}
}
};
});

afterEach(function () {
ajaxStub.restore();
});

it('should return no_risk when server responds with garm_no_risk', function () {
ajaxStub = sinon.stub(ajax, 'ajaxBuilder').returns(function(url, callbacks) {
callbacks.success({
garm_no_risk: true,
garm_low_risk: false,
garm_medium_risk: false,
garm_high_risk: false
});
});

return mobianBrandSafetySubmodule.getBidRequestData(bidReqConfig, {}, {}).then((risk) => {
expect(risk).to.have.property('mobianGarmRisk');
expect(risk['mobianGarmRisk']).to.equal('no_risk');
expect(bidReqConfig.ortb2Fragments.global.site.ext.data.mobian).to.deep.equal(risk);
});
});

it('should return low_risk when server responds with garm_no_risk', function () {
ajaxStub = sinon.stub(ajax, 'ajaxBuilder').returns(function(url, callbacks) {
callbacks.success({
garm_no_risk: false,
garm_low_risk: true,
garm_medium_risk: false,
garm_high_risk: false
});
});

return mobianBrandSafetySubmodule.getBidRequestData(bidReqConfig, {}, {}).then((risk) => {
expect(risk).to.have.property('mobianGarmRisk');
expect(risk['mobianGarmRisk']).to.equal('low_risk');
expect(bidReqConfig.ortb2Fragments.global.site.ext.data.mobian).to.deep.equal(risk);
});
});

it('should return medium_risk when server responds with garm_medium_risk', function () {
ajaxStub = sinon.stub(ajax, 'ajaxBuilder').returns(function(url, callbacks) {
callbacks.success({
garm_no_risk: false,
garm_low_risk: false,
garm_medium_risk: true,
garm_high_risk: false
});
});

return mobianBrandSafetySubmodule.getBidRequestData(bidReqConfig, {}, {}).then((risk) => {
expect(risk).to.have.property('mobianGarmRisk');
expect(risk['mobianGarmRisk']).to.equal('medium_risk');
expect(bidReqConfig.ortb2Fragments.global.site.ext.data.mobian).to.deep.equal(risk);
});
});

it('should return high_risk when server responds with garm_high_risk', function () {
ajaxStub = sinon.stub(ajax, 'ajaxBuilder').returns(function(url, callbacks) {
callbacks.success({
garm_no_risk: false,
garm_low_risk: false,
garm_medium_risk: false,
garm_high_risk: true
});
});

return mobianBrandSafetySubmodule.getBidRequestData(bidReqConfig, {}, {}).then((risk) => {
expect(risk).to.have.property('mobianGarmRisk');
expect(risk['mobianGarmRisk']).to.equal('high_risk');
expect(bidReqConfig.ortb2Fragments.global.site.ext.data.mobian).to.deep.equal(risk);
});
});

it('should return unknown when server response is not of the expected shape', function () {
ajaxStub = sinon.stub(ajax, 'ajaxBuilder').returns(function(url, callbacks) {
callbacks.success('unexpected output not even of the right type');
});

return mobianBrandSafetySubmodule.getBidRequestData(bidReqConfig, {}, {}).then((risk) => {
expect(risk).to.have.property('mobianGarmRisk');
expect(risk['mobianGarmRisk']).to.equal('unknown');
expect(bidReqConfig.ortb2Fragments.global.site.ext.data.mobian).to.deep.equal(risk);
});
});
});

0 comments on commit c746637

Please sign in to comment.