-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiDataForIronRouter.js
58 lines (47 loc) · 1.81 KB
/
apiDataForIronRouter.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
var Fiber = require('fibers');
var Future = require('fibers/futures');
//client only collection for storing the retrieved data
var Subscriptions = new Mongo.Collection('subscription');
//use waitOn to subscribe to Meteor publications
Router.route('/admin', {
name: 'admin',
waitOn: function() {
return [Meteor.subscribe("account"), Meteor.subscribe("subscription")];
},
action: function(){
this.render('admin');
}
});
//create a publication on the server that sends DDP messages to the clients after the data is retrieved using added and ready methods
Meteor.publish("subscription", function subscriptionPublish() {
var context = this;
var uuid = Meteor.users.findOne(this.userId).planUuid;
Meteor.call('getSubscription', uuid, function(error, subscription){
if (error) {
context.ready();
} else {
context.added("subscription", uuid, subscription);
context.ready();
}
});
});
//retrieve data asynchronously on the server and block using a future
Meteor.methods({
getSubscription: function getSubscription(uuid) {
var subscriptionInfo = new Future();
Meteor.http.get(Meteor.settings.private.recurlyURL + 'subscriptions/' + uuid, {content: "", headers: {"Authorization": "Basic " + RECURLY_API_KEY, "Accept": "application/xml", "Content-Type": "application/xml; charset=utf-8"}}, function(error, response) {
var content;
if (error) {
subscriptionInfo.return(new Meteor.Error('recurly-error', error));
} else {
xml2js.parseString(response.content, function(error, response) {
content = response;
});
subscriptionInfo.return(content.subscription);
}
});
return subscriptionInfo.wait();
}
});
//on the client we can then retrieve our new subscription
var subscription = Subscriptions.findOne({});