-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
118 lines (96 loc) · 3.06 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
var request = require("request-promise");
var util = require("util");
var DEFAULT_REQUEST_OPTIONS = {
json: true,
simple: false,
resolveWithFullResponse: true
};
var ALLOWED_REQUEST_OPTIONS = ["timeout"];
function ForecastIo(apiKey, requestOptions) {
this.requestOptions = this.checkOptions(requestOptions);
this.apiKey = apiKey;
this.baseUrl = "https://api.forecast.io/forecast/" + this.apiKey + "/";
}
ForecastIo.prototype.forecast = function(latitude, longitude, options, optionalCallback) {
if (typeof options === "function") {
optionalCallback = options;
options = {};
}
var url = this.buildUrl(latitude, longitude);
return this.makeRequest(url, options, optionalCallback);
};
ForecastIo.prototype.timeMachine = function(latitude, longitude, time, options, optionalCallback) {
if (typeof options === "function") {
optionalCallback = options;
options = {};
}
var url = this.buildUrl(latitude, longitude, time);
return this.makeRequest(url, options, optionalCallback);
};
ForecastIo.prototype.checkOptions = function(userOptions) {
if (typeof userOptions === "undefined")
return DEFAULT_REQUEST_OPTIONS;
var options = cloneObject(DEFAULT_REQUEST_OPTIONS);
ALLOWED_REQUEST_OPTIONS.forEach(function(optionName) {
if (userOptions[optionName])
options[optionName] = userOptions[optionName];
});
return options;
};
ForecastIo.prototype.buildUrl = function(latitude, longitude, time) {
var url = this.baseUrl + latitude + "," + longitude;
if (typeof time !== "undefined") {
url += "," + time;
}
return url;
};
ForecastIo.prototype.makeRequest = function(url, queryString, optionalCallback) {
var requestOptions = this.requestOptions;
requestOptions.uri = url;
requestOptions.qs = queryString;
var promise = request(requestOptions)
.then(function(response) {
if (response.statusCode != 200) {
throw new ForecastIoAPIError(url, response.statusCode, response.body);
}
return response.body;
});
if (typeof(optionalCallback) !== "undefined") {
promise
.then(function(data) {
optionalCallback(null, data)
})
.catch(optionalCallback);
} else {
return promise;
}
};
/** Represents API errors. */
function ForecastIoAPIError(url, statusCode, body) {
this.response = {
statusCode: statusCode,
body: body
};
this.message = this._formatErrorMessage(body);
this.name = "ForecastIoAPIError";
Error.call(this);
Error.captureStackTrace(this, arguments.callee);
this.request = "GET " + url;
}
util.inherits(ForecastIoAPIError, Error);
ForecastIoAPIError.prototype.toString = function() {
return this.name + ": " + JSON.stringify({
message: this.mesage,
request: this.request,
response: this.response
}, null, 2);
};
ForecastIoAPIError.prototype._formatErrorMessage = function(body) {
if ((body.code !== undefined) && (body.error !== undefined))
return "[" + body.code + "] " + body.error;
return "Request Failed";
};
function cloneObject(obj) {
return JSON.parse(JSON.stringify(obj));
}
module.exports = ForecastIo;