Weather::Com::CurrentConditions - class containing current weather conditions


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

Index


Code Index:

NAME

Top

Weather::Com::CurrentConditions - class containing current weather conditions

SYNOPSIS

Top

  #!/usr/bin/perl -w
  use Weather::Com::Finder;

  # you have to fill in your ids from weather.com here
  my $PartnerId  = 'somepartnerid';
  my $LicenseKey = 'mylicense';

  my %weatherargs = (
	'partner_id' => $PartnerId,
	'license'    => $LicenseKey,
	'language'   => 'de',
  );

  my $finder = Weather::Com::Finder->new(%weatherargs);

  # if you want an array of locations:
  my @locations = $finder->find('Heidelberg');

  my $conditions = $locations[0]->current_conditions();
  print "Found weather for city: ", $location->name(), "\n";
  print "Current conditions are ", $conditions->description(), "\n";
  print "Current temperature is ", $conditions->temperature(), "°C\n";
  print "... as found out by observatory ", $conditions->observatory(), "\n";

DESCRIPTION

Top

Using Weather::Com::CurrentCondition objects provide current weather conditions of its parent object (a location) to you.

You get Weather::Com::CurrentConditions objects by calling the method current_conditions() of your location object.

Weather::Com::CurrentConditions is a subclass of Weather::Com::Cached. An instance of this class will update itself corresponding to the caching rules any time one of its methods is called.

CONSTRUCTOR

Top

new(hash or hashref)

The constructor will usually not be used directly because you get a ready to use current conditions objects from your location object.

If you ever want to instantiate current conditions objects on your own, you have to provide the same configuration hash or hashref to the constructor you usually would provide to the new() method of Weather::Com::Finder. In addition it is necessary to add a hash element location_id to this config hash. The location_id has to be a valid weather.com location id.

METHODS

Top

id()

Returns the location id used to instantiate this object.

name()

Returns the name of the location this current conditions belong to.

description([$language])

Returns a textual representation of the current weather conditions.

This description is translated if you specified the language option as argument for your Weather::Com::Finder.

This attribute is dynamic language enabled.

dewpoint()

Returns the dewpoint.

humidity()

Returns the humidity (in %).

icon()

Returns the number of the icon that can be used to display the current weather conditions. These icons are available with the weather.com sdk. You can download this sdk from weather.com after you've registered to get your license.

last_updated()

Returns a Weather::Com::DateTime object containing the date and time of the last update as provided by weather.com.

observatory()

Returns the name of the observatory that provided the current conditions to weather.com.

pressure()

Returns a Weather::Com::AirPressure object.

Please refer to Weather::Com::AirPressure for further details.

temperature()

Returns the temperature (depending on how you instantiated your Weather::Com::Finder you'll get centigrade (default) or degrees fahrenheit).

uv_index()

Returns a Weather::Com::UVIndex object.

Please refer to Weather::Com::UVIndex for further details.

visibility()

Returns the visibility (depending on how you instantiated your Weather::Com::Finder you'll get km (default) or miles).

wind()

Returns a Weather::Com::Wind object.

Please refer to Weather::Com::Wind for further details.

windchill()

Returns the windchill temperature (depending on how you instantiated your Weather::Com::Finder you'll get centigrade (default) or degrees fahrenheit).

SEE ALSO

Top

See also documentation of Weather::Com, Weather::Com::CurrentConditions, Weather::Com::Units.

AUTHOR

Top

Thomas Schnuecker, <thomas@schnuecker.de>

COPYRIGHT AND LICENSE

Top


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

package Weather::Com::CurrentConditions;

use 5.006;
use strict;
use warnings;
use Weather::Com::AirPressure;
use Weather::Com::DateTime;
use Weather::Com::Moon;
use Weather::Com::UVIndex;
use Weather::Com::Wind;
use Weather::Com::L10N;
use base "Weather::Com::Cached";

our $VERSION = sprintf "%d.%03d", q$Revision: 1.14 $ =~ /(\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}  = 1;
	$parameters{forecast} = 0;
	$parameters{links}    = 0;

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

	# getting first weather information
	$self->{BAR}             = undef;
	$self->{UV}              = undef;
	$self->{MOON}            = undef;
	$self->{WEATHER}         = $self->get_weather( $self->{ID} );
	$self->{WIND}            = undef;
	$self->{LAST_XML_UPDATE} = undef;

	# remember instantiation time (and later last update time)
	$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->{LSUP}    = $now;
	}
	return 1;
}

#------------------------------------------------------------------------
# access location data
#------------------------------------------------------------------------
sub id {
	my $self = shift;
	return $self->{ID};
}

sub name {
	my $self = shift;
	$self->refresh();
	return $self->{WEATHER}->{loc}->{dnam};
}

sub icon {
	my $self = shift;
	return $self->{WEATHER}->{cc}->{icon};
}

sub description {
	my $self     = shift;
	my $language = shift;

	$self->refresh();
	return $self->get_language_handle($language)
	  ->maketext( lc( $self->{WEATHER}->{cc}->{t} ) );

}

sub temperature {
	my $self = shift;
	$self->refresh();
	return $self->{WEATHER}->{cc}->{tmp};
}

sub windchill {
	my $self = shift;
	$self->refresh();
	return $self->{WEATHER}->{cc}->{flik};
}

sub humidity {
	my $self = shift;
	$self->refresh();
	return $self->{WEATHER}->{cc}->{hmid};
}

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

	unless ( $self->{BAR} ) {
		$self->{BAR} = Weather::Com::AirPressure->new( $self->{ARGS} );
	}
	$self->{BAR}->update( $self->{WEATHER}->{cc}->{bar} );
	return $self->{BAR};
}

sub dewpoint {
	my $self = shift;
	$self->refresh();
	return $self->{WEATHER}->{cc}->{dewp};
}

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

	unless ( $self->{MOON} ) {
		$self->{MOON} = Weather::Com::Moon->new( $self->{ARGS} );
	}
	$self->{MOON}->update( $self->{WEATHER}->{cc}->{moon} );
	return $self->{MOON};
}

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

	unless ( $self->{UV} ) {
		$self->{UV} = Weather::Com::UVIndex->new( $self->{ARGS} );
	}
	$self->{UV}->update( $self->{WEATHER}->{cc}->{uv} );
	return $self->{UV};
}

sub visibility {
	my $self = shift;
	$self->refresh();
	return $self->{WEATHER}->{cc}->{vis};
}

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

	unless ( $self->{WIND} ) {
		$self->{WIND} = Weather::Com::Wind->new( $self->{ARGS} );
	}
	$self->{WIND}->update( $self->{WEATHER}->{cc}->{wind} );
	return $self->{WIND};
}

sub observatory {
	my $self = shift;
	$self->refresh();
	return $self->{WEATHER}->{cc}->{obst};
}

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

	unless ( $self->{LAST_XML_UPDATE} ) {
		$self->{LAST_XML_UPDATE} =
		  Weather::Com::DateTime->new( $self->{ARGS}->{zone} );
	}

	$self->{LAST_XML_UPDATE}->set_lsup( $self->{WEATHER}->{cc}->{lsup} );
	return $self->{LAST_XML_UPDATE};
}

1;

__END__