Comparing version 0.0.1 to 0.1.0
111
adform.js
@@ -9,5 +9,7 @@ var request = require('request').defaults({ jar: false }); | ||
uri: 'https://api.adform.com/Services/AdvertiserService.svc', | ||
action: 'http://www.adform.com/api/2010/06/IAdvertiserService/GetAdvertisers' | ||
action: | ||
'http://www.adform.com/api/2010/06/IAdvertiserService/GetAdvertisers' | ||
}, function(err, etree) { | ||
var advertisers = etree.findall('./s:Body/Advertisers/Advertiser').map(function(advertiser) { | ||
var rawAdvertisers = etree.findall('./s:Body/Advertisers/Advertiser'); | ||
var advertisers = rawAdvertisers.map(function(advertiser) { | ||
return { | ||
@@ -19,4 +21,4 @@ name: advertiser.findtext('Name'), | ||
callback(null, advertisers); | ||
}) | ||
} | ||
}); | ||
}; | ||
@@ -35,11 +37,12 @@ adform.createAd = function(ticket, campaign, name, data, callback) { | ||
'_attr': { | ||
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance', | ||
'xmlns:xsi': | ||
'http://www.w3.org/2001/XMLSchema-instance', | ||
'xsi:type': 'ns:FlashAd' | ||
} | ||
}, | ||
'ns:IgnoreWarnings': true | ||
'ns:IgnoreWarnings': true | ||
} | ||
} | ||
}, callback); | ||
} | ||
}; | ||
@@ -49,3 +52,4 @@ adform.getCampaigns = function(ticket, advertiser, callback) { | ||
uri: 'https://api.adform.com/Services/CampaignService.svc', | ||
action: 'http://www.adform.com/api/2010/06/ICampaignService/GetCampaigns', | ||
action: | ||
'http://www.adform.com/api/2010/06/ICampaignService/GetCampaigns', | ||
data: { | ||
@@ -55,5 +59,6 @@ 'ns:GetCampaignsData': { | ||
} | ||
} | ||
} | ||
}, function(err, etree) { | ||
var campaigns = etree.findall('./s:Body/Campaigns/Campaign').map(function(campaign) { | ||
var rawCampaigns = etree.findall('./s:Body/Campaigns/Campaign'); | ||
var campaigns = rawCampaigns.map(function(campaign) { | ||
return { | ||
@@ -64,25 +69,91 @@ name: campaign.findtext('Name'), | ||
endDate: new Date(campaign.findtext('EndDate')) | ||
} | ||
}; | ||
}); | ||
callback(null, campaigns); | ||
}); | ||
} | ||
}; | ||
adform.getAdStats = function(ticket, campaign, startDate, endDate, callback) { | ||
function formatStats(stats) { | ||
return { | ||
impressions: Number(stats.findtext('Impressions')), | ||
clicks: Number(stats.findtext('Clicks')), | ||
leads: Number(stats.findtext('Leads')), | ||
sales: Number(stats.findtext('Sales')) | ||
}; | ||
} | ||
function formatAd(ad) { | ||
return { | ||
id: Number(ad.findtext('Id')), | ||
days: ad.findall('./Days/Day').map(function(day) { | ||
var stats = day.find('Stats'); | ||
return { | ||
date: new Date(day.findtext('Date')), | ||
stats: formatStats(day.find('Stats')) | ||
}; | ||
}) | ||
}; | ||
} | ||
var ns = 'http://www.adform.com/api/CampaignStatsService/2010/09'; | ||
makeRequest(ticket, { | ||
uri: 'https://api.adform.com/Services/CampaignStatsService.svc', | ||
action: | ||
'http://www.adform.com/api/CampaignStatsService/2010/09/' + | ||
'ICampaignStatsService/GetAdStats', | ||
data: { | ||
'ns1:GetAdStatsData': { | ||
'ns1:IdFilter': { | ||
'ns:CampaignId': { | ||
'_attr': { | ||
'xmlns:ns': ns | ||
}, | ||
'_text': campaign.id | ||
}, | ||
'_attr': { | ||
'i:type': 'ns1:CampaignIdFilter', | ||
'xmlns:i': 'http://www.w3.org/2001/XMLSchema-instance' | ||
} | ||
}, | ||
// transform startDate and endDate to 'yyyy-mm-dd' format | ||
'ns1:StartDate': startDate.toJSON().slice(0, 10), | ||
'ns1:EndDate': endDate.toJSON().slice(0, 10) | ||
} | ||
}, | ||
namespaces: [{ | ||
name: 'ns1', | ||
src: ns | ||
}] | ||
}, function(err, etree) { | ||
if (err) { | ||
console.log(err.body); | ||
throw err; | ||
} | ||
var ads = etree.findall('./s:Body/Campaign/Ads/Ad').map(formatAd); | ||
callback(null, ads); | ||
}); | ||
}; | ||
adform.login = function(username, password, callback) { | ||
var body = { | ||
"UserName": username, | ||
"Password": password | ||
'UserName': username, | ||
'Password': password | ||
}; | ||
var uri = 'https://api.adform.com/Services/SecurityService.svc/JSON/Login'; | ||
request.post({ | ||
"json": true, | ||
"body": body, | ||
"uri": 'https://api.adform.com/Services/SecurityService.svc/JSON/Login', | ||
"headers": { | ||
'json': true, | ||
'body': body, | ||
'uri': uri, | ||
'headers': { | ||
'content-length': body.length | ||
} | ||
}, | ||
function (err, res, ticket) { | ||
function(err, res, ticket) { | ||
callback(err, ticket); | ||
} | ||
); | ||
} | ||
}; |
@@ -14,2 +14,8 @@ var util = require('util'); | ||
function SerializationError(body, message) { | ||
AdformError.call(this, body, message); | ||
this.name = 'SerializationError'; | ||
} | ||
util.inherits(SerializationError, AdformError); | ||
function ValidationError(body, message, fieldPath) { | ||
@@ -24,14 +30,20 @@ AdformError.call(this, body, message); | ||
var parsed = et.parse(body); | ||
var type = parsed.findtext('.//faultstring'); | ||
var type = parsed.findtext('.//faultcode'); | ||
var message = parsed.findtext('.//Message'); | ||
if (type === 'ValidationError') { | ||
if (type === 's:SerializationError') { | ||
return new SerializationError(body, message); | ||
} | ||
if (type === 's:ValidationError') { | ||
var fieldPath = parsed.findtext('.//FieldPath'); | ||
return new ValidationError(body, message, fieldPath); | ||
} else { | ||
return new AdformError(body, message); | ||
} | ||
return new AdformError(body, message); | ||
} | ||
exports.AdformError = AdformError; | ||
exports.SerializationError = SerializationError; | ||
exports.ValidationError = ValidationError; | ||
exports.wrap = wrap; | ||
exports.wrap = wrap; |
@@ -13,3 +13,3 @@ var request = require('request').defaults({ jar: false }); | ||
Object.keys(obj).forEach(function(key) { | ||
if (key !== '_attr'){ | ||
if (key !== '_attr' && key !== '_text') { | ||
walk(obj[key], subElement(elm, key)); | ||
@@ -24,2 +24,5 @@ } | ||
} | ||
if (obj._text) { | ||
elm.text = obj._text; | ||
} | ||
} else { | ||
@@ -30,12 +33,20 @@ elm.text = obj; | ||
var toXml = function(ticket, opts) { | ||
function toXml(ticket, opts) { | ||
var root = element('soapenv:Envelope'); | ||
root.set('xmlns:soapenv', 'http://schemas.xmlsoap.org/soap/envelope/'); | ||
root.set('xmlns:ns', 'http://www.adform.com/api/2010/06'); | ||
root.set('xmlns:arr', 'http://schemas.microsoft.com/2003/10/Serialization/Arrays'); | ||
root.set( | ||
'xmlns:arr', | ||
'http://schemas.microsoft.com/2003/10/Serialization/Arrays' | ||
); | ||
if (opts.namespaces) { | ||
opts.namespaces.forEach(function(namespace) { | ||
root.set('xmlns:' + namespace.name, namespace.src); | ||
}); | ||
} | ||
var header = subElement(root, 'soapenv:Header'); | ||
var tick = subElement(header, 'ns:Ticket'); | ||
tick.text = ticket; | ||
var body = subElement(root, 'soapenv:Body') | ||
if (opts.data){ | ||
var body = subElement(root, 'soapenv:Body'); | ||
if (opts.data) { | ||
walk(opts.data, body); | ||
@@ -49,3 +60,2 @@ } | ||
var xml = toXml(ticket, opts); | ||
console.log(xml); | ||
request.post({ | ||
@@ -70,2 +80,2 @@ body: xml, | ||
}); | ||
} | ||
}; |
{ | ||
"name": "adform", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "A node-module to integrate with the adform api (http://api.adform.com/Services/Documentation/Index.htm)", | ||
@@ -5,0 +5,0 @@ "main": "adform.js", |
@@ -17,2 +17,12 @@ var test = require('tap').test; | ||
test('SerializationError', function(t) { | ||
var body = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><s:Fault><faultcode>s:SerializationError</faultcode><faultstring xml:lang="en-US">Request serialization error occurred. Please check if your request is valid and data types are correct.</faultstring><detail><FaultDetails xmlns="http://www.adform.com/api/2010/06" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Errors><Error><Message>Request serialization error occurred. Please check if your request is valid and data types are correct.</Message><FieldPath i:nil="true"/></Error></Errors><RefNo i:nil="true"/></FaultDetails></detail></s:Fault></s:Body></s:Envelope>'; | ||
var err = errors.wrap(body); | ||
t.type(err, errors.SerializationError); | ||
t.equal(err.message, 'Request serialization error occurred. Please check if your request is valid and data types are correct.'); | ||
t.equal(err.body, body); | ||
t.end(); | ||
}); | ||
test('UnknownError', function(t) { | ||
@@ -19,0 +29,0 @@ var input = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><s:Fault><faultcode>s:SomeUnknownError</faultcode><faultstring xml:lang="en-US">SomeUnknownError</faultstring><detail><FaultDetails xmlns="http://www.adform.com/api/2010/06" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Errors><Error><Message>Ad name is not unique.</Message><FieldPath>Ad.Name</FieldPath></Error></Errors><RefNo i:nil="true"/></FaultDetails></detail></s:Fault></s:Body></s:Envelope>'; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13377
295