Small improvements and code cleaning

This commit is contained in:
Samer Albahra
2015-08-11 15:21:57 -05:00
parent 76acc76f0f
commit 36a70a14ce
2 changed files with 33 additions and 23 deletions

View File

@@ -17,9 +17,8 @@
"devDependencies": { "devDependencies": {
"chai": "^3.0.0", "chai": "^3.0.0",
"codecov.io": "^0.1.5", "codecov.io": "^0.1.5",
"grunt": "^0.4.5",
"grunt-contrib-jshint": "^0.11.2", "grunt-contrib-jshint": "^0.11.2",
"hippie": "^0.3.0", "hippie": "^0.4.0",
"istanbul": "^0.3.17", "istanbul": "^0.3.17",
"mocha": "^2.2.5", "mocha": "^2.2.5",
"nock": "^2.7.0" "nock": "^2.7.0"

View File

@@ -48,6 +48,11 @@ function resolveWxLocation( location, callback ) {
httpRequest( url, function( xml ) { httpRequest( url, function( xml ) {
parseXML( xml, function( err, result ) { parseXML( xml, function( err, result ) {
if ( err ) {
callback( null );
return;
}
callback( result.search.loc[0].$.id ); callback( result.search.loc[0].$.id );
} ); } );
} ); } );
@@ -101,6 +106,11 @@ function getWxWeatherData( location, callback ) {
httpRequest( url, function( xml ) { httpRequest( url, function( xml ) {
parseXML( xml, function( err, data ) { parseXML( xml, function( err, data ) {
if ( err ) {
callback( null );
return;
}
data = data.weather; data = data.weather;
var tz = parseInt( data.loc[0].zone[0] ), var tz = parseInt( data.loc[0].zone[0] ),
@@ -117,14 +127,9 @@ function getWxWeatherData( location, callback ) {
getCache( { getCache( {
key: "yesterdayHumidity", key: "yesterdayHumidity",
location: location location: location,
}, function( record ) { weather: weather,
if ( record ) { callback: callback
weather.yesterdayHumidity = record.yesterdayHumidity;
}
// Return the data to the callback function if successful
callback( weather );
} ); } );
updateCache( location, weather ); updateCache( location, weather );
@@ -167,14 +172,9 @@ function getWeatherData( location, callback ) {
getCache( { getCache( {
key: "yesterdayHumidity", key: "yesterdayHumidity",
location: location location: location,
}, function( record ) { weather: weather,
if ( record ) { callback: callback
weather.yesterdayHumidity = record.yesterdayHumidity;
}
// Return the data to the callback function if successful
callback( weather );
} ); } );
updateCache( location, weather ); updateCache( location, weather );
@@ -204,6 +204,11 @@ function getYesterdayWeatherData( location, callback ) {
// Perform the HTTP request to retrieve the weather data // Perform the HTTP request to retrieve the weather data
httpRequest( url, function( xml ) { httpRequest( url, function( xml ) {
parseXML( xml, function( err, result ) { parseXML( xml, function( err, result ) {
if ( err ) {
callback( null );
return;
}
callback( result.WeatherResponse.WeatherRecords[0].WeatherData[0].$ ); callback( result.WeatherResponse.WeatherRecords[0].WeatherData[0].$ );
} ); } );
} ); } );
@@ -218,14 +223,16 @@ function getCache( opt, callback ) {
// Find the cache entry for the provided location // Find the cache entry for the provided location
Cache.findOne( { location: opt.location }, function( err, record ) { Cache.findOne( { location: opt.location }, function( err, record ) {
if ( err ) {
return;
}
// If a record is found for the provided key, return it // If a record is found for the provided key, return it
if ( record && record[ opt.key ] !== null ) { if ( record && record[ opt.key ] !== null ) {
callback( record[ opt.key ] ); opt.weather[ opt.key ] = record[ opt.key ];
} else {
// Otherwise return null indicating no match is found
callback( null );
} }
callback( opt.weather );
} ); } );
} }
@@ -235,6 +242,10 @@ function updateCache( location, weather ) {
// Search for a cache record for the provided location // Search for a cache record for the provided location
Cache.findOne( { location: location }, function( err, record ) { Cache.findOne( { location: location }, function( err, record ) {
if ( err ) {
return;
}
// If a record is found update the data and save it // If a record is found update the data and save it
if ( record ) { if ( record ) {