blib/lib/Weather/Airport.pm

filter: Statementstmt 28.8% 13 / 45 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 66.6% 4 / 6 Podpod 100% 2 / 2 totaltotal 26.3% 19 / 72 CC 9 SCAR 30.8
CC
9
Cov
21%
new
30.0
query
30.0
BEGIN
1.0
CRAP 21.7
↑ Summary
1 package Weather::Airport;
2  
3 2 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.01';
9  
10 =head1 NAME
11  
12 Weather::Airport - Provides an interface to FlightStats.com Airport Weather Query
13  
14 =head1 SYNOPSIS
15  
16 use Weather::Airport;
17 my $wa = Weather::Airport->new;
18 my $airport = $wa->query('LAX');
19 use Data::Dumper;
20 print Dumper ($airport);
21  
22 =head1 DESCRIPTION
23  
24  
25  
26 =head2 new
27  
28 Creates Weather::Airport object.
29  
30 =cut
31  
32 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
33 0 my $self = bless({}, shift);
34 0 my %args = @_;
35 0 ▶ $self->{url} = $args{url} || 'http://www.flightstats.com/go/Airport/weather.do?airport=';
Condition0%0/2
$args{'url'} || 'http://www.flightstats.com/go/Airport/weather.do?airport='
l!l
00
MC/DC0%0/1
$args{'url'} || 'http://www.flightstats.com/go/Airport/weather.do?airport='
$args{'url'}
Truth table0%0/2
A
A= $args{'url'}
Aresult
11
00
36 0 ▶ $self->{referer} = $args{referer} || 'http://www.flightstats.com/go/Home/home.do';
Condition0%0/2
$args{'referer'} || 'http://www.flightstats.com/go/Home/home.do'
l!l
00
MC/DC0%0/1
$args{'referer'} || 'http://www.flightstats.com/go/Home/home.do'
$args{'referer'}
Truth table0%0/2
A
A= $args{'referer'}
Aresult
11
00
37 0 ▶ $self->{uastring} = $args{uastring} || 'Weather::Airport/'.$VERSION;
Condition0%0/2
$args{'uastring'} || 'Weather::Airport/' . $Weather::Airport::VERSION
l!l
00
MC/DC0%0/1
$args{'uastring'} || 'Weather::Airport/' . $Weather::Airport::VERSION
$args{'uastring'}
Truth table0%0/2
A
A= $args{'uastring'}
Aresult
11
00
38 0 $self->{ua} = $args{ua}; # pass an existing LWP::UserAgent object
39  
40 0 ▶ if (!$self->{ua})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ua"}) 0 0 0
41 {
42 0 $self->{ua} = LWP::UserAgent->new;
43 0 $self->{ua}->agent($self->{uastring});
44 }
45 0 return $self;
46 }
47  
48 =head2 query
49  
50 Queries the site. Provide an airport code to retrieve current conditions.
51  
52 =cut
53  
54 0 ▶sub query {
Subroutine query: not called CC=5 SCAR=34.0
Pod documented
55 0 my $self = shift;
56 0 my $apcode = shift;
57 0 my @data;
58 0 my $error = "The airport code is invalid.";
59 0 $apcode =~ s/[^A-Za-z0-9]//g;
60 0 my $req = HTTP::Request->new(GET => $self->{url} . $apcode);
61 0 ▶ $req->referer($self->{referer}) if $self->{referer};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"referer"} 0 0 0
62 0 $req->content_type('application/x-www-form-urlencoded');
63 0 my $res = $self->{ua}->request($req);
64 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 0 0 0
65 0 my $content = $res->content;
66 0 $content =~ s/\s+/ /g;
67 0 ▶ if ($content =~ m#The airport code is invalid#) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /The airport code is invalid/) { } 0 0 0
68 0 return $error;
69 }
70 else {
71 0 ▶ while ($content =~ m/<tr> <td class="label">(.*?)<\/tr>/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[<tr> <td class="label">(.*?)</tr>]g) 0 0 0
72 0 my $ap = ($1);
73 0 $ap =~ s/<(.*?)>//gi;
74 0 $ap =~ s/\&nbsp\;//gi;
75 0 $ap =~ s/\&deg\;/°/gi;
76 0 $ap =~ s/\s+/ /g;
77 0 push(@data, "$ap");
78 }
79 0 return \@data;
80 }
81 }
82 }
83  
84 2 1;
85  
86 =head1 COPYRIGHT AND LICENSE
87  
88 Copyright (C) 2010 Joseph Tartaro, E<lt>droogie@foster.stonedcoder.orgE<gt>
89  
90 This library is free software; you can redistribute it and/or modify
91 it under the same terms as Perl itself, either Perl version 5.10.0 or,
92 at your option, any later version of Perl 5 you may have available.
93  
94 =cut
95  
96  
↑ Summary