| 1 | package POEx::Weather::OpenWeatherMap; | ||
| 2 | $POEx::Weather::OpenWeatherMap::VERSION = '0.002002'; | ||
| 3 | 1 | ▶ | use Defaults::Modern; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 1 | ▶ | use POE 'Component::Client::HTTP'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | use Weather::OpenWeatherMap::Cache; | ||
| 8 | use Weather::OpenWeatherMap::Error; | ||
| 9 | |||
| 10 | use Weather::OpenWeatherMap::Request; | ||
| 11 | use Weather::OpenWeatherMap::Request::Current; | ||
| 12 | use Weather::OpenWeatherMap::Request::Forecast; | ||
| 13 | use Weather::OpenWeatherMap::Request::Find; | ||
| 14 | |||
| 15 | use Weather::OpenWeatherMap::Result; | ||
| 16 | use Weather::OpenWeatherMap::Result::Current; | ||
| 17 | use Weather::OpenWeatherMap::Result::Forecast; | ||
| 18 | use Weather::OpenWeatherMap::Request::Find; | ||
| 19 | |||
| 20 | |||
| 21 | use Moo; use MooX::late; | ||
| 22 | with 'MooX::Role::POE::Emitter'; | ||
| 23 | |||
| 24 | |||
| 25 | has api_key => ( | ||
| 26 | lazy => 1, | ||
| 27 | is => 'ro', | ||
| 28 | isa => Str, | ||
| 29 | predicate => 1, | ||
| 30 | builder => sub { '' }, | ||
| 31 | ); | ||
| 32 | |||
| 33 | has _ua_alias => ( | ||
| 34 | lazy => 1, | ||
| 35 | init_arg => 'ua_alias', | ||
| 36 | is => 'ro', | ||
| 37 | isa => Str, | ||
| 38 | builder => sub { | ||
| 39 | my ($self) = @_; | ||
| 40 | $self->alias ? $self->alias . 'UA' | ||
| 41 | : confess "Cannot build ua_alias; emitter not running" | ||
| 42 | }, | ||
| 43 | ); | ||
| 44 | |||
| 45 | |||
| 46 | has cache => ( | ||
| 47 | lazy => 1, | ||
| 48 | is => 'ro', | ||
| 49 | isa => Bool, | ||
| 50 | builder => sub { 0 }, | ||
| 51 | ); | ||
| 52 | |||
| 53 | has cache_dir => ( | ||
| 54 | lazy => 1, | ||
| 55 | is => 'ro', | ||
| 56 | isa => Maybe[Str], | ||
| 57 | builder => sub { undef }, | ||
| 58 | ); | ||
| 59 | |||
| 60 | has cache_expiry => ( | ||
| 61 | lazy => 1, | ||
| 62 | is => 'ro', | ||
| 63 | isa => Maybe[StrictNum], | ||
| 64 | builder => sub { undef }, | ||
| 65 | ); | ||
| 66 | |||
| 67 | has _cache => ( | ||
| 68 | lazy => 1, | ||
| 69 | is => 'ro', | ||
| 70 | isa => InstanceOf['Weather::OpenWeatherMap::Cache'], | ||
| 71 | builder => sub { | ||
| 72 | my ($self) = @_; | ||
| 73 | Weather::OpenWeatherMap::Cache->new( | ||
| 74 | maybe dir => $self->cache_dir, | ||
| 75 | maybe expiry => $self->cache_expiry, | ||
| 76 | ) | ||
| 77 | }, | ||
| 78 | ); | ||
| 79 | |||
| 80 | |||
| 81 | method start { | ||
| 82 | $self->set_object_states( | ||
| 83 | [ | ||
| 84 | $self => +{ | ||
| 85 | emitter_started => 'mxrp_emitter_started', | ||
| 86 | emitter_stopped => 'mxrp_emitter_stopped', | ||
| 87 | |||
| 88 | get_weather => 'ext_get_weather', | ||
| 89 | ext_http_response => 'ext_http_response', | ||
| 90 | }, | ||
| 91 | |||
| 92 | ( $self->has_object_states ? $self->object_states->all : () ), | ||
| 93 | ] | ||
| 94 | ); | ||
| 95 | $self->_start_emitter | ||
| 96 | } | ||
| 97 | |||
| 98 | method stop { $self->_shutdown_emitter } | ||
| 99 | |||
| 100 | method _emit_error (@params) { | ||
| 101 | my $err = Weather::OpenWeatherMap::Error->new(@params); | ||
| 102 | $self->emit( error => $err ); | ||
| 103 | $err | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | sub mxrp_emitter_started { | ||
| 108 | # my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub mxrp_emitter_stopped { | ||
| 112 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 113 | $kernel->call( $self->_ua_alias => 'shutdown' ) | ||
| 114 | if $kernel->alias_resolve( $self->_ua_alias ); | ||
| 115 | } | ||
| 116 | |||
| 117 | method _result_type (Object $my_request) { | ||
| 118 | state $base = 'Weather::OpenWeatherMap::Request::'; | ||
| 119 | my ($type, $event); | ||
| 120 | CLASS: { | ||
| 121 | if ($my_request->isa($base.'Current')) { | ||
| 122 | $type = 'Current'; | ||
| 123 | $event = 'weather'; | ||
| 124 | last CLASS | ||
| 125 | } | ||
| 126 | |||
| 127 | if ($my_request->isa($base.'Forecast')) { | ||
| 128 | $type = 'Forecast'; | ||
| 129 | $event = 'forecast'; | ||
| 130 | last CLASS | ||
| 131 | } | ||
| 132 | |||
| 133 | confess "Unknown request type: $my_request" | ||
| 134 | } | ||
| 135 | wantarray ? ($type, $event) : $type | ||
| 136 | } | ||
| 137 | |||
| 138 | method get_weather (@params) { $self->yield(get_weather => @params) } | ||
| 139 | |||
| 140 | sub ext_get_weather { | ||
| 141 | my $self = $_[OBJECT]; | ||
| 142 | |||
| 143 | my %args = @_[ARG0 .. $#_]; | ||
| 144 | |||
| 145 | my $location = $args{location}; | ||
| 146 | unless ($location) { | ||
| 147 | warn "Missing 'location =>' in query\n"; | ||
| 148 | my $fake_req = Weather::OpenWeatherMap::Request->new_for( | ||
| 149 | Current => | ||
| 150 | tag => $args{tag}, | ||
| 151 | location => '', | ||
| 152 | ); | ||
| 153 | $self->_emit_error( | ||
| 154 | source => 'internal', | ||
| 155 | request => $fake_req, | ||
| 156 | status => "Missing 'location =>' in query", | ||
| 157 | ); | ||
| 158 | return | ||
| 159 | } | ||
| 160 | |||
| 161 | my $type = | ||
| 162 | delete $args{forecast} ? 'Forecast' | ||
| 163 | : delete $args{find} ? 'Find' | ||
| 164 | : 'Current'; | ||
| 165 | |||
| 166 | my $my_request = Weather::OpenWeatherMap::Request->new_for( | ||
| 167 | $type => | ||
| 168 | ( | ||
| 169 | $self->has_api_key && length $self->api_key ? | ||
| 170 | (api_key => $self->api_key) : () | ||
| 171 | ), | ||
| 172 | %args | ||
| 173 | ); | ||
| 174 | |||
| 175 | if ($self->cache && (my $cached = $self->_cache->retrieve($my_request)) ) { | ||
| 176 | my $obj = $cached->object; | ||
| 177 | my (undef, $event) = $self->_result_type($my_request); | ||
| 178 | $self->emit( $event => $obj ); | ||
| 179 | return $obj | ||
| 180 | } | ||
| 181 | |||
| 182 | $self->_issue_http_request($my_request); | ||
| 183 | () | ||
| 184 | } | ||
| 185 | |||
| 186 | |||
| 187 | method _issue_http_request (Object $my_request) { | ||
| 188 | unless ( $poe_kernel->alias_resolve($self->_ua_alias) ) { | ||
| 189 | POE::Component::Client::HTTP->spawn( | ||
| 190 | Alias => $self->_ua_alias, | ||
| 191 | FollowRedirects => 2, | ||
| 192 | ) | ||
| 193 | } | ||
| 194 | |||
| 195 | $poe_kernel->post( $self->_ua_alias => request => ext_http_response => | ||
| 196 | $my_request->http_request, | ||
| 197 | $my_request | ||
| 198 | ); | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | sub ext_http_response { | ||
| 203 | my $self = $_[OBJECT]; | ||
| 204 | |||
| 205 | my (undef, $my_request) = @{ $_[ARG0] }; | ||
| 206 | my ($http_response) = @{ $_[ARG1] }; | ||
| 207 | |||
| 208 | unless ($http_response->is_success) { | ||
| 209 | $self->_emit_error( | ||
| 210 | source => 'http', | ||
| 211 | request => $my_request, | ||
| 212 | status => $http_response->status_line, | ||
| 213 | ); | ||
| 214 | return | ||
| 215 | } | ||
| 216 | |||
| 217 | my ($type, $event) = $self->_result_type($my_request); | ||
| 218 | |||
| 219 | my $content = $http_response->content; | ||
| 220 | my $my_response = Weather::OpenWeatherMap::Result->new_for( | ||
| 221 | $type => | ||
| 222 | request => $my_request, | ||
| 223 | json => $content, | ||
| 224 | ); | ||
| 225 | |||
| 226 | unless ($my_response->is_success) { | ||
| 227 | my $code = $my_response->response_code; | ||
| 228 | $self->_emit_error( | ||
| 229 | source => 'api', | ||
| 230 | request => $my_request, | ||
| 231 | status => "$code: ".$my_response->error, | ||
| 232 | ); | ||
| 233 | return | ||
| 234 | } | ||
| 235 | |||
| 236 | $self->_cache->cache($my_response) if $self->cache; | ||
| 237 | |||
| 238 | $self->emit( | ||
| 239 | $event => $my_response | ||
| 240 | ); | ||
| 241 | } | ||
| 242 | |||
| 243 | 1; | ||
| 244 | |||
| 245 | |||
| 246 | =pod | ||
| 247 | |||
| 248 | =for Pod::Coverage ext_\w+ mxrp_\w+ has_\w+ | ||
| 249 | |||
| 250 | =head1 NAME | ||
| 251 | |||
| 252 | POEx::Weather::OpenWeatherMap - POE-enabled OpenWeatherMap client | ||
| 253 | |||
| 254 | =head1 SYNOPSIS | ||
| 255 | |||
| 256 | use POE; | ||
| 257 | use POEx::Weather::OpenWeatherMap; | ||
| 258 | |||
| 259 | # An API key can be obtained (free) at http://www.openweathermap.org: | ||
| 260 | my $api_key = 'foo'; | ||
| 261 | |||
| 262 | POE::Session->create( | ||
| 263 | package_states => [ | ||
| 264 | main => [qw/ | ||
| 265 | _start | ||
| 266 | |||
| 267 | pwx_error | ||
| 268 | pwx_weather | ||
| 269 | pwx_forecast | ||
| 270 | /], | ||
| 271 | ], | ||
| 272 | ); | ||
| 273 | |||
| 274 | sub _start { | ||
| 275 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 276 | |||
| 277 | # Create, store, and start emitter: | ||
| 278 | my $wx = POEx::Weather::OpenWeatherMap->new( | ||
| 279 | api_key => $api_key, | ||
| 280 | event_prefix => 'pwx_', | ||
| 281 | ); | ||
| 282 | |||
| 283 | $heap->{wx} = $wx; | ||
| 284 | $wx->start; | ||
| 285 | |||
| 286 | ## An example request: | ||
| 287 | $wx->get_weather( | ||
| 288 | location => 'Manchester, NH', | ||
| 289 | tag => 'mytag', | ||
| 290 | ); | ||
| 291 | } | ||
| 292 | |||
| 293 | sub pwx_error { | ||
| 294 | my $err = $_[ARG0]; | ||
| 295 | my $status = $err->status; | ||
| 296 | my $request = $err->request; | ||
| 297 | # ... do something with error ... | ||
| 298 | warn "Error! ($status)"; | ||
| 299 | } | ||
| 300 | |||
| 301 | sub pwx_weather { | ||
| 302 | my $result = $_[ARG0]; | ||
| 303 | |||
| 304 | my $tag = $result->request->tag; | ||
| 305 | |||
| 306 | my $place = $result->name; | ||
| 307 | |||
| 308 | my $tempf = $result->temp_f; | ||
| 309 | my $conditions = $result->conditions_verbose; | ||
| 310 | # (see Weather::OpenWeatherMap::Result::Current for a method list) | ||
| 311 | # ... | ||
| 312 | } | ||
| 313 | |||
| 314 | sub pwx_forecast { | ||
| 315 | my $result = $_[ARG0]; | ||
| 316 | |||
| 317 | my $place = $result->name; | ||
| 318 | |||
| 319 | my $itr = $result->iter; | ||
| 320 | while (my $day = $itr->()) { | ||
| 321 | my $date = $day->dt->mdy; | ||
| 322 | my $temp_hi = $day->temp_max_f; | ||
| 323 | my $temp_lo = $day->temp_min_f; | ||
| 324 | # (see Weather::OpenWeatherMap::Result::Forecast) | ||
| 325 | # ... | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | POE::Kernel->run; | ||
| 330 | |||
| 331 | =head1 DESCRIPTION | ||
| 332 | |||
| 333 | A POE-enabled interface to OpenWeatherMap (L<http://www.openweathermap.org>), | ||
| 334 | providing an object-oriented asynchronous interface to current & forecast | ||
| 335 | weather conditions for a given city, latitude/longitude, or OpenWeatherMap | ||
| 336 | city code. | ||
| 337 | |||
| 338 | This is really just an asynchronous counterpart to L<Weather::OpenWeatherMap>; | ||
| 339 | look there for documentation regarding Request & Result objects. | ||
| 340 | |||
| 341 | This an event emitter that consumes L<MooX::Role::POE::Emitter>; look there | ||
| 342 | for documentation on composed methods. See L<http://www.openweathermap.org> | ||
| 343 | for more on OpenWeatherMap itself. | ||
| 344 | |||
| 345 | =head2 ATTRIBUTES | ||
| 346 | |||
| 347 | =head3 api_key | ||
| 348 | |||
| 349 | Your L<OpenWeatherMap|http://www.openweathermap.org> API key. | ||
| 350 | |||
| 351 | (See L<http://www.openweathermap.org/api> to register for free.) | ||
| 352 | |||
| 353 | =head3 cache | ||
| 354 | |||
| 355 | A boolean value indicating whether successful results should be cached to disk | ||
| 356 | via L<Weather::OpenWeatherMap::Cache>. | ||
| 357 | |||
| 358 | Defaults to false. This may change in a future release. | ||
| 359 | |||
| 360 | =head3 cache_dir | ||
| 361 | |||
| 362 | The directory in which cache files are saved. The default may be fine; see | ||
| 363 | L<Weather::OpenWeatherMap::Cache>. | ||
| 364 | |||
| 365 | =head3 cache_expiry | ||
| 366 | |||
| 367 | The duration (in seconds) for which cache files are considered valid. The | ||
| 368 | default may be fine; see L<Weather::OpenWeatherMap::Cache>. | ||
| 369 | |||
| 370 | =head2 METHODS | ||
| 371 | |||
| 372 | =head3 start | ||
| 373 | |||
| 374 | Start our session. | ||
| 375 | |||
| 376 | Must be called before events will be received or emitted. | ||
| 377 | |||
| 378 | =head3 stop | ||
| 379 | |||
| 380 | Stop our session, shutting down the emitter and user agent (which will cancel | ||
| 381 | pending requests). | ||
| 382 | |||
| 383 | =head3 get_weather | ||
| 384 | |||
| 385 | $wx->get_weather( | ||
| 386 | # 'location =>' is mandatory. | ||
| 387 | # These are all valid location strings: | ||
| 388 | # By name: | ||
| 389 | # 'Manchester, NH' | ||
| 390 | # 'London, UK' | ||
| 391 | # By OpenWeatherMap city code: | ||
| 392 | # 5089178 | ||
| 393 | # By latitude/longitude: | ||
| 394 | # 'lat 42, long -71' | ||
| 395 | location => 'Manchester, NH', | ||
| 396 | |||
| 397 | # Set 'forecast => 1' to get the forecast, | ||
| 398 | # omit or set to false for current weather: | ||
| 399 | forecast => 1, | ||
| 400 | |||
| 401 | # If 'forecast' is true, you can specify the number of days to fetch | ||
| 402 | # (up to 14): | ||
| 403 | days => 3, | ||
| 404 | |||
| 405 | # Optional tag for identifying the response to this request: | ||
| 406 | tag => 'foo', | ||
| 407 | ); | ||
| 408 | |||
| 409 | Request a weather report for the given C<< location => >>. | ||
| 410 | |||
| 411 | The location can be a 'City, State' or 'City, Country' string, an | ||
| 412 | L<OpenWeatherMap|http://www.openweathermap.org/> city code, or a 'lat X, long | ||
| 413 | Y' string. | ||
| 414 | |||
| 415 | Requests the current weather by default (see | ||
| 416 | L<Weather::OpenWeatherMap::Request::Current>). | ||
| 417 | |||
| 418 | If passed C<< forecast => 1 >>, requests a weather forecast (see | ||
| 419 | L<Weather::OpenWeatherMap::Request::Forecast>), in which case C<< days | ||
| 420 | => $count >> can be specified (up to 14). If C<< hourly => 1 >> is also | ||
| 421 | specified, an hourly (rather than daily) report is returned; see | ||
| 422 | the documentation for L<Weather::OpenWeatherMap> for details. | ||
| 423 | |||
| 424 | If passed C<< find => 1 >>, requests search results for a given location name | ||
| 425 | or latitude & longitude; see L<Weather::OpenWeatherMap::Request::Find>. | ||
| 426 | |||
| 427 | An optional C<< tag => >> can be specified to identify the response when it | ||
| 428 | comes in. | ||
| 429 | |||
| 430 | Any extra arguments are passed to the constructor for the appropriate Request | ||
| 431 | subclass; see L<Weather::OpenWeatherMap::Request>. | ||
| 432 | |||
| 433 | The request is made asynchronously and a response (or error) emitted when it | ||
| 434 | is available; see L</EMITTED EVENTS>. There is no useful return value. | ||
| 435 | |||
| 436 | =head2 RECEIVED EVENTS | ||
| 437 | |||
| 438 | =head3 get_weather | ||
| 439 | |||
| 440 | $poe_kernel->post( $wx->session_id => | ||
| 441 | get_weather => | ||
| 442 | location => 'Manchester, NH', | ||
| 443 | tag => 'foo', | ||
| 444 | ); | ||
| 445 | |||
| 446 | POE interface to the L</get_weather> method; see L</METHODS> for available | ||
| 447 | options. | ||
| 448 | |||
| 449 | =head2 EMITTED EVENTS | ||
| 450 | |||
| 451 | =head3 error | ||
| 452 | |||
| 453 | Emitted when an error occurs; this may be an internal error, an HTTP error, | ||
| 454 | or an error reported by the OpenWeatherMap API. | ||
| 455 | |||
| 456 | C<$_[ARG0]> is a L<Weather::OpenWeatherMap::Error> object. | ||
| 457 | |||
| 458 | =head3 weather | ||
| 459 | |||
| 460 | Emitted when a request for the current weather has been successfully processed. | ||
| 461 | |||
| 462 | C<$_[ARG0]> is a L<Weather::OpenWeatherMap::Result::Current> object; see | ||
| 463 | that module's documentation for details on retrieving weather information. | ||
| 464 | |||
| 465 | =head3 forecast | ||
| 466 | |||
| 467 | Emitted when a request for a weather forecast has been successfully processed. | ||
| 468 | |||
| 469 | C<$_[ARG0]> is a L<Weather::OpenWeatherMap::Result::Forecast> object; | ||
| 470 | see that module's documentation for details on retrieving daily or hourly forecasts | ||
| 471 | (L<Weather::OpenWeatherMap::Result::Forecast::Day> & | ||
| 472 | L<Weather::OpenWeatherMap::Result::Forecast::Hour> objects) from the retrieved | ||
| 473 | forecast. | ||
| 474 | |||
| 475 | =head1 SEE ALSO | ||
| 476 | |||
| 477 | L<Weather::OpenWeatherMap> | ||
| 478 | |||
| 479 | L<Weather::OpenWeatherMap::Error> | ||
| 480 | |||
| 481 | L<Weather::OpenWeatherMap::Result> | ||
| 482 | |||
| 483 | L<Weather::OpenWeatherMap::Result::Current> | ||
| 484 | |||
| 485 | L<Weather::OpenWeatherMap::Result::Forecast> | ||
| 486 | |||
| 487 | L<Weather::OpenWeatherMap::Request> | ||
| 488 | |||
| 489 | L<Weather::OpenWeatherMap::Request::Current> | ||
| 490 | |||
| 491 | L<Weather::OpenWeatherMap::Request::Forecast> | ||
| 492 | |||
| 493 | The C<examples/> directory of this distribution. | ||
| 494 | |||
| 495 | =head1 AUTHOR | ||
| 496 | |||
| 497 | Jon Portnoy <avenj@cobaltirc.org> | ||
| 498 | |||
| 499 | =cut | ||
| 500 | |||
| 501 | # vim: ts=2 sw=2 et sts=2 ft=perl | ||