-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.js
143 lines (114 loc) · 3.55 KB
/
response.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
Response = function(type) {
this.type = type;
this.errorMessage = (type == Response.ERROR ? 'Please try again with a *real* mountain this time' : '');
this.errorDetail = '';
this.message = '';
this.url = undefined;
this.name = undefined;
this.country = undefined;
this.attachments = [];
this.elevations = undefined;
this.output = {};
};
Response.prototype.generate = function() {
if (this.type == Response.ERROR) {
this.generateError();
} else
{
this.generateAnswer();
}
return this.output;
};
Response.prototype.generateAnswer = function() {
this.output['response_type'] = 'in_channel';
this.output['text'] = this.message;
if (this.attachments.length > 0) {
this.output['attachments'] = this.attachments;
};
};
Response.prototype.createGenericMessage = function() {
if (this.url && this.name && this.country) {
this.message = 'Weather forecast for <' + this.url + '|' + this.name + '>, ' + this.country;
} else {
throw new Error('URL, peak name and country values must not be undefined');
}
};
Response.prototype.attachWeather = function(weather, altitude) {
if (!this.elevations) {
throw new Error('Must provide elevations');
}
var att = {};
var i = 0;
att['text'] = 'Current weather is ' + weather.summaries[i] + ' (altitude *' + altitude + ' m*)';
var fields = [];
fields = Response.pushNewField('Wind', weather.winds[i] + ' km/h', fields);
fields = Response.pushNewField('Snow', weather.snow[i] + ' cm', fields);
fields = Response.pushNewField('High', weather.high[i] + ' °C', fields);
fields = Response.pushNewField('Low', weather.low[i] + ' °C', fields);
fields = Response.pushNewField('Wind chill', weather.chill[i] + ' °C', fields);
fields = Response.pushNewField('Freezing level', weather.freezing[i] + ' m', fields);
att['fields'] = fields;
att['mrkdwn_in'] = ['text'];
var actions = [];
for (var i = 0; i < 4 && i < this.elevations.length; i++) {
var altitude = this.elevations[i].split('/');
altitude = altitude[altitude.length - 1];
var style = (i == 0) ? 'primary': 'default';
var action = {
'name': 'elevation',
'text': altitude + ' m',
'type': 'button',
'style': style,
'value': this.elevations[i]
}
actions.push(action);
}
att['actions'] = actions;
this.attachments.push(att);
}
Response.prototype.setMessage = function(message) {
this.message = message;
};
Response.prototype.setUrl = function(url) {
this.url = url;
};
Response.prototype.setName = function(name) {
this.name = name;
};
Response.prototype.setCountry = function(country) {
this.country = country;
};
Response.prototype.setElevations = function(elevations) {
this.elevations = elevations;
};
Response.prototype.generateError = function() {
this.output['response_type'] = 'in_channel';
this.output['text'] = 'I\'m sorry, I could not find this mountain.';
var text = this.errorMessage;
if (this.errorDetail != '') {
text += '\n\n_Error detail_:\n' + this.errorDetail;
}
this.output['attachments'] = [{
'mrkdwn_in': ['text'],
'color': 'danger',
'text': text
}];
};
Response.prototype.setErrorMessage = function (message) {
this.errorMessage = message;
};
Response.prototype.setErrorDetail = function (message) {
this.errorDetail = message;
};
Response.pushNewField = function(title, value, fields, short) {
if (short == undefined) short = true;
fields.push({
'title': title,
'value': value,
'short': short
});
return fields;
};
Response.SUCCESS = 0;
Response.ERROR = 1;
module.exports = Response;