-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathti.forecast.io.js
363 lines (342 loc) · 7.42 KB
/
ti.forecast.io.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* Helper Class for forecast.io webservice
* Including moment.js for date help.
* HTTTClient supports Cross-Domain XHR - no need for callbacks
*
* MIT License (MIT)
* Copyright (c) 2013 Ian Tearle @iantearle
*
*/
var moment = require('alloy/moment');
function ForecastIO() {
FORECAST_API = 'API_KEY'; // Sign up at https://developer.forecast.io/
FORECAST_URL = 'https://api.forecast.io/forecast/';
this.requestData = function(latitude, longitude, onSuccess, onError, timestamp) {
var onSuccess = onSuccess || function(){};
var onError = onError || function(){};
var timestamp = (timestamp) ? ','+timestamp : '';
var request_url = FORECAST_URL + FORECAST_API + '/' + latitude + ',' + longitude + timestamp + '?units=uk';
var xhr = Titanium.Network.createHTTPClient({
enableKeepAlive: false
});
// Create the result object
var result = {};
// Open the HTTP connection
xhr.open("GET", request_url);
// When the connection was successful
xhr.onload = function() {
// Check the status of this
result.status = xhr.status == 200 ? "ok" : xhr.status;
result.data = JSON.parse(xhr.responseText);
onSuccess(result);
};
// When there was an error
xhr.onerror = function(e) {
// Check the status of this
result.status = "error";
result.data = e;
result.code = xhr.status;
onError(result);
};
xhr.send();
}
/**
* Will return the current conditions
*
* @param float $latitude
* @param float $longitude
* @return \ForecastIOConditions|boolean
*/
this.getCurrentConditions = function(latitude, longitude, onSuccess, onError, timestamp) {
var onSuccess = onSuccess || function(){};
var onError = onError || function(){};
this.requestData(latitude, longitude, onSuccessCallback, onErrorCallback, timestamp);
function onSuccessCallback(e) {
var bigReturn = new ForecastIOConditions(e.data.currently);
onSuccess(bigReturn);
};
function onErrorCallback(e) {
// Handle your errors in here
onError("error");
};
}
/**
* Will return conditions on hourly basis for today
*
* @param type $latitude
* @param type $longitude
* @return \ForecastIOConditions|boolean
*/
this.getForecastToday = function(latitude, longitude, onSuccess, onError, timestamp) {
var onSuccess = onSuccess || function(){};
var onError = onError || function(){};
this.requestData(latitude, longitude, onSuccessCallback, onErrorCallback, timestamp);
function onSuccessCallback(e) {
conditions = [];
today = moment().format("YYYY-MM-DD");
for(i=0; i<e.data.hourly.data.length; i++) {
raw_data = e.data.hourly.data[i];
if(moment.unix(raw_data.time).format("YYYY-MM-DD") == today) {
conditions.push(new ForecastIOConditions(raw_data));
}
}
onSuccess(conditions);
};
function onErrorCallback(e) {
// Handle your errors in here
onError("error");
};
}
/**
* Will return daily conditions for next seven days
*
* @param float $latitude
* @param float $longitude
* @return \ForecastIOConditions|boolean
*/
this.getForecastWeek = function(latitude, longitude, onSuccess, onError, timestamp) {
var onSuccess = onSuccess || function(){};
var onError = onError || function(){};
this.requestData(latitude, longitude, onSuccessCallback, onErrorCallback, timestamp);
function onSuccessCallback(e) {
var conditions = [];
for(i=0; i<e.data.daily.data.length; i++) {
raw_data = e.data.daily.data[i];
conditions.push(new ForecastIOConditions(raw_data));
}
var bigReturn = conditions;
onSuccess(bigReturn);
};
function onErrorCallback(e) {
// Handle your errors in here
onError("error");
};
}
}
/**
* Wrapper for get data by getters
*/
function ForecastIOConditions(raw_data) {
ForecastIOConditions.prototype = {
raw_data: raw_data
}
/**
* Get the time, when $format not set timestamp else formatted time
*
* @param String $format
* @return String
*/
this.getTime = function(format) {
if(!format) {
return raw_data.time;
} else {
return moment.unix(raw_data.time).format(format);
}
}
/**
* Get the summary of the conditions
*
* @return String
*/
this.getSummary = function() {
return raw_data.summary;
}
/**
* Get the icon of the conditions
*
* @return String
*/
this.getIcon = function() {
return raw_data.icon;
}
/**
* get sunrise time
*
* only available for week forecast
*
* @return type
*/
this.getSunrise = function() {
return raw_data.sunriseTime;
}
/**
* get sunset time
*
* only available for week forecast
*
* @return type
*/
this.getSunset = function() {
return raw_data.sunsetTime;
}
/**
* get precipitation probability
*
* @return type
*/
this.getPrecipitationProbability = function() {
return raw_data.precipProbability;
}
/**
* get precipitation accumilation
*
* only available for week forecast
*
* @return type
*/
this.getPrecipitationAccumulation = function() {
return raw_data.precipAccumulation;
}
/**
* get precipitation intensity
*
* @return type
*/
this.getPrecipitationIntensity = function() {
return raw_data.precipIntensity;
}
/**
* get precipitation max
*
* only available for week forecast
*
* @return type
*/
this.getPrecipitationIntensityMax = function() {
return raw_data.precipIntensityMax;
}
/**
* get precipitation max time
*
* only available for week forecast
*
* @return type
*/
this.getPrecipitationIntensityMaxTime = function() {
return raw_data.precipIntensityMaxTime;
}
/**
* get precipitation type
*
* only available for week forecast
*
* @return type
*/
this.getPrecipitationType = function() {
return raw_data.precipType;
}
/**
* Will return the temperature
*
* only available for week forecast
*
* @return String
*/
this.getTemperature = function() {
return raw_data.temperature;
}
/**
* get the min temperature
*
* only available for week forecast
*
* @return type
*/
this.getMinTemperature = function() {
return raw_data.temperatureMin;
}
/**
* get the min temperature time
*
* only available for week forecast
*
* @return type
*/
this.getMinTemperatureTime = function() {
return raw_data.temperatureMinTime;
}
/**
* get max temperature
*
* only available for week forecast
*
* @return type
*/
this.getMaxTemperature = function() {
return raw_data.temperatureMax;
}
/**
* get max temperature time
*
* only available for week forecast
*
* @return type
*/
this.getMaxTemperatureTime = function() {
return raw_data.temperatureMaxTime;
}
/**
* Get the pressure
*
* @return String
*/
this.getDewPoint = function() {
return raw_data.dewPoint;
}
/**
* Get the wind speed
*
* @return String
*/
this.getWindSpeed = function() {
return raw_data.windSpeed;
}
/**
* Get wind direction
*
* @return type
*/
this.getWindBearing = function() {
return raw_data.windBearing;
}
/**
* Get the cloud cover
*
* @return type
*/
this.getCloudCover = function() {
return raw_data.cloudCover;
}
/**
* get humidity
*
* @return String
*/
this.getHumidity = function() {
return raw_data.humidity;
}
/**
* Get the pressure
*
* @return String
*/
this.getPressure = function() {
return raw_data.pressure;
}
/**
* Get the visibility
*
* @return String
*/
this.getVisibility = function() {
return raw_data.visibility;
}
/**
* Get the visibility
*
* @return String
*/
this.getOzone = function() {
return raw_data.ozone;
}
}