-
Notifications
You must be signed in to change notification settings - Fork 6
/
createEngine.js
144 lines (121 loc) · 3.86 KB
/
createEngine.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
// Create story board
// Map onto functions
const { Wit } = require('node-wit');
const { WIT_TOKEN, WEATHER_APPID } = require('./config');
const fetch = require('isomorphic-fetch');
// const request = require('request');
// ------------------------------------------------------------
// Helpers
function mapObject(obj, f) {
return Object
.keys(obj)
.map(k => [k, f(obj[k], k)])
.reduce(
(newObj, [k, v]) => {
newObj[k] = v;
return newObj;
}, {}
);
}
// function forecastFor(apiRes) {
// return `${fahrenheit(apiRes.main.temp)}, ${apiRes.weather[0].description}`
// }
// function locationFor(apiRes) {
// return apiRes.name;
// }
// function fahrenheit(kelvin) {
// return `${Math.round(kelvin * 9/5 - 459.67)} °F`
// }
function getRoute(loc) {
var url = 'https://maps.googleapis.com/maps/api/directions/json?origin=Atlanta&destination=' + loc + '&key=AIzaSyA_2lY9VZ5_ohmSOkdvaDN2cGryDcecwmU';
console.log(url, 'URL');
return fetch(
url
).then(res => res.json())
}
const firstEntityValue = (entities, name) => {
const val = entities && entities[name] &&
Array.isArray(entities[name]) &&
entities[name].length > 0 &&
entities[name][0].value;
console.log('entities[name] or whatever that means-------', entities[name]);
console.log('what are entities?-----------', entities);
if (!val) {
return null;
}
return typeof val === 'object' ? val.value : val;
};
const noop = () => {};
// ------------------------------------------------------------
// Actions
const noLocation = (ctx) => {
ctx.missingLocation = true;
delete ctx.forecast;
return ctx;
}
const withLocation = (ctx, loc) => {
ctx.location = loc;
delete ctx.missingLocation;
return ctx;
}
// const withForecast = (ctx, forecast) => {
// ctx.forecast = forecast;
// return ctx;
// }
const withAPIError = (ctx, err) => {
ctx.apiError = true
return ctx;
}
function wrapActions(actions, cb) {
return mapObject(
actions,
(f, k) => function() {
const args = [].slice.call(arguments);
console.log('these are the args---------', args);
cb({ name: k, args })
return f.apply(null, arguments);
}
);
}
function fetchBestRoute({ context, entities }) {
const location = firstEntityValue(entities, 'location');
if (!location) return Promise.resolve(noLocation(context));
return getRoute(location).then(
res => {
// console.log("Code reached", res);
// Tried changing to var/let
const routeSummaryReturned = res.routes[0].summary;
context.routeSummary = routeSummaryReturned
return context;
// Tried looping and returning a single value.
// for (var i = 0; i < res.routes.length; i++) {
// const routeSummaryReturned = res.routes[i].summary;
// return context.routeSummary = routeSummaryReturned
// };
// return res.routes[0].summary;
// return withLocation(
// withForecast(context, forecastFor(res)),
// locationFor(res)
// );
},
err => withAPIError(withLocation(context, location), err)
);
}
const actions = {
send(request, response) {
console.log('this is the response', response);
console.log('stringified-------', JSON.stringify(response))
// console.log('sending...', JSON.stringify(response));
return Promise.resolve();
},
fetchBestRoute
};
// ------------------------------------------------------------
// init
function createEngine(accessToken, cb) {
return new Wit({
accessToken: WIT_TOKEN,
actions: wrapActions(actions, cb || noop)
});
}
module.exports = createEngine;