From 36a70a14ce15b001ebea8f5a472cc9f3a1c55e6a Mon Sep 17 00:00:00 2001 From: Samer Albahra Date: Tue, 11 Aug 2015 15:21:57 -0500 Subject: [PATCH] Small improvements and code cleaning --- package.json | 3 +-- routes/weather.js | 53 ++++++++++++++++++++++++++++------------------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 092d436..dbc2350 100644 --- a/package.json +++ b/package.json @@ -17,9 +17,8 @@ "devDependencies": { "chai": "^3.0.0", "codecov.io": "^0.1.5", - "grunt": "^0.4.5", "grunt-contrib-jshint": "^0.11.2", - "hippie": "^0.3.0", + "hippie": "^0.4.0", "istanbul": "^0.3.17", "mocha": "^2.2.5", "nock": "^2.7.0" diff --git a/routes/weather.js b/routes/weather.js index a0d110b..d3eadd5 100644 --- a/routes/weather.js +++ b/routes/weather.js @@ -48,6 +48,11 @@ function resolveWxLocation( location, callback ) { httpRequest( url, function( xml ) { parseXML( xml, function( err, result ) { + if ( err ) { + callback( null ); + return; + } + callback( result.search.loc[0].$.id ); } ); } ); @@ -101,6 +106,11 @@ function getWxWeatherData( location, callback ) { httpRequest( url, function( xml ) { parseXML( xml, function( err, data ) { + if ( err ) { + callback( null ); + return; + } + data = data.weather; var tz = parseInt( data.loc[0].zone[0] ), @@ -117,14 +127,9 @@ function getWxWeatherData( location, callback ) { getCache( { key: "yesterdayHumidity", - location: location - }, function( record ) { - if ( record ) { - weather.yesterdayHumidity = record.yesterdayHumidity; - } - - // Return the data to the callback function if successful - callback( weather ); + location: location, + weather: weather, + callback: callback } ); updateCache( location, weather ); @@ -167,14 +172,9 @@ function getWeatherData( location, callback ) { getCache( { key: "yesterdayHumidity", - location: location - }, function( record ) { - if ( record ) { - weather.yesterdayHumidity = record.yesterdayHumidity; - } - - // Return the data to the callback function if successful - callback( weather ); + location: location, + weather: weather, + callback: callback } ); updateCache( location, weather ); @@ -204,6 +204,11 @@ function getYesterdayWeatherData( location, callback ) { // Perform the HTTP request to retrieve the weather data httpRequest( url, function( xml ) { parseXML( xml, function( err, result ) { + if ( err ) { + callback( null ); + return; + } + callback( result.WeatherResponse.WeatherRecords[0].WeatherData[0].$ ); } ); } ); @@ -218,14 +223,16 @@ function getCache( opt, callback ) { // Find the cache entry for the provided location Cache.findOne( { location: opt.location }, function( err, record ) { + if ( err ) { + return; + } + // If a record is found for the provided key, return it if ( record && record[ opt.key ] !== null ) { - callback( record[ opt.key ] ); - } else { - - // Otherwise return null indicating no match is found - callback( null ); + opt.weather[ opt.key ] = record[ opt.key ]; } + + callback( opt.weather ); } ); } @@ -235,6 +242,10 @@ function updateCache( location, weather ) { // Search for a cache record for the provided location Cache.findOne( { location: location }, function( err, record ) { + if ( err ) { + return; + } + // If a record is found update the data and save it if ( record ) {