| 1 | package Weather::Com::Simple; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use Weather::Com::Base qw(celsius2fahrenheit convert_winddirection); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use Weather::Com::Cached; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | our $VERSION = sprintf "%d.%03d", q$Revision: 1.3 $ =~ /(\d+)/g; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 10 | # Constructor | ||||||||||||||||||||
| 11 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 12 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||
| 13 | 0 | my $proto = shift; | |||||||||||||||||||
| 14 | 0 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 15 | 0 | my $self = {}; | |||||||||||||||||||
| 16 | 0 | my %parameters; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | # some general attributes | ||||||||||||||||||||
| 19 | 0 | $self->{PROXY} = "none"; | |||||||||||||||||||
| 20 | 0 | $self->{DEBUG} = 0; | |||||||||||||||||||
| 21 | 0 | $self->{CACHE} = "."; | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | # parameters provided by new method | ||||||||||||||||||||
| 24 | 0 | ▶ | if ( ref( $_[0] ) eq "HASH" ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | 0 | %parameters = %{ $_[0] }; | |||||||||||||||||||
| 26 | } else { | ||||||||||||||||||||
| 27 | 0 | %parameters = @_; | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 0 | $self = bless( $self, $class ); | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | # check mandatory parameters | ||||||||||||||||||||
| 33 | 0 | ▶ | unless ( $parameters{place} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | 0 | $self->_debug("ERROR: Location not specified"); | |||||||||||||||||||
| 35 | 0 | return undef; | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | # put wanted parameters into $self | ||||||||||||||||||||
| 39 | 0 | $self->{PLACE} = $parameters{place}; | |||||||||||||||||||
| 40 | 0 | ▶ | $self->{PROXY} = $parameters{proxy} if ( $parameters{proxy} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | $self->{PROXY_USER} = $parameters{proxy_user} | ||||||||||||||||||||
| 42 | 0 | ▶ | if ( $parameters{proxy_user} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | $self->{PROXY_PASS} = $parameters{proxy_pass} | ||||||||||||||||||||
| 44 | 0 | ▶ | if ( $parameters{proxy_pass} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | 0 | ▶ | $self->{DEBUG} = $parameters{debug} if ( $parameters{debug} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 0 | ▶ | $self->{CACHE} = $parameters{cache} if ( $parameters{cache} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | # Weather::Com::Cached object | ||||||||||||||||||||
| 49 | my %weatherargs = ( | ||||||||||||||||||||
| 50 | 'current' => 1, | ||||||||||||||||||||
| 51 | 'forecast' => 0, | ||||||||||||||||||||
| 52 | 'proxy' => $self->{PROXY}, | ||||||||||||||||||||
| 53 | 'proxy_user' => $self->{PROXY_USER}, | ||||||||||||||||||||
| 54 | 'proxy_pass' => $self->{PROXY_PASS}, | ||||||||||||||||||||
| 55 | 'debug' => $self->{DEBUG}, | ||||||||||||||||||||
| 56 | 'cache' => $self->{CACHE}, | ||||||||||||||||||||
| 57 | 0 | ); | |||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 0 | ▶ | $weatherargs{timeout} = $parameters{timeout} if ( $parameters{timeout} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | $weatherargs{partner_id} = $parameters{partner_id} | ||||||||||||||||||||
| 61 | 0 | ▶ | if ( $parameters{partner_id} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | 0 | ▶ | $weatherargs{license} = $parameters{license} if ( $parameters{license} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | # initialize weather object | ||||||||||||||||||||
| 65 | 0 | $self->{WEATHER} = Weather::Com::Cached->new(%weatherargs); | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | return $self; | |||||||||||||||||||
| 68 | } # end new() | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 71 | # accessor methods | ||||||||||||||||||||
| 72 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 73 | 0 | ▶ | sub get_weather { | ||||||||||||||||||
|
Subroutine
get_weather: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 74 | 0 | my $self = shift; | |||||||||||||||||||
| 75 | 0 | my $allweather; | |||||||||||||||||||
| 76 | 0 | my $place_result = $self->{WEATHER}->search( $self->{PLACE} ); | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | # check if search succeeded | ||||||||||||||||||||
| 79 | 0 | ▶ | unless ($place_result) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | 0 | $self->_debug($@); | |||||||||||||||||||
| 81 | 0 | return undef; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | foreach ( keys %{$place_result} ) { | |||||||||||||||||||
| 85 | 0 | my $weatherdata = $self->{WEATHER}->get_weather($_); | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 0 | ▶ | unless ($weatherdata) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | 0 | $self->_debug($@); | |||||||||||||||||||
| 89 | 0 | return 0; | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | my $place_weather = { | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | # header data | ||||||||||||||||||||
| 95 | 'place' => $weatherdata->{loc}->{dnam}, | ||||||||||||||||||||
| 96 | 'updated' => _parse_timestring( $weatherdata->{cc}->{lsup} ), | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | # temperature celsius/fahrenheit | ||||||||||||||||||||
| 99 | 'celsius' => $weatherdata->{cc}->{tmp}, | ||||||||||||||||||||
| 100 | 'temperature_celsius' => $weatherdata->{cc}->{tmp}, | ||||||||||||||||||||
| 101 | 'windchill_celsius' => $weatherdata->{cc}->{flik}, | ||||||||||||||||||||
| 102 | 'fahrenheit' => celsius2fahrenheit( $weatherdata->{cc}->{tmp} ), | ||||||||||||||||||||
| 103 | 'temperature_fahrenheit' => | ||||||||||||||||||||
| 104 | celsius2fahrenheit( $weatherdata->{cc}->{tmp} ), | ||||||||||||||||||||
| 105 | 'windchill_fahrenheit' => | ||||||||||||||||||||
| 106 | celsius2fahrenheit( $weatherdata->{cc}->{flik} ), | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | # wind | ||||||||||||||||||||
| 109 | 'wind' => _parse_wind( $weatherdata->{cc}->{wind} ), | ||||||||||||||||||||
| 110 | 'windspeed_kmh' => | ||||||||||||||||||||
| 111 | _parse_windspeed_kmh( $weatherdata->{cc}->{wind} ), | ||||||||||||||||||||
| 112 | 'windspeed_mph' => | ||||||||||||||||||||
| 113 | _parse_windspeed_mph( $weatherdata->{cc}->{wind} ), | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | # other | ||||||||||||||||||||
| 116 | 'humidity' => $weatherdata->{cc}->{hmid}, | ||||||||||||||||||||
| 117 | 'conditions' => $weatherdata->{cc}->{t}, | ||||||||||||||||||||
| 118 | 0 | 'pressure' => _parse_pressure( $weatherdata->{cc}->{bar} ), | |||||||||||||||||||
| 119 | }; | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 0 | push( @{$allweather}, $place_weather ); | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 0 | return $allweather; | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 0 | ▶ | sub getweather { | ||||||||||||||||||
|
Subroutine
getweather: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 128 | 0 | return get_weather(@_); | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 132 | # internal parsing utilities | ||||||||||||||||||||
| 133 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 134 | 0 | ▶ | sub _parse_wind { | ||||||||||||||||||
|
Subroutine
_parse_wind: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 135 | 0 | my $winddata = shift; | |||||||||||||||||||
| 136 | 0 | my $wind; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 0 | ▶ | if ( lc( $winddata->{s} ) =~ /calm/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | 0 | $wind = "calm"; | |||||||||||||||||||
| 140 | } else { | ||||||||||||||||||||
| 141 | 0 | my $kmh = _parse_windspeed_kmh($winddata); | |||||||||||||||||||
| 142 | 0 | my $mph = _parse_windspeed_mph($winddata); | |||||||||||||||||||
| 143 | 0 | my $direction = convert_winddirection( $winddata->{t} ); | |||||||||||||||||||
| 144 | 0 | $wind = "$mph mph $kmh km/h from the $direction"; | |||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | 0 | return $wind; | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 0 | ▶ | sub _parse_windspeed_kmh { | ||||||||||||||||||
|
Subroutine
_parse_windspeed_kmh: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 150 | 0 | my $winddata = shift; | |||||||||||||||||||
| 151 | 0 | my $speed; | |||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 0 | ▶ | if ( lc( $winddata->{s} ) =~ /calm/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | 0 | $speed = "calm"; | |||||||||||||||||||
| 155 | } else { | ||||||||||||||||||||
| 156 | 0 | $speed = $winddata->{s}; | |||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | 0 | return $speed; | |||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 0 | ▶ | sub _parse_windspeed_mph { | ||||||||||||||||||
|
Subroutine
_parse_windspeed_mph: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 162 | 0 | my $winddata = shift; | |||||||||||||||||||
| 163 | 0 | my $speed; | |||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 0 | ▶ | if ( lc( $winddata->{s} ) =~ /calm/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 166 | 0 | $speed = "calm"; | |||||||||||||||||||
| 167 | } else { | ||||||||||||||||||||
| 168 | 0 | $speed = sprintf( "%d", $winddata->{s} * 0.6213722 ); | |||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | 0 | return $speed; | |||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 0 | ▶ | sub _parse_pressure { | ||||||||||||||||||
|
Subroutine
_parse_pressure: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 174 | 0 | my $pressuredata = shift; | |||||||||||||||||||
| 175 | 0 | my $pressure; | |||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 0 | my $hPa = $pressuredata->{r}; | |||||||||||||||||||
| 178 | 0 | $hPa =~ s/,//g; | |||||||||||||||||||
| 179 | 0 | my $in = $hPa * 0.02953; | |||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 0 | $pressure = sprintf( "%.2f in / %.1f hPa", $in, $hPa ); | |||||||||||||||||||
| 182 | } | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 0 | ▶ | sub _parse_timestring { | ||||||||||||||||||
|
Subroutine
_parse_timestring: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 185 | 0 | my $timestring = shift; | |||||||||||||||||||
| 186 | 0 | my @months = ( | |||||||||||||||||||
| 187 | "", "January", "February", "March", | ||||||||||||||||||||
| 188 | "April", "May", "June", "July", | ||||||||||||||||||||
| 189 | "August", "September", "October", "November", | ||||||||||||||||||||
| 190 | "Dezember" | ||||||||||||||||||||
| 191 | ); | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | 0 | my ( $date, $time, $ampm, $zone ) = split( / /, $timestring ); | |||||||||||||||||||
| 194 | 0 | my ( $month, $mday, $year ) = split( "/", $date ); | |||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | 0 | return "$time $ampm $zone on $months[$month] $mday, " . ( $year + 2000 ); | |||||||||||||||||||
| 197 | } | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 200 | # other internals | ||||||||||||||||||||
| 201 | #------------------------------------------------------------------------ | ||||||||||||||||||||
| 202 | 0 | ▶ | sub _debug { | ||||||||||||||||||
|
Subroutine
_debug: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 203 | 0 | my $self = shift; | |||||||||||||||||||
| 204 | 0 | my $notice = shift; | |||||||||||||||||||
| 205 | 0 | ▶ | if ( $self->{DEBUG} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 206 | 0 | carp ref($self) . " DEBUG NOTE: $notice\n"; | |||||||||||||||||||
| 207 | 0 | return 1; | |||||||||||||||||||
| 208 | } | ||||||||||||||||||||
| 209 | 0 | return 0; | |||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | 1 | 1; | |||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | __END__ | ||||||||||||||||||||