Add a ensure() method that can check to see that any requirements a driver
authorMichael J. Rubinsky <mrubinsk@horde.org>
Sun, 7 Jun 2009 20:00:44 +0000 (16:00 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Sun, 7 Jun 2009 20:00:44 +0000 (16:00 -0400)
may have are met.

timeobjects/lib/Driver.php
timeobjects/lib/Driver/Weatherdotcom.php
timeobjects/lib/api.php

index b59768b..d62f257 100644 (file)
@@ -17,10 +17,22 @@ class TimeObjects_Driver
      * @param $end
      * @return unknown_type
      */
-    public function listTimeObjects($start, $end)
-    {
-    }
+    public function listTimeObjects($start, $end){}
 
+    /**
+     * Ensure we have minimum requirements for concrete driver to run.
+     *
+     * @abstract
+     */
+    function ensure(){}
+
+    /**
+     * Factory method
+     *
+     * @param $name
+     * @param $params
+     * @return unknown_type
+     */
     public function factory($name, $params = array())
     {
         $class = 'TimeObjects_Driver_' . basename($name);
index 4937857..10cc37f 100644 (file)
@@ -27,12 +27,32 @@ class TimeObjects_Driver_Weatherdotcom extends TimeObjects_Driver
             $params['location'] = $contact['homeCity'] . (!empty($contact['homeProvince']) ? ', ' . $contact['homeProvince'] : '') . (!empty($contact['homeCountry']) ? ', ' . $contact['homeCountry'] : '');
         }
 
-        // TODO: Try some other way?
+        // TODO: Try some other way, maybe a hook or a new preference in Horde
+        //       to set your current location, maybe with a google map?
 
         parent::__construct($params);
     }
 
     /**
+     * Ensures that we meet all requirements to use this time object
+     *
+     * @return boolean
+     */
+    public function ensure()
+    {
+        if (!class_exists('Services_Weather') ||
+            !class_exists('Cache') ||
+            empty($this->_params['location']) ||
+            empty($conf['weatherdotcom']['partner_id']) ||
+            empty($conf['weatherdotcom']['license_key'])) {
+
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
      *
      * @param mixed $start  The start time of the period
      * @param mixed $time   The end time of the period
@@ -86,10 +106,6 @@ class TimeObjects_Driver_Weatherdotcom extends TimeObjects_Driver
 
         // If the user entered a zip code for the location, no need to
         // search (weather.com accepts zip codes as location IDs).
-        // The location ID should already have been validated in
-        // getParams.
-        //
-        // @TODO: These need to all be changed to use exceptions
         $search = (preg_match('/\b(?:\\d{5}(-\\d{5})?)|(?:[A-Z]{4}\\d{4})\b/',
             $this->_params['location'], $matches) ?
             $matches[0] :
@@ -142,9 +158,9 @@ class TimeObjects_Driver_Weatherdotcom extends TimeObjects_Driver
             // simplify and just check for it's presence or use night.
             $title = sprintf("%s %d%s/%d%s", (!empty($data['day']['condition']) ? $data['day']['condition'] : $data['night']['condition']),
                                              $data['temperatureHigh'],
-                                             $units['temp'],
+                                             String::upper($units['temp']),
                                              $data['temperatureLow'],
-                                             $units['temp']);
+                                             String::upper($units['temp']));
             $objects[] = array('id' => $day->timestamp(), //???
                                'title' => $title,
                                'start' => sprintf('%d-%02d-%02dT00:00:00',
index c335ee6..22e24a7 100644 (file)
@@ -27,7 +27,15 @@ $_services['show'] = array(
  */
 function _timeobjects_listTimeObjectCategories()
 {
-    return array('Weatherdotcom' => _("Weather"));
+    // @TODO: Probably want to iterate the driver directory
+    //        and dynamically build this list and/or maybe provide
+    //        a $conf[] setting to explicitly disable certain drivers?
+    $drv = TimeObjects_Driver::factory('Weatherdotcom');
+    if ($drv->ensure()) {
+        return array('Weatherdotcom' => _("Weather"));
+    } else {
+        return array();
+    }
 }
 
 /**