@schibstedspain/openads-appnexus
Version:
OpenAds AppNexus module for handle ads
160 lines (139 loc) • 4.76 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = undefined;
var _promise = require('babel-runtime/core-js/promise');
var _promise2 = _interopRequireDefault(_promise);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _events = require('./event/events');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* @class
* @implements {AdLoadable}
* @implements {AdViewable}
* @implements {Logger}
*/
var AppNexusConnector = function () {
function AppNexusConnector(_ref) {
var pageOpts = _ref.pageOpts,
logger = _ref.logger,
astClient = _ref.astClient,
adRepository = _ref.adRepository,
loggerProvider = _ref.loggerProvider;
(0, _classCallCheck3.default)(this, AppNexusConnector);
this._logger = logger;
this._adRepository = adRepository;
this._loggerProvider = loggerProvider;
this._astClient = astClient;
this._pageOpts = pageOpts;
if (this._pageOpts) {
this._astClient.setPageOpts(this._pageOpts);
}
}
(0, _createClass3.default)(AppNexusConnector, [{
key: 'display',
value: function display(_ref2) {
var _this = this;
var id = _ref2.id;
return _promise2.default.resolve().then(function () {
return _this._astClient.showTag({ targetId: id });
}).then(null);
}
}, {
key: 'loadAd',
value: function loadAd() {
var _this2 = this;
var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
id = _ref3.id,
_ref3$specification = _ref3.specification,
specification = _ref3$specification === undefined ? {} : _ref3$specification;
return _promise2.default.resolve().then(function () {
return _this2._adRepository.remove({ id: id });
}).then(function () {
return _this2._astClient.defineTag(specification.appnexus);
}).then(function (astClient) {
return astClient.onEvent({
event: _events.AD_AVAILABLE,
targetId: id,
callback: consumer(_this2._adRepository)(id)(_events.AD_AVAILABLE)
});
}).then(function (astClient) {
return astClient.onEvent({
event: _events.AD_BAD_REQUEST,
targetId: id,
callback: consumer(_this2._adRepository)(id)(_events.AD_BAD_REQUEST)
});
}).then(function (astClient) {
return astClient.onEvent({
event: _events.AD_ERROR,
targetId: id,
callback: consumer(_this2._adRepository)(id)(_events.AD_ERROR)
});
}).then(function (astClient) {
return astClient.onEvent({
event: _events.AD_NO_BID,
targetId: id,
callback: consumer(_this2._adRepository)(id)(_events.AD_NO_BID)
});
}).then(function (astClient) {
return astClient.onEvent({
event: _events.AD_REQUEST_FAILURE,
targetId: id,
callback: consumer(_this2._adRepository)(id)(_events.AD_REQUEST_FAILURE)
});
}).then(function (astClient) {
return astClient.loadTags();
}).then(function () {
return _this2._adRepository.find({ id: id });
});
}
}, {
key: 'refresh',
value: function refresh(_ref4) {
var _this3 = this;
var id = _ref4.id,
specification = _ref4.specification;
return _promise2.default.resolve().then(function () {
return _this3._adRepository.remove({ id: id });
}).then(function () {
if (specification && specification.appnexus) {
_this3._astClient.modifyTag({
targetId: id,
data: specification.appnexus
});
}
}).then(function () {
return _this3._astClient.refresh([id]);
}).then(function () {
return _this3._adRepository.find({ id: id });
});
}
}, {
key: 'enableDebug',
value: function enableDebug(_ref5) {
var debug = _ref5.debug;
this._astClient.debugMode({ debug: debug });
this._loggerProvider.debugMode({ debug: debug });
}
}, {
key: 'pageOpts',
get: function get() {
return this._pageOpts;
}
}]);
return AppNexusConnector;
}();
exports.default = AppNexusConnector;
var consumer = function consumer(adRepository) {
return function (id) {
return function (status) {
return function (data) {
return adRepository.save({ id: id, adResponse: { data: data, status: status } });
};
};
};
};
;