1 | <?php |
||
16 | class OpenWeatherMap implements WeatherProviderInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $apiUrl = 'http://api.openweathermap.org/data/2.5/weather'; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $appId = ""; |
||
27 | |||
28 | 2 | public function __construct(array $config = array()) |
|
29 | { |
||
30 | 2 | if (isset($config['appId'])) { |
|
31 | 1 | $this->appId = $config['appId']; |
|
32 | 1 | } |
|
33 | 2 | } |
|
34 | |||
35 | /** |
||
36 | * Return the url for the API request |
||
37 | * |
||
38 | * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
||
39 | * |
||
40 | * @return string |
||
41 | */ |
||
42 | 1 | public function getApiRequestUrl(Event $event) |
|
43 | { |
||
44 | 1 | $params = $event->getCustomParams(); |
|
45 | 1 | $query = trim(implode(" ", $params)); |
|
46 | |||
47 | $querystringParams = array( |
||
48 | 1 | 'q' => $query, |
|
49 | 1 | 'appid' => $this->appId |
|
50 | 1 | ); |
|
51 | |||
52 | 1 | return sprintf("%s?%s", $this->apiUrl, http_build_query($querystringParams)); |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * Validate the provided parameters |
||
57 | * The plugin requires at least one parameter (in most cases, this will be a location string) |
||
58 | * |
||
59 | * @param array $params |
||
60 | * |
||
61 | * @return boolean |
||
62 | */ |
||
63 | 1 | public function validateParams(array $params) |
|
67 | |||
68 | /** |
||
69 | * Returns an array of lines to send back to IRC when the http request is successful |
||
70 | * |
||
71 | * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
||
72 | * @param string $apiResponse |
||
73 | * |
||
74 | * @return array |
||
75 | */ |
||
76 | 1 | public function getSuccessLines(Event $event, $apiResponse) |
|
77 | { |
||
78 | 1 | $data = json_decode($apiResponse); |
|
79 | 1 | if (isset($data->name) && $data->name) { |
|
80 | return array( |
||
81 | 1 | sprintf( |
|
82 | 1 | "%s, %s | %s | Temp: %dC | Humidity: %s%% | Sunrise: %s | Sunset: %s", |
|
83 | 1 | $data->name, |
|
84 | 1 | $data->sys->country, |
|
85 | 1 | $data->weather[0]->main, |
|
86 | 1 | round($data->main->temp - 273.15), |
|
87 | 1 | $data->main->humidity, |
|
88 | 1 | date("H:i:s", $data->sys->sunrise), |
|
89 | 1 | date("H:i:s", $data->sys->sunset) |
|
90 | 1 | ) |
|
91 | 1 | ); |
|
92 | } else { |
||
93 | 1 | return $this->getNoResultsLines($event, $apiResponse); |
|
94 | } |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Return an array of lines to send back to IRC when there are no results |
||
99 | * |
||
100 | * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
||
101 | * @param string $apiResponse |
||
102 | * |
||
103 | * @return array |
||
104 | */ |
||
105 | 1 | public function getNoResultsLines(Event $event, $apiResponse) |
|
109 | |||
110 | /** |
||
111 | * Return an array of lines to send back to IRC when the request fails |
||
112 | * |
||
113 | * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event |
||
114 | * @param string $apiError |
||
115 | * |
||
116 | * @return array |
||
117 | */ |
||
118 | 1 | public function getRejectLines(Event $event, $apiError) |
|
122 | |||
123 | /** |
||
124 | * Returns an array of lines for the help response |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | 1 | public function getHelpLines() |
|
136 | } |
||
137 |