-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwundergroundnode.js
160 lines (137 loc) · 4.59 KB
/
wundergroundnode.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*jshint esversion: 6,node: true,-W041: false */
var request = require('request');
var _ = require('underscore');
var moment = require('moment');
var Wunderground = function(apikey, language) {
"use strict";
var that = this;
that.chainedRequests = [];
var format = ".json";
that.conditions = function() {
this.chainedRequests.push("conditions/");
return this;
};
that.alerts = function() {
this.chainedRequests.push("alerts/");
return this;
};
that.hourlyForecast = function() {
this.chainedRequests.push("hourly/");
return this;
};
that.hourlyTenDayForecast = function(){
this.chainedRequests.push('hourly10day/');
return this;
};
that.forecast = function() {
this.chainedRequests.push("forecast/");
return this;
};
that.forecastTenDay = function() {
this.chainedRequests.push("forecast10day/");
return this;
};
that.almanac = function() {
this.chainedRequests.push("almanac/");
return this;
};
that.yesterday = function() {
this.chainedRequests.push("yesterday/");
return this;
};
that.geolookup = function() {
this.chainedRequests.push("geolookup/");
return this;
};
that.astronomy = function() {
this.chainedRequests.push("astronomy/");
return this;
};
that.planner = function(startDay, endDay){
var startDayFormatted = startDay.replace('/', ''),
endDayFormatted = endDay.replace('/', '');
this.chainedRequests.push("planner_"+startDayFormatted+endDayFormatted+"/");
return this;
};
/**
* Historical request, cannot be chained.
*
* @param day String with format 'YYYYMMDD' or date object
* @param query Location Query (e.g., 84010)
* @param callback function to return results to
*/
that.history = function(day, query, callback){
if (_.isDate(day)){
var dayMoment = moment(day);
day = dayMoment.format('YYYYMMDD');
}
// A little pre-query validation
if (!query){
callback(true, "You must supply a query");
return;
}else if (!day || !_.isString(day)){
callback(true, "You must supply a valid day");
return;
}else if (!_.isFunction(callback)){
throw "The third argument must be a function";
}
// Construct the url
var url = 'http://api.wunderground.com/api/' + apikey + '/history_' + day + '/q/'+query + format;
// Request the url
request(url, function (error, response, body) {
var json = false;
if (!error && response.statusCode === 200) {
error = false;
try {
json = JSON.parse(body);
} catch (err) {
console.error('Exception caught in JSON.parse', body);
error = err;
}
}
callback.call(that, error, json);
return;
});
};
/**
* Performs the actual request
*
* @param query
* @param callback
*/
that.request = function(query, callback){
// A little pre-query validation
var url;
if (!query){
callback(true, "You must supply a query");
return;
}else if (!that.chainedRequests.length){
callback(true, "You must specify a resource to request first (e.g., wu.conditions().req...)");
return;
}else if (!_.isFunction(callback)){
throw "The second argument must be a function";
}
// Construct the url
if (language===undefined)
url = 'http://api.wunderground.com/api/' + apikey + '/' + that.chainedRequests.join('') + 'q/'+query + format;
else
url = 'http://api.wunderground.com/api/' + apikey + '/lang:' + language + '/' + that.chainedRequests.join('') + 'q/'+query + format;
that.chainedRequests = [];
// Request the url
request(url, function (error, response, body) {
var json = false;
if (!error && response.statusCode === 200) {
error = false;
try {
json = JSON.parse(body);
} catch (err) {
console.error('Exception caught in JSON.parse', body);
error = err;
}
}
callback.call(that, error, json);
return;
});
};
};
module.exports = Wunderground;