TropicalCyclone.pm

filter: Statementstmt 100% 127 / 127 Branchbran 92.5% 37 / 40 Conditioncond 77.7% 14 / 18 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 100% 26 / 26 Podpod 91.6% 11 / 12 totaltotal 94.4% 221 / 234 CC 28 SCAR 11.1
CC
28
Cov
96%
_fetch_options
6.0
fetch
5.0
load_json
4.0
CRAP 3.0
« TropicalCyclone/Resource.pm ↑ Summary TropicalCyclone/StormTable.pm »
1 package Weather::NHC::TropicalCyclone;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use JSON::PP qw/decode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Util::H2O::More qw/h2o/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Weather::NHC::TropicalCyclone::HTTP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Weather::NHC::TropicalCyclone::Storm ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Weather::NHC::TropicalCyclone::Validation ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $VERSION = q{0.36};
12 3 our $DEFAULT_URL = q{https://www.nhc.noaa.gov/CurrentStorms.json};
13 3 our $DEFAULT_RSS_ATLANTIC = q{https://www.nhc.noaa.gov/index-at.xml};
14 3 our $DEFAULT_RSS_EAST_PACIFIC = q{https://www.nhc.noaa.gov/index-ep.xml};
15 3 our $DEFAULT_RSS_CENTRAL_PACIFIC = q{https://www.nhc.noaa.gov/index-cp.xml};
16 3 our $DEFAULT_TIMEOUT = 10;
17  
18 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
19 4 my ( $class, %args ) = @_;
20  
21 my $http = Weather::NHC::TropicalCyclone::HTTP->new(
22 4 ▶ ( exists $args{http} ? ( client => $args{http} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'http'} ? : 4 0 4
23 );
24  
25 4 return bless {
26 _obj => undef,
27 _storms => {},
28 _http => $http,
29 _validation_warnings => [],
30 }, $class;
31 }
32  
33 8 ▶sub fetch {
Subroutine fetch: called CC=5 SCAR=16.1
Pod documented
34 8 my ( $self, @args ) = @_;
35 8 my %opts = $self->_fetch_options(@args);
36  
37 8 ▶ my $url = $opts{url} // $DEFAULT_URL;
Condition66%2/3
$opts{"url"} // $Weather::NHC::TropicalCyclone::DEFAULT_URL
l!l&&r!l&&!r
260
MC/DC0%0/2
$opts{"url"} // $Weather::NHC::TropicalCyclone::DEFAULT_URL
$opts{"url"} $Weather::NHC::TropicalCyclone::DEFAULT_URL
Truth table66%2/3
A // B
A= $opts{"url"}B= $Weather::NHC::TropicalCyclone::DEFAULT_URL
ABresult
1X1
011
000
38 8 ▶ my $timeout = exists $opts{timeout} ? $opts{timeout} : $DEFAULT_TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'timeout'} ? : 7 1 8
39  
40 8 my $response = $self->_get_with_timeout( $url, $timeout );
41 6 my $content = $response->{content};
42  
43 6 ▶ my $file = $opts{save_to} // $opts{file};
Condition100%3/3
$opts{"save_to"} // $opts{"file"}
l!l&&r!l&&!r
312
MC/DC100%2/2
$opts{"save_to"} // $opts{"file"}
$opts{"save_to"} $opts{"file"}
Truth table100%3/3
A // B
A= $opts{"save_to"}B= $opts{"file"}
ABresult
1X1
011
000
44 6 ▶ _write_file( $file, $content ) if defined $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file 4 2 6
45  
46 5 return $self->load_json($content);
47 }
48  
49 12 ▶sub load_json {
Subroutine load_json: called CC=4 SCAR=13.9
Pod documented
50 12 my ( $self, $json ) = @_;
51 12 ▶ die qq{JSON input is required\n} if !defined $json;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $json 1 11 12
52  
53 11 my $data = eval { decode_json($json) };
54 11 ▶ die qq{JSON decode error: $@} if $@ || !defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not defined $data 1 10 11
Condition66%2/3
$@ or not defined $data
l!l&&r!l&&!r
1010
MC/DC50%1/2
$@ or not defined $data
$@ not defined $data
Truth table66%2/3
A or not(B)
A= $@B= not defined $data
ABresult
1X1
011
000
55  
56 10 return $self->_load_data($data);
57 }
58  
59 3 ▶sub load_file {
Subroutine load_file: called CC=4 SCAR=13.9
Pod documented
60 3 my ( $self, $file ) = @_;
61 3 ▶ die qq{JSON file is required\n} if !defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 1 2 3
62 2 ▶ open my $fh, q{<}, $file or die qq{Can't open '$file': $!\n};
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Can't open '${file}': $!\n" 1 1 2
63 1 local $/;
64 1 my $json = <$fh>;
65 1 ▶ close $fh or die qq{Can't close '$file': $!\n};
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Can't close '${file}': $!\n" 1 0 1
66 1 return $self->load_json($json);
67 }
68  
69 3 ▶sub validate {
Subroutine validate: called CC=4 SCAR=13.9
Pod documented
70 3 my ( $self, $input ) = @_;
71 3 my $data = $input;
72  
73 3 ▶ if ( !ref $input ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $input) 2 1 3
74 2 $data = eval { decode_json($input) };
75 2 ▶ return { errors => [ qq{JSON decode error: $@} ], warnings => [] } if $@ || !defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not defined $data 1 1 2
Condition66%2/3
$@ or not defined $data
l!l&&r!l&&!r
101
MC/DC50%1/2
$@ or not defined $data
$@ not defined $data
Truth table66%2/3
A or not(B)
A= $@B= not defined $data
ABresult
1X1
011
000
76 }
77  
78 2 return Weather::NHC::TropicalCyclone::Validation->validate($data);
79 }
80  
81 1 ▶sub validation_warnings {
Subroutine validation_warnings: called CC=1 SCAR=0.0
Pod documented
82 1 my $self = shift;
83 1 return [ @{ $self->{_validation_warnings} } ];
84 }
85  
86 2 ▶sub active_storms {
Subroutine active_storms: called CC=1 SCAR=0.0
Pod documented
87 2 my $self = shift;
88 2 return [ map { $self->{_storms}->{$_} } sort keys %{ $self->{_storms} } ];
89 }
90  
91 6 ▶sub get_storm_by_id {
Subroutine get_storm_by_id: called CC=2 SCAR=6.9
Pod documented
92 6 my ( $self, $id ) = @_;
93 6 ▶ return undef if !defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 1 5 6
94 5 return $self->{_storms}->{$id};
95 }
96  
97 3 ▶sub get_storm_ids {
Subroutine get_storm_ids: called CC=1 SCAR=0.0
Pod documented
98 3 my $self = shift;
99 3 return [ sort keys %{ $self->{_storms} } ];
100 }
101  
102 2 ▶sub fetch_rss_atlantic {
Subroutine fetch_rss_atlantic: called CC=1 SCAR=0.0
Pod documented
103 2 my ( $self, $local_file ) = @_;
104 2 return $self->_fetch_rss( $DEFAULT_RSS_ATLANTIC, $local_file );
105 }
106  
107 1 ▶sub fetch_rss_east_pacific {
Subroutine fetch_rss_east_pacific: called CC=1 SCAR=0.0
Pod documented
108 1 my ( $self, $local_file ) = @_;
109 1 return $self->_fetch_rss( $DEFAULT_RSS_EAST_PACIFIC, $local_file );
110 }
111  
112 1 ▶sub fetch_rss_central_pacific {
Subroutine fetch_rss_central_pacific: called CC=1 SCAR=0.0
Pod documented
113 1 my ( $self, $local_file ) = @_;
114 1 return $self->_fetch_rss( $DEFAULT_RSS_CENTRAL_PACIFIC, $local_file );
115 }
116  
117 10 ▶sub _load_data {
Subroutine _load_data: called CC=2 SCAR=6.9
118 10 my ( $self, $data ) = @_;
119 10 my $report = Weather::NHC::TropicalCyclone::Validation->validate($data);
120  
121 10 ▶ if ( @{ $report->{errors} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$report->{"errors"};}) 1 9 10
122 die q{NHC CurrentStorms.json validation failed: }
123 1 . join( q{; }, @{ $report->{errors} } ) . qq{\n};
124 }
125  
126 9 $self->{_validation_warnings} = [ @{ $report->{warnings} } ];
127  
128 # Keep the decoded structure recognizable as HASH/ARRAY data while adding
129 # convenient accessors to hashes. Storm objects are created separately.
130 9 $self->{_obj} = h2o -recurse, $data;
131 9 $self->_update_storm_cache;
132 9 return $self;
133 }
134  
135 9 ▶sub _update_storm_cache {
Subroutine _update_storm_cache: called CC=2 SCAR=6.9
136 9 my $self = shift;
137 9 $self->{_storms} = {};
138  
139 9 for my $storm ( @{ $self->{_obj}->{activeStorms} } ) {
140 my $object = Weather::NHC::TropicalCyclone::Storm->new(
141 $storm,
142 http => $self->{_http},
143 16 );
144 16 $self->{_storms}->{ $object->id } = $object;
145 }
146  
147 9 return $self->{_storms};
148 }
149  
150 4 ▶sub _fetch_rss {
Subroutine _fetch_rss: called CC=2 SCAR=6.9
151 4 my ( $self, $rss_url, $local_file ) = @_;
152 4 my $response = $self->{_http}->get($rss_url);
153 4 my $content = $response->{content};
154 4 ▶ _write_file( $local_file, $content ) if defined $local_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $local_file 1 3 4
155 4 return $content;
156 }
157  
158 8 ▶sub _get_with_timeout {
Subroutine _get_with_timeout: called CC=3 SCAR=11.0
159 8 my ( $self, $url, $timeout ) = @_;
160  
161 8 ▶ return $self->{_http}->get($url) if !$timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless $timeout 6 2 8
162  
163 2 ▶ local $SIG{ALRM} = sub { die qq{Request has timed out.\n} };
Subroutine __ANON__: called CC=1 SCAR=0.0
164 2 my $response;
165 2 my $ok = eval {
166 2 alarm($timeout);
167 2 $response = $self->{_http}->get($url);
168 1 alarm(0);
169 1 1;
170 };
171 2 my $error = $@;
172 2 alarm(0);
173 2 ▶ die $error if !$ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 1 2
174 1 return $response;
175 }
176  
177 8 ▶sub _fetch_options {
Subroutine _fetch_options: called CC=6 SCAR=17.9
178 8 my ( $self, @args ) = @_;
179 8 ▶ return () if !@args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 1 7 8
180  
181 7 ▶ if ( @args % 2 == 0 && defined $args[0] && $args[0] =~ /^(?:timeout|save_to|file|url)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args % 2 == 0 and defined $args[0] and $args[0] =~ /^(?:timeout|save_to|file|url)$/) 5 2 7
Condition83%5/6
@args % 2 == 0 and defined $args[0] and $args[0] =~ /^(?:timeout|save_to|file|url)$/
!ll&&!rl&&r
115
@args % 2 == 0 and defined $args[0]
!ll&&!rl&&r
106
MC/DC66%2/3
@args % 2 == 0 and defined $args[0] and $args[0] =~ /^(?:timeout|save_to|file|url)$/
@args % 2 == 0 defined $args[0] $args[0] =~ /^(?:timeout|save_to|file|url)$/
Truth table75%3/4
A and B and C
A= @args % 2 == 0B= defined $args[0]C= $args[0] =~ /^(?:timeout|save_to|file|url)$/
ABCresult
0XX0
10X0
1100
1111
182 5 return @args;
183 }
184  
185 # Backward-compatible positional form: fetch($timeout, $file)
186 return (
187 2 ▶ timeout => $args[0],
Branch: 1 branch
BranchTrueFalseTotal
@args > 1 ? : 1 1 2
188 ( @args > 1 ? ( save_to => $args[1] ) : () ),
189 );
190 }
191  
192 5 ▶sub _write_file {
Subroutine _write_file: called CC=3 SCAR=11.0
193 5 my ( $file, $content ) = @_;
194 5 ▶ open my $fh, q{>}, $file or die qq{Can't open '$file': $!\n};
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die "Can't open '${file}': $!\n" 4 1 5
195 4 print {$fh} $content;
196 4 ▶ close $fh or die qq{Can't close '$file': $!\n};
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Can't close '${file}': $!\n" 4 0 4
197 4 return $file;
198 }
199  
200 3 1;
201  
202 __END__
« TropicalCyclone/Resource.pm ↑ Summary TropicalCyclone/StormTable.pm »