-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (112 loc) · 3.72 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
var request = require('request')
var pick = require('lodash.pick')
var async = require('async')
function CobudgetClient (params) {
this.authHeaders = {}
this.params = params
this.baseUri = 'https://cobudget-beta-api.herokuapp.com/api/v1'
}
CobudgetClient.prototype.init = function (callback) {
var self = this
request.post({
uri: self.baseUri + '/auth/sign_in',
form: self.params
}, function (err, response, body) {
var headers = response.toJSON().headers
self.authHeaders = pick(headers, ['access-token', 'uid', 'client', 'token-type'])
callback()
})
}
CobudgetClient.prototype.getItAll = function (groupId, callback) {
var self = this
async.parallel({
me: self.getMe.bind(self),
group: async.apply(self.getGroup.bind(self), groupId),
buckets: async.apply(self.getBucketsForGroup.bind(self), {id: groupId}),
allocations: async.apply(self.getAllocationsForGroup.bind(self), { id: groupId }),
users: async.apply(self.getUsersForGroup.bind(self), { id: groupId })
}, callback);
}
CobudgetClient.prototype.getMe = function (callback) {
var self = this
request.get({
uri: self.baseUri + '/users/me',
headers: self.authHeaders
}, function (err, response, body) {
if (err) return callback(err);
callback(null, JSON.parse(body).users[0]);
});
}
CobudgetClient.prototype.getGroup = function (id, callback) {
var self = this
request.get({
uri: self.baseUri + '/groups/' + id,
headers: self.authHeaders
}, function (err, response, body) {
if (err) return callback(err);
callback(null, JSON.parse(body).groups[0]);
});
}
CobudgetClient.prototype.getBucketsForGroup = function (group, callback) {
var self = this
request.get({
uri: self.baseUri + '/buckets?group_id=' + group.id,
headers: self.authHeaders
}, function (err, response, body) {
if (err) return callback(err);
var buckets = JSON.parse(body).buckets
async.map(buckets, self.getActivityForBucket.bind(self), callback)
});
}
CobudgetClient.prototype.getActivityForBucket = function (bucket, callback) {
var self = this
async.parallel({
comments: async.apply(self.getCommentsForBucket.bind(self), bucket),
contributions: async.apply(self.getContributionsForBucket.bind(self), bucket)
}, function (err, results) {
bucket.contributions = results.contributions
bucket.comments = results.comments
callback(null, bucket)
})
}
CobudgetClient.prototype.getAllocationsForGroup = function (group, callback) {
var self = this
request.get({
uri: self.baseUri + '/allocations?group_id=' + group.id,
headers: self.authHeaders
}, function (err, response, body) {
if (err) return callback(err);
callback(null, JSON.parse(body).allocations);
});
}
CobudgetClient.prototype.getContributionsForBucket = function (bucket, callback) {
var self = this
request.get({
uri: self.baseUri + '/contributions?bucket_id=' + bucket.id,
headers: self.authHeaders
}, function (err, response, body) {
if (err) return callback(err);
callback(null, JSON.parse(body).contributions);
});
}
CobudgetClient.prototype.getCommentsForBucket = function (bucket, callback) {
var self = this
request.get({
uri: self.baseUri + '/comments?bucket_id=' + bucket.id,
headers: self.authHeaders
}, function (err, response, body) {
if (err) return callback(err);
callback(null, JSON.parse(body).comments);
});
}
CobudgetClient.prototype.getUsersForGroup = function (group, callback) {
var self = this
request.get({
uri: self.baseUri + '/memberships?group_id=' + group.id,
headers: self.authHeaders
}, function (err, response, body) {
if (err) return callback(err);
callback(null, JSON.parse(body).users);
});
}
module.exports = CobudgetClient