-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
146 lines (126 loc) · 3.67 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
schedule = require('node-schedule');
request = require('request');
config = require('./config');
// BostonFeed URLs
location_url = 'http://bostonfeed.me/backend/getLocation.php';
truck_url = 'http://bostonfeed.me/backend/getTruck.php';
// Sets up and sends the populated food truck list to Slack.
function sendFoodTruckList() {
request(truck_url, function (error, response, body) {
if (!error && response.statusCode == 200) {
truck_info = JSON.parse(body);
var found_trucks = [];
var current_date = new Date();
var current_day = current_date.getDay();
request(location_url, function (error, response, body) {
if (!error && response.statusCode == 200) {
body = JSON.parse(body);
truck_arr = Object.keys(body).map(function(k) { return body[k] });
for (var i = 0; i < truck_arr.length; i++) {
truck = truck_arr[i];
days = truck.day.split(',');
if (days.indexOf(current_day.toString()) > -1) {
for (var j = 0; j < config.locations.length; j++) {
if (truck.location_id === config.locations[j]) {
truck = mapTruck(truck_info, truck);
if (!(truck.location in found_trucks)) {
found_trucks[truck.location] = [truck];
} else {
found_trucks[truck.location].push(truck);
}
}
}
}
}
}
sendSlackReponse(found_trucks);
});
}
});
}
// Maps the truck information with the found location trucks.
function mapTruck(truck_info, found_truck) {
for (var i = 0; i < truck_info.length; i++) {
if (truck_info[i].route_name.toUpperCase() === found_truck.truck_route.toUpperCase()) {
found_truck.name_url = 'https://twitter.com/' + truck_info[i].twitter;
found_truck.yelp_rating_emoji = parseYelpRating(truck_info[i].yelp_rating);
found_truck.type_emoji = parseType(truck_info[i].type_name);
}
}
return found_truck;
}
// Parses the Yelp rating into :star: emojis.
function parseYelpRating(rating) {
var rating_int = parseInt(rating);
var rating_str = ':star:';
for (var i = 0; i < rating_int - 1; i++) {
rating_str = rating_str + ':star:';
}
return rating_str;
}
// Parses the food truck type and returns an emoji message based on it.
function parseType(type) {
switch(type) {
case 'Mexican':
return ':taco:'
break;
case 'Asian':
return ':ramen:'
break;
case 'Healthy':
return ':apple:'
break;
case 'American':
return ':us:'
break;
case 'International':
return ':earth_americas:'
break;
case 'Sandwiches':
return ':bread:'
break;
case 'Burgers':
return ':hamburger:'
break;
case 'Dessert':
return ':cake:'
break;
case 'Pizza':
return ':pizza:'
break;
case 'Drinks':
return ':tropical_drink:'
break;
default:
return '';
}
}
// Sends the Slack message via an incoming webhook.
function sendSlackReponse(found_trucks) {
var slack_message = buildSlackMessage(found_trucks);
request.post({url: config.slack_url, body: slack_message}, function (error, response, body) {
console.log(body);
});
}
// Builds the Slack message to send.
function buildSlackMessage(found_trucks) {
var text = '';
for (var location in found_trucks) {
text = text + '*' + location + '*' + '\n';
trucks_arr = found_trucks[location];
for (var i = 0; i < trucks_arr.length; i++) {
truck = trucks_arr[i];
text = text + '> ' + truck.type_emoji + ' *' + truck.truck_name + ':* ' + truck.name_url + ' ' + truck.yelp_rating_emoji + '\n'
}
}
var json_obj = {
username: config.slack_bot_name,
icon_emoji: config.slack_bot_emoji,
text: text
}
return JSON.stringify(json_obj);
}
// Schedule a job every Mon-Fri at 16:00 UTC.
schedule.scheduleJob(config.job_time, function(){
sendFoodTruckList();
});