-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwz.js
94 lines (77 loc) · 2.46 KB
/
wz.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
var rp = require('request-promise');
var _ = require('lodash');
var Promise = require('bluebird');
function WeezEvent(opts){
var self = this;
self.wz_user = opts.wz_user;
self.wz_pwd = opts.wz_pwd;
self.wz_api = opts.wz_api;
self.wz_event_id = opts.wz_event_id;
self.wz_access_token = null;
};
WeezEvent.prototype.init = function(opts){
var self = this;
// Retrieving weezevent access token given user/password
return rp({
uri: 'https://api.weezevent.com/auth/access_token',
method: 'POST',
qs: {
username: self.wz_user,
password: self.wz_pwd,
api_key: self.wz_api
},
json: true
}).then(function(result){
self.wz_access_token = result.accessToken;
console.log('Weezevent access token retrieved : %s', result.accessToken);
return Promise.resolve();
}).catch(function(err){
console.error("Error while retrieving Weezevent Access Token : %s", err);
return Promise.rejected("Error while retrieving Weezevent Access Token");
});
};
WeezEvent.prototype.fetchWZParticipants = function() {
var self = this;
return rp({
uri: 'https://api.weezevent.com/participants',
qs: {
access_token: self.wz_access_token,
api_key: self.wz_api,
"id_event[]": self.wz_event_id,
full: 1,
max: 512
},
json: true
}).then(function (results) {
console.log(JSON.stringify(results));
return results.participants;
});
};
function fillTicketsFromNode(tickets, categoryNode) {
tickets.push.apply(tickets, categoryNode.tickets || []);
if(categoryNode.categories) {
_.each(categoryNode.categories, function(categorySubNode) {
fillTicketsFromNode(tickets, categorySubNode);
});
}
}
WeezEvent.prototype.fetchWZEventTickets = function() {
var self = this;
return rp({
uri: 'https://api.weezevent.com/tickets',
qs: {
access_token: self.wz_access_token,
api_key: self.wz_api,
"id_event[]": self.wz_event_id
},
json: true
}).then(function (results) {
console.log(JSON.stringify(results));
var flattenedTickets = [];
_.each(results.events, function(event) {
fillTicketsFromNode(flattenedTickets, event);
});
return flattenedTickets;
});
};
module.exports = WeezEvent;