Weather::Com::Forecast - class representing all available weather


Weather-Com documentation Contained in the Weather-Com distribution.

Index


Code Index:

NAME

Top

Weather::Com::Forecast - class representing all available weather forecasts for one location

SYNOPSIS

Top

  [...]

  my @locations = $weather_finder->find('Heidelberg');

  my $forecast = $locations[0]->forecast();
  my $tomorrow = $forecast->day(1);

  print "Forecast for tomorrow:\n";
  print " - tomorrow it's the ", $tomorrow->date()->date(), "\n";  
  print " - sunrise will be at ", $tomorrow->sunrise()->time(), "\n";  
  print " - maximum temperature will be ", $tomorrow->high(), "\n";  

DESCRIPTION

Top

Using Weather::Com::Forecast objects is the way to access weather forecast information for one specific location (city) and 0 (today) to 9 days in the future.

Each time you call the Weather::Com::Location objects' forecast() method, you'll get an updated Weather::Com::Forecast object. This object is used to access the 10 Weather::Com::DayForecast objects containing the actual data.

CONSTRUCTOR

Top

new(hash or hashref)

You usually would not construct an object of this class yourself. This is implicitely done when you call the forecast() method of a Weather::Com::Location object.

METHODS

Top

all()

Returns an arrayref of all Weather::Com::DayForecast objects if called in scalar context, an array if called in list context.

day(day number)

Returns the Weather::Com::DayForecast object that corresponds to the day number you provided.

The day number can be any number between 0 and 9.

Day 0 is usually today. Due to a bug (I think it is one) in the weather.com XOAP API, you may get the full forecast data of yesterday if you call for day 0 just after midnight. I think this may have do something with the timezone. I have not fully investigated this issue, yet. Please contact me, if you have!

SEE ALSO

Top

See also documentation of Weather::Com, Weather::Com::Location, Weather::Com::DayForecast.

AUTHOR

Top

Thomas Schnuecker, <thomas@schnuecker.de>

COPYRIGHT AND LICENSE

Top


Weather-Com documentation Contained in the Weather-Com distribution.

package Weather::Com::Forecast;

use 5.006;
use strict;
use Carp;
use Data::Dumper;
use Weather::Com::DayForecast;
use base "Weather::Com::Cached";

our $VERSION = sprintf "%d.%03d", q$Revision: 1.6 $ =~ /(\d+)/g;

#------------------------------------------------------------------------
# Constructor
#------------------------------------------------------------------------
sub new {
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my %parameters;

	# parameters provided by new method
	if ( ref( $_[0] ) eq "HASH" ) {
		%parameters = %{ $_[0] };
	} else {
		%parameters = @_;
	}

	unless ( $parameters{location_id} ) {
		die "You need to provide a location id!\n";
	}

	# set some parameters to sensible values for a
	# current conditions object
	$parameters{current}  = 0;
	$parameters{forecast} = 10;
	$parameters{links}    = 0;

	# creating the SUPER instance
	my $self = $class->SUPER::new( \%parameters );
	$self->{ID} = $parameters{location_id};

	# getting first weather info
	$self->{WEATHER} = $self->get_weather( $self->{ID} );
	$self->{DAYS}    = undef;
	$self->_build_forecasts();    
	$self->{LSUP} = time();

	return $self;
}    # end new()

#------------------------------------------------------------------------
# refresh weather data
#------------------------------------------------------------------------
sub refresh {
	my $self = shift;
	my $now  = time();

	# only refresh if last update has been more than 15 min ago
	if ( ( $now - $self->{LSUP} ) > 900 ) {
		$self->{WEATHER} = $self->get_weather( $self->{ID} );
		$self->_build_forecasts();
		$self->{LSUP} = $now;
	}
	return 1;
}

#------------------------------------------------------------------------
# access location data
#------------------------------------------------------------------------
sub day {
	my $self = shift;
	my $day  = shift;    # 0 - 9

	return 0 unless ( ( $day >= 0 ) && ( $day <= 9 ) );

	$self->refresh();
	return $self->{DAYS}->[$day];
}

sub all {
	my $self = shift;
	$self->refresh();

	if ( wantarray() ) {
		return @{ $self->{DAYS} };
	} else {
		return $self->{DAYS};
	}
}

#------------------------------------------------------------------------
# build up forecast hashes
#------------------------------------------------------------------------
sub _build_forecasts {
	my $self = shift;

	# initialize $self->{DAYS} array of new DayForecast objects
	unless ( $self->{DAYS} ) {
		for ( my $i = 0 ; $i < 10 ; $i++ ) {
			$self->_debug("Initializing forecast for day $i");
			$self->{DAYS}->[$i] = Weather::Com::DayForecast->new($self->{ARGS});
		}
	}

	# then update the DayForecast objects
	# but put the timeszone into it...
	foreach my $day ( @{ $self->{WEATHER}->{dayf}->{day} } ) {
		my %args = (%{$day}, (zone => $self->{ARGS}->{zone}));
		$self->{DAYS}->[ $day->{d} ]->update(\%args);
	}

	return 1;
}

1;

__END__