From 57b44e4dd662cbd548be4127f6775fadf3e54f79 Mon Sep 17 00:00:00 2001 From: Samer Albahra Date: Fri, 7 Aug 2015 20:31:13 -0500 Subject: [PATCH] Fix bug parsing adjustment options when encoded in \x?? format --- routes/weather.js | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/routes/weather.js b/routes/weather.js index d86ca25..f6cde40 100644 --- a/routes/weather.js +++ b/routes/weather.js @@ -115,11 +115,11 @@ function getWxWeatherData( location, callback ) { wind: parseInt( data.cc[0].wind[0].s[0] ) }; - Cache.findOne( { location: location }, function( err, record ) { - - if ( record && record.yesterdayHumidity !== null ) { - weather.yesterdayHumidity = record.yesterdayHumidity; - } + getCache( { + key: "yesterdayHumidity", + location: location + }, function( record ) { + weather.yesterdayHumidity = record.yesterdayHumidity; // Return the data to the callback function if successful callback( weather ); @@ -163,11 +163,11 @@ function getWeatherData( location, callback ) { location = location.join( "," ); - Cache.findOne( { location: location }, function( err, record ) { - - if ( record && record.yesterdayHumidity !== null ) { - weather.yesterdayHumidity = record.yesterdayHumidity; - } + getCache( { + key: "yesterdayHumidity", + location: location + }, function( record ) { + weather.yesterdayHumidity = record.yesterdayHumidity; // Return the data to the callback function if successful callback( weather ); @@ -205,6 +205,26 @@ function getYesterdayWeatherData( location, callback ) { } ); } +// Retrieve cached record for a given location +// opt is defined as an object with two required items +// opt.location defines the location for the cache record +// opt.key defines the key to return for the location +function getCache( opt, callback ) { + + // Find the cache entry for the provided location + Cache.findOne( { location: opt.location }, function( err, record ) { + + // 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 ); + } + } ); +} + // Update weather cache record in the local database function updateCache( location, weather ) { @@ -392,6 +412,9 @@ exports.getWeather = function( req, res ) { // Parse weather adjustment options try { + // Parse data that may be encoded + adjustmentOptions = decodeURIComponent( adjustmentOptions.replace( /\\x/g, "%" ) ); + // Reconstruct JSON string from deformed controller output adjustmentOptions = JSON.parse( "{" + adjustmentOptions + "}" ); } catch ( err ) {