Browse Source

update WeatherLib documentation

euromark 12 years ago
parent
commit
56a0ec962c
1 changed files with 31 additions and 27 deletions
  1. 31 27
      Lib/WeatherLib.php

+ 31 - 27
Lib/WeatherLib.php

@@ -16,10 +16,10 @@ App::uses('HttpSocket', 'Network/Http');
  * WeatherLib to retreive the current weather + forecast
  * WeatherLib to retreive the current weather + forecast
  *
  *
  * You can use Configure::write('Weather', ...) to adjust settings for it globabally via configs:
  * You can use Configure::write('Weather', ...) to adjust settings for it globabally via configs:
- * - key (required)
+ * - key (recommended)
  * - free (true/false)
  * - free (true/false)
- * - format
- * - num_of_days
+ * - format (json, csv, xml)
+ * - num_of_days (defaults to 5)
  *
  *
  * @author Mark Scherer
  * @author Mark Scherer
  * @license MIT
  * @license MIT
@@ -33,11 +33,11 @@ class WeatherLib {
 	const API_URL_FREE = 'http://free.worldweatheronline.com/feed/';
 	const API_URL_FREE = 'http://free.worldweatheronline.com/feed/';
 
 
 	public $settings = array(
 	public $settings = array(
-		'format' => 'xml', # json, csv, xml
+		'format' => 'xml',
 		'num_of_days' => 5,
 		'num_of_days' => 5,
-		'q' => '', # ; 48.00,11.00
+		'q' => '', # e.g. 48.00,11.00
 		'key' => '',
 		'key' => '',
-		'free' => true, # true/false
+		'free' => true,
 	);
 	);
 
 
 	public function __construct() {
 	public function __construct() {
@@ -45,6 +45,9 @@ class WeatherLib {
 	}
 	}
 
 
 	/**
 	/**
+	 * Get weather data - cached.
+	 * Provide cache=>false to prevent/clear the cache
+	 *
 	 * @return array Data or false on failure
 	 * @return array Data or false on failure
 	 */
 	 */
 	public function get($q, $options = array()) {
 	public function get($q, $options = array()) {
@@ -58,6 +61,9 @@ class WeatherLib {
 	}
 	}
 
 
 	/**
 	/**
+	 * Get weather conditions - cached.
+	 * Provide cache=>false to prevent/clear the cache
+	 *
 	 * @return array
 	 * @return array
 	 */
 	 */
 	public function conditions() {
 	public function conditions() {
@@ -68,9 +74,9 @@ class WeatherLib {
 		}
 		}
 		$conditions = $this->_get('wwoConditionCodes.xml', $options);
 		$conditions = $this->_get('wwoConditionCodes.xml', $options);
 		if (empty($conditions) || empty($conditions['codes']['condition'])) {
 		if (empty($conditions) || empty($conditions['codes']['condition'])) {
- 			return array();
- 		}
- 		return $conditions['codes']['condition'];
+			return array();
+		}
+		return $conditions['codes']['condition'];
 	}
 	}
 
 
 	//.../feed/weather.ashx?q=Neufahrn&format=json&num_of_days=2&key=598dfbdaeb121715111208
 	//.../feed/weather.ashx?q=Neufahrn&format=json&num_of_days=2&key=598dfbdaeb121715111208
@@ -95,30 +101,28 @@ class WeatherLib {
 		if (empty($content)) {
 		if (empty($content)) {
 			return false;
 			return false;
 		}
 		}
- 		switch ($options['format']) {
- 			case 'json':
- 				$res = json_decode($content);
- 				break;
- 			case 'xml':
-				// now parse it
-				//debug($file);
-				$parsed_xml = Xml::build($content);
-				//debug($parsed_xml);
-				$res = Xml::toArray($parsed_xml);
-				//debug($res);
- 				break;
+		switch ($options['format']) {
+			case 'json':
+				$res = json_decode($content);
+				break;
+			case 'xml':
+				$res = Xml::build($content);
+				$res = Xml::toArray($res);
+				break;
 			case 'csv':
 			case 'csv':
+				//TODO?
 				$res = array();
 				$res = array();
- 		}
+				throw new CakeException('Not implemented yet');
+		}
 
 
- 		if (!empty($cache)) {
+		if (!empty($cache)) {
 			Cache::write(md5($url), $res, 'data');
 			Cache::write(md5($url), $res, 'data');
 		}
 		}
 
 
- 		if (empty($res)) {
- 			return false;
- 		}
- 		return $res;
+		if (empty($res)) {
+			return false;
+		}
+		return $res;
 	}
 	}
 
 
 	/**
 	/**