-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpush.js
121 lines (109 loc) · 3.36 KB
/
push.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
var http = require('http');
var pusher_config = require('./pusher_config').get();
var simple_pusher = require('./vendor/simple_pusher');
var sys = require('sys');
var domain = 'www.groupon.com';
var dealsPath = '/api/v1/deals.json';
var divisionsPath = '/api/v1/divisions.json';
var FREQUENCY = 20000;
var dealDatabase = {};
(function loop() {
sys.puts("Looping");
setTimeout(loop, FREQUENCY);
try {
var divisionsRequest = createClient().request('GET', divisionsPath, {host: domain});
divisionsRequest.addListener('response', responseHandler(processDivisions));
divisionsRequest.end();
} catch (e) {
sys.puts("Error in loop(): " + sys.inspect(e));
}
})();
function processDivisions(divisionsData) {
try {
var divisions = JSON.parse(divisionsData).divisions;
divisions.forEach(function(division) {
var path = dealsPath + '?' +
'lat=' + division.location.latitude +
'&lng=' + division.location.longitude;
var dealsRequest = createClient().request('GET', path, {host: domain});
dealsRequest.addListener('response', responseHandler(processDeals(division)));
dealsRequest.end();
});
} catch (e) {
sys.puts("Error in processDivisions(): " + sys.inspect(e));
}
}
function processDeals(division) {
return function(dealsData) {
try {
var deals = JSON.parse(dealsData).deals;
} catch (e) {
sys.puts("Error parsing JSON in processDeals("+division.id+")");
return;
}
pushDivisionTotal(division, deals);
pushDealUpdates(deals);
}
}
function pushDivisionTotal(division, deals) {
try {
var total = 0;
deals.forEach(function(deal) {
if (deal.tipped) {
total += parseInt(deal.quantity_sold) * parseFloat(deal.discount_amount);
}
});
setTimeout(function() {
simple_pusher.trigger(pusher_config, division.id, 'citySavings', total);
}, FREQUENCY);
} catch (e) {
sys.puts("Error in pushDivisionTotal(): " + sys.inspect(e));
}
}
function pushDealUpdates(deals) {
try {
deals.forEach(function(deal) {
if (dealDatabase[deal.id]) {
var justPurchased = parseInt(deal.quantity_sold) - dealDatabase[deal.id];
// Push an event for *every* new deal purhcased
for (var i = 0; i < justPurchased; i++) {
var pushData = {
latitude: deal.division_lat,
longitude: deal.division_lng,
image: deal.medium_image_url,
url: deal.deal_url
}
setTimeout(function() {
simple_pusher.trigger(pusher_config, 'deals', 'purchase', pushData);
}, Math.random() * FREQUENCY);
}
}
dealDatabase[deal.id] = parseInt(deal.quantity_sold);
});
} catch (e) {
sys.puts("Error in pushDealUpdates(): " + sys.inspect(e));
}
}
function responseHandler(callback) {
return function(response) {
try {
var responseBody = "";
response.setEncoding('utf8');
response.addListener('data', function(chunk) {
responseBody += chunk;
});
response.addListener('end', function() {
callback(responseBody);
});
} catch (e) {
sys.puts("Error in responseHandler() " + sys.inspect(e));
}
}
}
function createClient() {
var client = http.createClient(80, domain);
client.addListener('error', function(error) {
sys.puts("Client error: " + error);
});
return client;
}