Bug.pm

filter: Statementstmt 96.6% 87 / 90 Branchbran 50.0% 8 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 100% 6 / 6 totaltotal 90.5% 115 / 127 CC 12 SCAR 8.2
CC
12
Cov
90%
get_forecast
3.0
get_live_compact_weather
3.0
request
3.0
CRAP 2.3
« Bug/Quantity.pm ↑ Summary Bug/Weather.pm »
1 package Weather::Bug;
2  
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Weather::Bug::Station;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Weather::Bug::SevenDayForecast;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Weather::Bug::Alert;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 our $VERSION = '0.25';
13  
14 12 has 'licensekey' => ( is => 'ro', isa => 'Str', init_arg => '-key', required => 1 );
15  
16 # This can be overridden in the child test class
17 0 ▶sub _get {
Subroutine _get: not called CC=1 SCAR=6.9
18 0 my $self = shift;
19 0 my $url = shift;
20 0 return LWP::Simple::get( $url );
21 }
22  
23 sub list_stations
24 4 ▶{
Subroutine list_stations: called CC=3 SCAR=11.0
Pod documented
25 4 my $self = shift;
26 4 my $zipcode = shift; # might want to validate.
27  
28 4 my $response = $self->request( 'getStations', { zipcode => $zipcode } );
29  
30 4 my $p = XML::LibXML->new();
31 4 my $doc = $p->parse_string( $response );
32 4 ▶ die "Unable to parse output from 'getStations' request.\n" unless defined $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc 0 4 4
33  
34 4 my @stations = ();
35 4 foreach my $node ($doc->findnodes( "//aws:station" ))
36 {
37 100 my $station = Weather::Bug::Station->from_xml(
38 $self, $node
39 );
40 100 push @stations, $station;
41 }
42  
43 4 return @stations;
44 }
45  
46 sub request
47 12 ▶{
Subroutine request: called CC=3 SCAR=11.0
Pod documented
48 12 my $self = shift;
49 12 my $cmd = shift;
50 12 my $parms = shift;
51  
52 12 my $url = "http://$self->{licensekey}.api.wxbug.net/$cmd.aspx?acode=$self->{licensekey}";
53 12 foreach my $p (keys %{$parms})
54 {
55 16 $url .= "&$p=$parms->{$p}";
56 }
57  
58 12 my $output = $self->_get( $url );
59 12 ▶ die "Request for '$cmd' failed.\n" unless defined $output;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output 0 12 12
60 12 return $output;
61 }
62  
63 sub get_forecast
64 3 ▶{
Subroutine get_forecast: called CC=3 SCAR=11.1
Pod documented
65 3 my $self = shift;
66 3 my $zipcode = shift;
67  
68 3 my $response = $self->request( 'getFullForecast', { zipcode => $zipcode } );
69  
70 3 my $p = XML::LibXML->new();
71 3 my $doc = $p->parse_string( $response );
72 3 ▶ die "Unable to parse output from 'getFullForecast' request.\n" unless defined $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc 0 3 3
73  
74 3 my ($f) = $doc->findnodes( "//aws:forecasts" );
75 3 ▶ die "No 'aws:forecasts' node found.\n" unless defined $f;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f 0 3 3
76  
77 3 return Weather::Bug::SevenDayForecast->from_xml( $f );
78 }
79  
80 sub get_alerts
81 1 ▶{
Subroutine get_alerts: called CC=3 SCAR=11.0
Pod documented
82 1 my $self = shift;
83 1 my $zipcode = shift;
84  
85 1 my $response = $self->request( 'getalerts', { zipcode => $zipcode } );
86  
87 1 my $p = XML::LibXML->new();
88 1 my $doc = $p->parse_string( $response );
89 1 ▶ die "Unable to parse output from 'getalerts' request.\n" unless defined $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc 0 1 1
90  
91 1 my @alerts = ();
92 1 foreach my $node ($doc->findnodes( "//aws:alert" ))
93 {
94 1 my $alert = Weather::Bug::Alert->from_xml( $node );
95 1 push @alerts, $alert;
96 }
97  
98 1 return @alerts;
99 }
100  
101 sub get_live_compact_weather
102 1 ▶{
Subroutine get_live_compact_weather: called CC=3 SCAR=11.1
Pod documented
103 1 my $self = shift;
104 1 my $id = shift;
105 1 my $zipcode = shift;
106  
107 1 my $response = $self->request(
108 'getLiveCompactWeather',
109 { zipcode => $zipcode, StationID => $id }
110 );
111  
112 1 my $p = XML::LibXML->new();
113 1 my $doc = $p->parse_string( $response );
114 1 ▶ die "Unable to parse output from 'getLiveCompactWeather' request.\n" unless defined $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc 0 1 1
115  
116 1 my ($w) = $doc->findnodes( "//aws:weather" );
117 1 ▶ die "No 'aws:weather' node found.\n" unless defined $w;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w 0 1 1
118 1 return Weather::Bug::CompactWeather->from_xml(
119 $w, $self
120 );
121 }
122  
123 sub get_live_weather
124 1 ▶{
Subroutine get_live_weather: called CC=2 SCAR=6.9
Pod documented
125 1 my $self = shift;
126 1 my $id = shift;
127 1 my $zipcode = shift;
128  
129 1 my $response = $self->request(
130 'getLiveWeather',
131 { zipcode => $zipcode, StationID => $id }
132 );
133  
134 1 my $p = XML::LibXML->new();
135 1 my $doc = $p->parse_string( $response );
136 1 ▶ die "Unable to parse output from 'getLiveWeather' request.\n" unless defined $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc 0 1 1
137  
138 1 my ($ob) = $doc->findnodes( "//aws:ob" );
139 1 return Weather::Bug::Weather->from_xml( $ob, $self );
140 }
141  
142 12 1; # Magic true value required at end of module
143 __END__
« Bug/Quantity.pm ↑ Summary Bug/Weather.pm »