App-wu

 view release on metacpan or  search on metacpan

lib/App/wu.pm  view on Meta::CPAN

use strict;
use warnings;
package App::wu;
$App::wu::VERSION = '0.06';
use WWW::Wunderground::API 0.06;
use Cache::FileCache;
use Carp;
use Try::Tiny;

#ABSTRACT: Terminal app that provides an hourly weather forecast using Weather Underground API




sub new
{
  croak 'Incorrect number of args passed to constructor' unless @_ == 3;
  my ($class, $location, $api_key) = @_;

  my $wu = new WWW::Wunderground::API(
    location => $location,
    api_key  => $api_key,
    auto_api => 1,
    cache    => Cache::FileCache->new({
      namespace          => 'wundercache',
      default_expires_in => 2400 }),
  );

  return bless { wu => $wu }, $class;
}

sub print_hourly
{
  my $self = shift;
  my $wu = $self->{wu};

  try {
    my @hourly_results = @{ $wu->hourly };

    # print header
    binmode STDOUT, ':utf8'; # for degrees symbol
    printf "%-10s%-4s%-4s%-8s%-20s\n",
           'Time',
           "\x{2109}",
           "\x{2103}",
           'Rain %',
           'Conditions';

    # print hourly
    for (@hourly_results)
    {
      printf "%8s%4i%4i%8i  %-30s\n",
             $_->{FCTTIME}{civil},
             $_->{temp}{english},
             $_->{temp}{metric},
             $_->{pop},
             $_->{condition};
    }
  } catch
  {   # see if there is an error message to display
    if (exists $wu->{data}{hourly}{response}{error}{description})
    {
      print "$wu->{data}{hourly}{response}{error}{description}\n";
    }
    else
    {
      print "Error connecting to Wunderground API (is your Internet connection active?)\n";
    }



( run in 1.630 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )