| Weather-Bug documentation | Contained in the Weather-Bug distribution. |
Weather::Bug::Station - Simple class interface to the WeatherBug station data
This document describes Weather::Bug::Station version 0.25
use Weather::Bug;
my $wbug = Weather::Bug->new( 'YOURAPIKEYHERE' );
my @stations = $wbug->list_stations( 77096 );
for my $s (@stations)
{
my $l = $s->location();
printf( "%s: %s\n\tin %s, %s %s\n\t(%0.5f,%0.5f), (dist=%0.3f, type=%s)\n",
$s->id(), $s->name(),
$l->city(), $l->state(), $l->zipcode(),
$l->latitude(), $l->longitude(),
$l->distance(), $l->station_type()
);
}
The Station class wraps the concept of a WeatherBug station. A Station object provides some information about its location and identity. Since actual weather observations are made at particular stations, this object also supplies the methods needed to request the current weather.
The Station interface can be divided into three groups of methods: factory methods, accessor methods, and request methods.
Since the Station object will almost always be created from an XML stream, this class provides a set of methods for constructing a Station object from the XML responses.
Extract the station information from an aws:station node, such as the ones returned by the getStations WeatherBug request.
the Weather::Bug object
the aws:station XML node
Return a new Weather::Bug::Station object on success.
Extract the station information from an aws:weather node, such as the
ones returned by the getLiveCompactWeather WeatherBug request.
the Weather::Bug object
the aws:weather XML node
Return a new Weather::Bug::Station object on success.
Extract the station information from an aws:obs node, such as the
ones returned by the getLiveWeather WeatherBug request.
the Weather::Bug object
the aws:obs XML node
Return a new Weather::Bug::Station object on success.
The Station object provides accessor methods for the following fields:
This is the short ID string that can be used to refer to a particular WeatherBug station.
This is a longer printable name for the WeatherBug station.
This is a Weather::Bug::Location object that describes the location of the station.
This is a string telling what kind of station this is. This value appears to be either WeatherBug or NWS (National Weather Service).
An optional URL associated with the Station returned as a string.
A boolean method that returns a true value if the Station has an associated URL.
Returns true if all of the required elements of the Station match those of the other Station and if both Stations have a URL, they match.
Perform a request on this Station to get the live weather. A Weather::Bug::Weather object representing the current weather is returned.
Perform a request on this Station to get the compact version of the live weather. A Weather::Bug::CompactWeather object representing the current weather is returned.
Request for '%s' failed.The request to the WeatherBug API was not successful. The %s is the
name of the page that failed.
Unable to parse output from '%s' request.Although the WeatherBug API did return successfully, the output could not
be parsed as well-formed XML. The %s is the name of the page that failed.
No '%s' node found.Although the WeatherBug API did return successfully, the output XML did not contain the specified node.
Weather::Bug requires no configuration files or environment variables.
Moose, XML::LibXML.
None reported.
No bugs have been reported.
Please report any bugs or feature requests to
bug-weather-weatherbug@rt.cpan.org, or through the web interface at
http://rt.cpan.org.
G. Wade Johnson <wade@anomaly.org>
Copyright (c) 2008, G. Wade Johnson <wade@anomaly.org>. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
| Weather-Bug documentation | Contained in the Weather-Bug distribution. |
package Weather::Bug::Station; use warnings; use strict; use Moose; use XML::LibXML; use Weather::Bug; use Weather::Bug::Location; use Weather::Bug::Temperature; use Weather::Bug::Quantity; use Weather::Bug::CompactWeather; use Weather::Bug::Weather; our $VERSION = '0.25'; has 'id' => ( is => 'ro', isa => 'Str', init_arg => '-id' ); has 'name' => ( is => 'ro', isa => 'Str', init_arg => '-name' ); has 'url' => ( is => 'ro', isa => 'Str', init_arg => '-url', predicate => 'has_url' ); has 'location' => ( is => 'ro', isa => 'Weather::Bug::Location', init_arg => '-loc' ); has 'station_type' => ( is => 'ro', isa => 'Str', init_arg => '-type' ); has 'bug' => ( is => 'ro', isa => 'Weather::Bug', init_arg => '-bug' ); sub from_xml { my $class = shift; my $bug = shift; my $statnode = shift; return Weather::Bug::Station->new( -id => $statnode->findvalue( '@id' ), -name => $statnode->findvalue( '@name' ), -loc => Weather::Bug::Location->from_station_xml( $statnode ), -type => $statnode->findvalue( '@station-type' ), -bug => $bug, ); } sub from_compact_xml { my $class = shift; my $bug = shift; my $statnode = shift; return Weather::Bug::Station->new( -id => $statnode->findvalue( '@id' ), -name => $statnode->findvalue( '@name' ), -loc => Weather::Bug::Location->from_compact_station_xml( $statnode ), -type => $statnode->findvalue( '@station-type' ), -bug => $bug, ); } sub from_obs_xml { my $class = shift; my $bug = shift; my $node = shift; return Weather::Bug::Station->new( -id => $node->findvalue( 'aws:station-id' ), -name => $node->findvalue( 'aws:station' ), -loc => Weather::Bug::Location->from_obs_xml( ($node->findnodes( 'aws:city-state' ))[0] ), -url => $node->findvalue( 'aws:site-url' ), -bug => $bug, ); } # # Utility method that simplifies calling the WeatherBug::request method for # the API requests associated with the Station object. # # $cmd - command name # # Returns the XML response as a string if successful, or C<undef> on failure. sub _request { my $self = shift; my $cmd = shift; return $self->{bug}->request( $cmd, { zipcode => $self->location()->zipcode, StationId => $self->id } ); } sub is_equivalent { my $self = shift; my $other = shift; return unless $other->isa( __PACKAGE__ ); return unless $self->id() eq $other->id(); return unless $self->name() eq $other->name(); return unless $self->station_type() eq $other->tation_type(); return if $self->has_url() and $other->has_url and $self->url() ne $other->url(); return $self->location()->is_equivalent( $other->location() ); } sub get_live_weather { my $self = shift; my $response = $self->_request( 'getLiveWeather' ); my $p = XML::LibXML->new(); my $doc = $p->parse_string( $response ); my ($ob) = $doc->findnodes( '//aws:ob' ); return Weather::Bug::Weather->from_xml( $ob, $self ); } sub get_live_compact_weather { my $self = shift; my $response = $self->_request( 'getLiveCompactWeather' ); my $p = XML::LibXML->new(); my $doc = $p->parse_string( $response ); my ($w) = $doc->findnodes( '//aws:weather' ); return Weather::Bug::CompactWeather->from_xml( $w, $self ); } 1; # Magic true value required at end of module __END__