forked from tbuchok/vast-xml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (133 loc) · 5.91 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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', ad.VASTAdTagURI);
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;
creativeType = creative.element(c.type);
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) trackingEvents.element('Tracking', trackingEvent.url, { event : trackingEvent.event });
});
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, attributes).cdata(resource.uri);
});
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.creativeType) ? { creativeType : r.creativeType } : {}).cdata(r.uri);
if (r.adParameters) companion.element('AdParameters', r.adParameters.data, { xmlEncoded : r.adParameters.xmlEncoded });
});
});
var rootElement = inline || wrapper;
if(ad.Extensions && ad.Extensions.length > 0) {
var extensions = rootElement.element('Extensions');
ad.Extensions.forEach(function(extensionConfig){
var extension = extensions.element('Extension');
createNodes(extension, extensionConfig);
});
}
});
return response.end(options);
};
function createNodes(parentNode, nodeConfig) {
for(var nodeName in nodeConfig) {
if(typeof nodeConfig[nodeName] === 'string') {
parentNode.element(nodeName).cdata(nodeConfig[nodeName]);
continue;
}
if(nodeConfig[nodeName] instanceof Array) {
nodeConfig[nodeName].forEach(function(childNodeConfig) {
var childNode = parentNode.element(nodeName, childNodeConfig.attributes);
if(childNodeConfig.cdata) {
childNode.cdata(childNodeConfig.cdata);
return;
}
if(childNodeConfig.children) {
createNodes(childNode, childNodeConfig.children);
}
});
}
}
}
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;