-
Notifications
You must be signed in to change notification settings - Fork 4
/
node_helper.js
executable file
·95 lines (82 loc) · 2.29 KB
/
node_helper.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
'use strict';
/* Magic Mirror
* Module: MMM-lyft
*
* By Kyle Kelly
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
var request = require('request');
var moment = require('moment');
module.exports = NodeHelper.create({
start: function() {
var self = this;
console.log("Starting node helper for: " + this.name);
this.config = null;
},
getAccessToken: function() {
var self = this;
request({
url: 'https://api.lyft.com/oauth/token',
method: 'POST',
auth: {
user: this.config.clientId,
pass: this.config.clientSecret
},
form: {
'grant_type': 'client_credentials'
}
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
self.sendSocketNotification("ACCESS_SUCCESS", null);
var json = JSON.parse(body);
self.config.access_token = json.access_token;
}
else {
self.sendSocketNotification("ACCESS_ERROR", "In OAUTH request with status code: " + response.statusCode);
}
});
},
getData: function() {
var self = this;
request({
url: 'https://api.lyft.com/v1/eta?lat=' + this.config.lat + '&lng=' + this.config.lng,
method: 'GET',
auth: {
'bearer': this.config.access_token
}
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
self.sendSocketNotification("TIME", body);
}
else {
self.sendSocketNotification("TIME_ERROR", "In TIME request with status code: " + response.statusCode);
}
});
request({
url: 'https://api.lyft.com/v1/cost?start_lat=' + this.config.lat + '&start_lng=' + this.config.lng + '&end_lat=' + this.config.lat + '&end_lng=' + this.config.lng,
method: 'GET',
auth: {
'bearer': this.config.access_token
}
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
self.sendSocketNotification("COST", body);
}
else {
self.sendSocketNotification("COST_ERROR", "In COST request with status code: " + response.statusCode);
}
});
},
socketNotificationReceived: function(notification, payload) {
if (notification === "CONFIG") {
this.config = payload;
}
else if (notification === "ACCESS" && this.config !== null) {
this.getAccessToken();
}
else if (notification === "DATA" && this.config !== null){
this.getData();
}
}
});