-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
125 lines (119 loc) · 5.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var builder = require('xmlbuilder')
, Ad = require('./lib/ad');
var xml = function(options) {
options = options || {};
var track = (options.track === undefined) ? true : options.track;
var response = builder.create('VAST', { version : '1.0', encoding : 'UTF-8' });
response.att('version', this.version);
if (this.ads.length === 0 && this.VASTErrorURI)
return response.element('Error').cdata(this.VASTErrorURI).end(options);
this.ads.forEach(function(ad){
var adOptions = { id : ad.id }
if (ad.sequence) adOptions.sequence = ad.sequence;
var Ad = response.element('Ad', adOptions);
var creatives;
if (ad.structure.toLowerCase() === 'wrapper') {
var wrapper = Ad.element('Wrapper');
wrapper.element('AdSystem', ad.AdSystem.name, { version : ad.AdSystem.version });
wrapper.element('VASTAdTagURI').cdata(ad.VASTAdTagURI);
if (ad.Error)
wrapper.element('Error').cdata(ad.Error);
ad.impressions.forEach(function(impression) {
if (track) wrapper.element('Impression').cdata(impression.url);
});
creatives = wrapper.element('Creatives');
} else {
var inline = Ad.element('InLine');
inline.element('AdSystem', ad.AdSystem.name, { version : ad.AdSystem.version });
inline.element('AdTitle').cdata(ad.AdTitle);
inline.element('Description').cdata(ad.Description || '');
ad.surveys.forEach(function(survey) {
var attributes = {}
if (survey.type) attributes.type = survey.type
inline.element('Survey', attributes).cdata(survey.url);
});
if (ad.Error)
inline.element('Error').cdata(ad.Error);
ad.impressions.forEach(function(impression){
if (track) inline.element('Impression', { id : impression.id }).cdata(impression.url);
});
creatives = inline.element('Creatives');
}
var linearCreatives = ad.creatives.filter(function(c) { return c.type === 'Linear' });
var nonLinearCreatives = ad.creatives.filter(function(c) { return c.type === 'NonLinear' });
var companionAdCreatives = ad.creatives.filter(function(c) { return c.type === 'CompanionAd' });
linearCreatives.forEach(function(c) {
var creative = creatives.element('Creative', c.attributes)
var creativeType;
var creativeOpts = {};
if (c.skipoffset) creativeOpts.skipoffset = c.skipoffset;
creativeType = creative.element(c.type, creativeOpts);
if (c.icons.length > 0) var icons = creativeType.element('Icons');
c.icons.forEach(function(i){
var icon = icons.element('Icon', i.attributes);
i.resources.forEach(function(r){
icon.element(r.type, r.uri, (r.creativeType) ? { creativeType : r.creativeType } : {});
});
});
creativeType.element('Duration', c.Duration);
var trackingEvents = creativeType.element('TrackingEvents');
c.trackingEvents.forEach(function(trackingEvent){
if (track) {
var attributes = { event : trackingEvent.event };
if (trackingEvent.offset) attributes.offset = trackingEvent.offset;
trackingEvents.element('Tracking', trackingEvent.url, attributes);
}
});
if (c.AdParameters) creativeType.element('AdParameters').cdata(c.AdParameters);
var videoClicks = creativeType.element('VideoClicks');
c.videoClicks.forEach(function(videoClick){
videoClicks.element(videoClick.type, videoClick.url, { id : videoClick.id });
});
var mediaFiles = creativeType.element('MediaFiles');
c.mediaFiles.forEach(function(mediaFile) {
mediaFiles.element('MediaFile', mediaFile.attributes).cdata(mediaFile.url);
});
});
nonLinearCreatives.forEach(function(c){
var nonLinearAds = creatives.element('Creative').element('NonLinearAds');
var creativeType = nonLinearAds.element(c.type, c.attributes);
c.resources.forEach(function(resource) {
var attributes = {}
if (resource.creativeType) attributes.creativeType = resource.creativeType;
creativeType.element(resource.type, resource.uri, attributes);
});
c.clicks.forEach(function(click){
creativeType.element(click.type, click.uri);
});
if (c.adParameters) creativeType.element('AdParameters', c.adParameters.data, { xmlEncoded : c.adParameters.xmlEncoded });
});
if (companionAdCreatives.length > 0) var companionAds = creatives.element('Creative').element('CompanionAds');
companionAdCreatives.forEach(function(c) {
companion = companionAds.element('Companion', c.attributes);
c.resources.forEach(function(r) {
companion.element(r.type, r.uri, (r.creativeType) ? { creativeType : r.creativeType } : {});
if (r.adParameters) companion.element('AdParameters', r.adParameters.data, { xmlEncoded : r.adParameters.xmlEncoded });
});
});
if (ad.Extensions) {
var extensions = inline.element('Extensions');
[].concat(ad.Extensions).forEach(function(extension) {
extensions.element('Extension').raw(extension);
});
}
});
return response.end(options);
};
function VAST(settings) {
settings = settings || {};
this.version = settings.version || '3.0';
this.VASTErrorURI = settings.VASTErrorURI;
this.ads = [];
this.attachAd = function(settings) {
var ad = new Ad(settings);
this.ads.push(ad);
return ad;
};
this.xml = xml;
}
module.exports = VAST;