From 7b017b0d38094a712e1e5183a891313a7bab09b9 Mon Sep 17 00:00:00 2001
From: Erick Hitter <services@ethitter.com>
Date: Fri, 13 Jan 2017 21:59:45 -0800
Subject: [PATCH] Reformat to introduce forecast and provide better display

---
 index.js | 47 +++++++++++++++++++++++++++++++++--------------
 1 file changed, 33 insertions(+), 14 deletions(-)

diff --git a/index.js b/index.js
index 3256f91..490fc71 100644
--- a/index.js
+++ b/index.js
@@ -41,7 +41,7 @@ app.post('/post', function(req, res){
     pathname: 'http://api.wunderground.com/api/' + apikey + '/conditions/q/' + req.body.text + format,
   });
 
-  console.log(parsed_url);
+  console.log( req.body.text );
 
   request(parsed_url, function (error, response, body) {
     if ( error || 200 !== response.statusCode ) {
@@ -55,19 +55,38 @@ app.post('/post', function(req, res){
     var icon_url = conditionData.current_observation.icon_url;
     var location = conditionData.current_observation.display_location.full;
 
-    var body = {
-      response_type: "in_channel",
-      "attachments": [
-        {
-          "text": "Location: " + location + "\n"
-                + "Temperature: " + temperature + "\n"
-                + "Condition: " + weatherCondition,
-          "image_url": icon_url,
-        }
-      ]
-    };
-
-    res.send( body );
+    parsed_url = url.format({
+      pathname: 'http://api.wunderground.com/api/' + apikey + '/forecast/q/' + req.body.text + format,
+    });
+
+    request( parsed_url, function( error, response, body ) {
+      // Basic response for the conditions data already obtained
+      var responseBody = {
+        "response_type": "in_channel",
+        "icon": icon_url,
+        "text": "Forecast",
+        "attachments": [
+          {
+            "text": "Location: " + location + "\n"
+                  + "Temperature: " + temperature + "\n"
+                  + "Condition: " + weatherCondition,
+            "image_url": icon_url,
+          }
+        ]
+      };
+
+      // If forecast wasn't available, return just the conditions
+      if ( error || 200 !== response.statusCode ) {
+        responseBody.attachments[1] = { "text":"Could not retrieve forecast" };
+
+        return res.send( responseBody );
+      }
+
+      // Build forecast into a table
+
+      res.send( responseBody );
+
+    });
   });
 });
 
-- 
GitLab