Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mobian RTD provider: update API endpoint #12121

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions modules/mobianRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { deepSetValue, safeJSONParse } from '../src/utils.js';
* @typedef {import('../modules/rtdModule/index.js').RtdSubmodule} RtdSubmodule
*/

export const MOBIAN_URL = 'https://impact-api-prod.themobian.com/brand_safety';
export const MOBIAN_URL = 'https://prebid.outcomes.net/api/prebid/v1/assessment/async';

/** @type {RtdSubmodule} */
export const mobianBrandSafetySubmodule = {
Expand All @@ -26,33 +26,25 @@ function init() {
function getBidRequestData(bidReqConfig, callback, config) {
const { site: ortb2Site } = bidReqConfig.ortb2Fragments.global;
const pageUrl = encodeURIComponent(getPageUrl());
const requestUrl = `${MOBIAN_URL}/by_url?url=${pageUrl}`;
const requestUrl = `${MOBIAN_URL}?url=${pageUrl}`;

const ajax = ajaxBuilder();

return new Promise((resolve) => {
ajax(requestUrl, {
success: function(responseData) {
let response = safeJSONParse(responseData);
if (!response) {
if (!response || !response.meta.has_results) {
resolve({});
callback();
return;
}

let mobianRisk = response.garm_risk || 'unknown';

const contentCategories = Object.keys(response)
.filter(key => key.startsWith('garm_content_category_') && response[key])
.map(key => key.replace('garm_content_category_', ''));

const sentiment = Object.keys(response)
.find(key => key.startsWith('sentiment_') && response[key])
?.replace('sentiment_', '') || 'unknown';

const emotions = Object.keys(response)
.filter(key => key.startsWith('emotion_') && response[key])
.map(key => key.replace('emotion_', ''));
const results = response.results;
const mobianRisk = results.mobianRisk || 'unknown';
const contentCategories = results.mobianContentCategories || [];
const sentiment = results.mobianSentiment || 'unknown';
const emotions = results.mobianEmotions || [];

const risk = {
risk: mobianRisk,
Expand Down
87 changes: 62 additions & 25 deletions test/spec/modules/mobianRtdProvider_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ describe('Mobian RTD Submodule', function () {
ajaxStub.restore();
});

it('should set key-value pairs when server responds with garm_risk', function () {
it('should set key-value pairs when server responds with valid data', function () {
ajaxStub = sinon.stub(ajax, 'ajaxBuilder').returns(function(url, callbacks) {
callbacks.success(JSON.stringify({
garm_risk: 'low',
sentiment_positive: true,
emotion_joy: true
meta: {
url: 'https://example.com',
has_results: true
},
results: {
mobianRisk: 'low',
mobianSentiment: 'positive',
mobianContentCategories: [],
mobianEmotions: ['joy']
}
}));
});

Expand All @@ -50,15 +57,19 @@ describe('Mobian RTD Submodule', function () {
});
});

it('should handle response with GARM content categories, sentiment, and emotions', function () {
it('should handle response with content categories and multiple emotions', function () {
ajaxStub = sinon.stub(ajax, 'ajaxBuilder').returns(function(url, callbacks) {
callbacks.success(JSON.stringify({
garm_risk: 'medium',
garm_content_category_arms: true,
garm_content_category_crime: true,
sentiment_negative: true,
emotion_anger: true,
emotion_fear: true
meta: {
url: 'https://example.com',
has_results: true
},
results: {
mobianRisk: 'medium',
mobianSentiment: 'negative',
mobianContentCategories: ['arms', 'crime'],
mobianEmotions: ['anger', 'fear']
}
}));
});

Expand All @@ -78,26 +89,22 @@ describe('Mobian RTD Submodule', function () {
});
});

it('should return unknown risk when garm_risk is not present', function () {
it('should return empty object when server responds with has_results: false', function () {
ajaxStub = sinon.stub(ajax, 'ajaxBuilder').returns(function(url, callbacks) {
callbacks.success(JSON.stringify({
sentiment_neutral: true
meta: {
url: 'https://example.com',
has_results: false
},
results: {}
}));
});

return mobianBrandSafetySubmodule.getBidRequestData(bidReqConfig, {}, {}).then((result) => {
expect(result).to.deep.equal({
risk: 'unknown',
contentCategories: [],
sentiment: 'neutral',
emotions: []
});
expect(bidReqConfig.ortb2Fragments.global.site.ext.data).to.deep.include({
mobianRisk: 'unknown',
mobianContentCategories: [],
mobianSentiment: 'neutral',
mobianEmotions: []
});
expect(result).to.deep.equal({});
expect(bidReqConfig.ortb2Fragments.global.site.ext.data).to.not.have.any.keys(
'mobianRisk', 'mobianContentCategories', 'mobianSentiment', 'mobianEmotions'
);
});
});

Expand All @@ -122,4 +129,34 @@ describe('Mobian RTD Submodule', function () {
expect(result).to.deep.equal({});
});
});

it('should use default values when fields are missing in the response', function () {
ajaxStub = sinon.stub(ajax, 'ajaxBuilder').returns(function(url, callbacks) {
callbacks.success(JSON.stringify({
meta: {
url: 'https://example.com',
has_results: true
},
results: {
mobianRisk: 'high'
// Missing other fields
}
}));
});

return mobianBrandSafetySubmodule.getBidRequestData(bidReqConfig, {}, {}).then((result) => {
expect(result).to.deep.equal({
risk: 'high',
contentCategories: [],
sentiment: 'unknown',
emotions: []
});
expect(bidReqConfig.ortb2Fragments.global.site.ext.data).to.deep.include({
mobianRisk: 'high',
mobianContentCategories: [],
mobianSentiment: 'unknown',
mobianEmotions: []
});
});
});
});