Created
March 21, 2015 05:25
-
-
Save shouse/8a319badebe27bbfc4d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @class Utils.Weather | |
* @author Steven House <[email protected]> | |
* | |
* This retrieves weather information | |
*/ | |
// Include underscore awesomness | |
var _ = require('alloy/underscore')._; | |
var Alloy = require('alloy'); | |
// Include logging utility | |
var log = Alloy.Globals.log; | |
log.debug('[Weather] : Opened Library'); | |
var xhr = require('network/httpClient'); | |
/** | |
* Get current weather information given a lat and lng | |
* @method getCurrentWeather | |
* @param {Object} args | |
*/ | |
exports.getCurrentWeather = function(args) { | |
log.debug('[Weather] : createNode() with args: ', args); | |
args = args || {}; | |
// Callbacks | |
var success = args.success ? args.success : function() {}; | |
var error = args.error ? args.error : function() {}; | |
// Lat and Lng Paramaters | |
var lat = args.lat ? args.lat : false; | |
var lng = args.lng ? args.lng : false; | |
// Bail if lat and lng not provided | |
if (!lat || !lng) { | |
log.warn( | |
"[weather] : Attempted to get current weather w/o Lan / Lng" | |
); | |
return false; | |
} | |
var url = "http://api.openweathermap.org/data/2.5/find?lat=" + lng + | |
"&lon=" + lat + "&cnt=1"; | |
// Setup for xhr call via httpClient.js | |
var xhrSetup = {}; | |
xhrSetup.requestType = "GET"; | |
xhrSetup.parseJSON = true; | |
xhrSetup.url = url; | |
xhrSetup.data = {}; | |
xhrSetup.success = function(resp) { | |
if (resp.cod) { | |
} | |
success(resp); | |
done(resp); | |
}; | |
xhrSetup.error = function(err, context) { | |
error(err, context); | |
done(err, context); | |
}; | |
xhr.request(xhrSetup); | |
}; | |
/** | |
* Convert Kelvin to Fahrenheit | |
* (K - 273.15)* 1.8000 + 32.00 | |
* @method kelvinToFahrenheit | |
*/ | |
function kelvinToFahrenheit(degKelvin) { | |
var degF = (degKelvin - 273.15) * 1.8000 + 32.00; | |
return degF; | |
} | |
// An example response for current weather conditions | |
var exampleResp = { | |
message: "accurate", | |
cod: "200", | |
count: 1, | |
list: [{ | |
id: 4393217, | |
name: "Kansas City", | |
coord: { | |
lon: -94.578568, | |
lat: 39.099731 | |
}, | |
main: { | |
temp: 295.34, | |
pressure: 1002, | |
humidity: 53, | |
temp_min: 295.15, | |
temp_max: 296.15 | |
}, | |
dt: 1414428840, | |
wind: { | |
speed: 5.7, | |
deg: 200, | |
gust: 10.8 | |
}, | |
sys: { | |
country: "" | |
}, | |
clouds: { | |
all: 1 | |
}, | |
weather: [{ | |
id: 800, | |
main: "Clear", | |
description: "Sky is Clear", | |
icon: "01d" | |
//http://openweathermap.org/img/w/01d.png | |
}] | |
}] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment