1 | <?php |
||
16 | class Wunderground implements WeatherProviderInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $apiUrl = 'http://api.wunderground.com/api'; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $appId = ""; |
||
27 | |||
28 | 1 | public function __construct(array $config = array()) |
|
29 | { |
||
30 | 1 | if (isset($config['appId'])) { |
|
31 | 1 | $this->appId = $config['appId']; |
|
32 | 1 | } |
|
33 | 1 | } |
|
34 | |||
35 | /** |
||
36 | * Return the url for the API request |
||
37 | * |
||
38 | * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
||
39 | * @return string |
||
40 | */ |
||
41 | 1 | public function getApiRequestUrl(Event $event) |
|
42 | { |
||
43 | 1 | $params = $event->getCustomParams(); |
|
44 | |||
45 | // Final parameter should be the country |
||
46 | 1 | $country = $params[count($params) - 1]; |
|
47 | // Remove the final paramater |
||
48 | 1 | unset($params[count($params) - 1]); |
|
49 | // Merge the remainder of the supplied params and remove disallowed punctuation |
||
50 | 1 | $place = trim(implode("_", preg_replace('/[^\da-z\ ]/i', '', $params))); |
|
51 | 1 | return sprintf("%s/%s/conditions/q/%s/%s.json", $this->apiUrl, $this->appId, $country, $place); |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * Validate the provided parameters |
||
56 | * The plugin requires at least one parameter (in most cases, this will be a location string) |
||
57 | * |
||
58 | * @param array $params |
||
59 | * @return boolean |
||
60 | */ |
||
61 | 1 | public function validateParams(array $params) |
|
65 | |||
66 | /** |
||
67 | * Returns an array of lines to send back to IRC when the http request is successful |
||
68 | * |
||
69 | * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
||
70 | * @param string $apiResponse |
||
71 | * @return array |
||
72 | */ |
||
73 | 1 | public function getSuccessLines(Event $event, $apiResponse) |
|
74 | { |
||
75 | 1 | $data = json_decode($apiResponse); |
|
76 | 1 | if (isset($data->current_observation)) { |
|
77 | 1 | $data = $data->current_observation; |
|
78 | return array( |
||
79 | 1 | sprintf( |
|
80 | 1 | "%s | %s | Temp: %dC | Humidity: %s | %s", |
|
81 | 1 | $data->display_location->full, |
|
82 | 1 | $data->weather, |
|
83 | 1 | round($data->temp_c), |
|
84 | 1 | $data->relative_humidity, |
|
85 | 1 | $data->forecast_url |
|
86 | 1 | ) |
|
87 | 1 | ); |
|
88 | } else { |
||
89 | 1 | return $this->getNoResultsLines($event, $apiResponse); |
|
90 | } |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Return an array of lines to send back to IRC when there are no results |
||
95 | * |
||
96 | * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
||
97 | * @param string $apiResponse |
||
98 | * @return array |
||
99 | */ |
||
100 | 1 | public function getNoResultsLines(Event $event, $apiResponse) |
|
104 | |||
105 | /** |
||
106 | * Return an array of lines to send back to IRC when the request fails |
||
107 | * |
||
108 | * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
||
109 | * @param string $apiError |
||
110 | * @return array |
||
111 | */ |
||
112 | 1 | public function getRejectLines(Event $event, $apiError) |
|
116 | |||
117 | /** |
||
118 | * Returns an array of lines for the help response |
||
119 | * |
||
120 | * @return array |
||
121 | */ |
||
122 | 1 | public function getHelpLines() |
|
131 | } |
||
132 |