From c7781a9bc5b63c510ef20546da493f09669c38f0 Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Sun, 7 Jun 2009 16:00:44 -0400 Subject: [PATCH] Add a ensure() method that can check to see that any requirements a driver may have are met. --- timeobjects/lib/Driver.php | 18 +++++++++++++++--- timeobjects/lib/Driver/Weatherdotcom.php | 30 +++++++++++++++++++++++------- timeobjects/lib/api.php | 10 +++++++++- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/timeobjects/lib/Driver.php b/timeobjects/lib/Driver.php index b59768b8c..d62f257ae 100644 --- a/timeobjects/lib/Driver.php +++ b/timeobjects/lib/Driver.php @@ -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); diff --git a/timeobjects/lib/Driver/Weatherdotcom.php b/timeobjects/lib/Driver/Weatherdotcom.php index 49378579f..10cc37f50 100644 --- a/timeobjects/lib/Driver/Weatherdotcom.php +++ b/timeobjects/lib/Driver/Weatherdotcom.php @@ -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', diff --git a/timeobjects/lib/api.php b/timeobjects/lib/api.php index c335ee633..22e24a73a 100644 --- a/timeobjects/lib/api.php +++ b/timeobjects/lib/api.php @@ -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(); + } } /** -- 2.11.0