POEx::Weather::OpenWeatherMap - POE-enabled OpenWeatherMap client
use POE; use POEx::Weather::OpenWeatherMap; # An API key can be obtained (free) at http://www.openweathermap.org: my $api_key = 'foo'; POE::Session->create( package_states => [ main => [qw/ _start pwx_error pwx_weather pwx_forecast /], ], ); sub _start { my ($kernel, $heap) = @_[KERNEL, HEAP]; # Create, store, and start emitter: my $wx = POEx::Weather::OpenWeatherMap->new( api_key => $api_key, event_prefix => 'pwx_', ); $heap->{wx} = $wx; $wx->start; ## An example request: $wx->get_weather( location => 'Manchester, NH', tag => 'mytag', ); } sub pwx_error { my $err = $_[ARG0]; my $status = $err->status; my $request = $err->request; # ... do something with error ... warn "Error! ($status)"; } sub pwx_weather { my $result = $_[ARG0]; my $tag = $result->request->tag; my $place = $result->name; my $tempf = $result->temp_f; my $conditions = $result->conditions_verbose; # (see Weather::OpenWeatherMap::Result::Current for a method list) # ... } sub pwx_forecast { my $result = $_[ARG0]; my $place = $result->name; my $itr = $result->iter; while (my $day = $itr->()) { my $date = $day->dt->mdy; my $temp_hi = $day->temp_max_f; my $temp_lo = $day->temp_min_f; # (see Weather::OpenWeatherMap::Result::Forecast) # ... } } POE::Kernel->run;
A POE-enabled interface to OpenWeatherMap (http://www.openweathermap.org), providing an object-oriented asynchronous interface to current & forecast weather conditions for a given city, latitude/longitude, or OpenWeatherMap city code.
This is really just an asynchronous counterpart to Weather::OpenWeatherMap; look there for documentation regarding Request & Result objects.
This an event emitter that consumes MooX::Role::POE::Emitter; look there for documentation on composed methods. See http://www.openweathermap.org for more on OpenWeatherMap itself.
Your OpenWeatherMap API key.
(See http://www.openweathermap.org/api to register for free.)
api_key can be set after object construction via set_api_key; if the key is not valid, requests will likely fail with 401 Unauthorized errors.
api_key
401 Unauthorized
A boolean value indicating whether successful results should be cached to disk via Weather::OpenWeatherMap::Cache.
Defaults to false. This may change in a future release.
The directory in which cache files are saved. The default may be fine; see Weather::OpenWeatherMap::Cache.
The duration (in seconds) for which cache files are considered valid. The default may be fine; see Weather::OpenWeatherMap::Cache.
Start our session.
Must be called before events will be received or emitted.
Stop our session, shutting down the emitter and user agent (which will cancel pending requests).
$wx->get_weather( # 'location =>' is mandatory. # These are all valid location strings: # By name: # 'Manchester, NH' # 'London, UK' # By OpenWeatherMap city code: # 5089178 # By latitude/longitude: # 'lat 42, long -71' location => 'Manchester, NH', # Set 'forecast => 1' to get the forecast, # omit or set to false for current weather: forecast => 1, # If 'forecast' is true, you can specify the number of days to fetch # (up to 14): days => 3, # Optional tag for identifying the response to this request: tag => 'foo', );
Request a weather report for the given location =>.
location =>
The location can be a 'City, State' or 'City, Country' string, an OpenWeatherMap city code, or a 'lat X, long Y' string.
Requests the current weather by default (see Weather::OpenWeatherMap::Request::Current).
If passed forecast => 1, requests a weather forecast (see Weather::OpenWeatherMap::Request::Forecast), in which case days => $count can be specified (up to 14). If hourly => 1 is also specified, an hourly (rather than daily) report is returned; see the documentation for Weather::OpenWeatherMap for details.
forecast => 1
days => $count
hourly => 1
If passed find => 1, requests search results for a given location name or latitude & longitude; see Weather::OpenWeatherMap::Request::Find.
find => 1
An optional tag => can be specified to identify the response when it comes in.
tag =>
Any extra arguments are passed to the constructor for the appropriate Request subclass; see Weather::OpenWeatherMap::Request.
The request is made asynchronously and a response (or error) emitted when it is available; see "EMITTED EVENTS". There is no useful return value.
$poe_kernel->post( $wx->session_id => get_weather => location => 'Manchester, NH', tag => 'foo', );
POE interface to the "get_weather" method; see "METHODS" for available options.
Emitted when an error occurs; this may be an internal error, an HTTP error, or an error reported by the OpenWeatherMap API.
$_[ARG0] is a Weather::OpenWeatherMap::Error object.
$_[ARG0]
Emitted when a request for the current weather has been successfully processed.
$_[ARG0] is a Weather::OpenWeatherMap::Result::Current object; see that module's documentation for details on retrieving weather information.
Emitted when a request for a weather forecast has been successfully processed.
$_[ARG0] is a Weather::OpenWeatherMap::Result::Forecast object; see that module's documentation for details on retrieving daily or hourly forecasts (Weather::OpenWeatherMap::Result::Forecast::Day & Weather::OpenWeatherMap::Result::Forecast::Hour objects) from the retrieved forecast.
Weather::OpenWeatherMap
Weather::OpenWeatherMap::Error
Weather::OpenWeatherMap::Result
Weather::OpenWeatherMap::Result::Current
Weather::OpenWeatherMap::Result::Forecast
Weather::OpenWeatherMap::Request
Weather::OpenWeatherMap::Request::Current
Weather::OpenWeatherMap::Request::Forecast
The examples/ directory of this distribution.
examples/
Jon Portnoy <avenj@cobaltirc.org>
To install POEx::Weather::OpenWeatherMap, copy and paste the appropriate command in to your terminal.
cpanm
cpanm POEx::Weather::OpenWeatherMap
CPAN shell
perl -MCPAN -e shell install POEx::Weather::OpenWeatherMap
For more information on module installation, please visit the detailed CPAN module installation guide.