TropicalCyclone.pm

filter: Statementstmt 82.5% 71 / 86 Branchbran 33.3% 4 / 12 Conditioncond 31.2% 5 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 83.3% 15 / 18 Podpod 62.5% 5 / 8 totaltotal 67.1% 100 / 149 CC 14 SCAR 19.0
CC
14
Cov
70%
fetch
15.5
_fetch_rss
9.3
get_storm_by_id
2.0
CRAP 6.7
« TropicalCyclone/Storm.pm ↑ Summary TropicalCyclone/StormTable.pm »
1 package Weather::NHC::TropicalCyclone;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use HTTP::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use HTTP::Status qw/:constants/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Util::H2O qw/h2o/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Weather::NHC::TropicalCyclone::Storm ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = q{0.34};
12 2 our $DEFAULT_URL = q{https://www.nhc.noaa.gov/CurrentStorms.json};
13 2 our $DEFAULT_RSS_ATLANTIC = q{https://www.nhc.noaa.gov/index-at.xml};
14 2 our $DEFAULT_RSS_EAST_PACIFIC = q{https://www.nhc.noaa.gov/index-ep.xml};
15 2 our $DEFAULT_RSS_CENTRAL_PACIFIC = q{https://www.nhc.noaa.gov/index-cp.xml};
16 2 our $DEFAULT_TIMEOUT = 10;
17  
18 # container class for requesting JSON and providing
19 # iterator access and meta operations for the storms
20 # contained in the JSON returned by NHC
21  
22 sub new {
23 3 ▶ my $pkg = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
24 3 my $self = {
25 _obj => undef,
26 _storms => {},
27 };
28 3 return bless $self, $pkg;
29 }
30  
31 sub fetch {
32 2 ▶ my ( $self, $timeout, $file ) = @_;
Subroutine fetch: called CC=9 SCAR=27.4
Pod documented
33 2 my $http = HTTP::Tiny->new();
34  
35 2 ▶ local $SIG{ALRM} = sub { die "Request has timed out.\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
36  
37 2 ▶ alarm( $timeout // $DEFAULT_TIMEOUT );
Condition33%1/3
$timeout // $Weather::NHC::TropicalCyclone::DEFAULT_TIMEOUT
l!l&&r!l&&!r
020
MC/DC0%0/2
$timeout // $Weather::NHC::TropicalCyclone::DEFAULT_TIMEOUT
$timeout $Weather::NHC::TropicalCyclone::DEFAULT_TIMEOUT
Truth table33%1/3
A // B
A= $timeoutB= $Weather::NHC::TropicalCyclone::DEFAULT_TIMEOUT
ABresult
1X1
011
000
38  
39 # get content via $DEFAULT_URL unless --file option is passed
40 2 local $@;
41 2 my $response = eval { $http->get($DEFAULT_URL) };
42 2 ▶ if ( $@ or not $response or $response->{status} ne HTTP_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $response or $response->{"status"} ne 200) 0 2 2
Condition33%2/6
$@ or not $response or $response->{"status"} ne 200
l!l&&r!l&&!r
002
$@ or not $response
l!l&&r!l&&!r
002
MC/DC0%0/3
$@ or not $response or $response->{"status"} ne 200
$@ not $response $response->{"status"} ne 200
Truth table25%1/4
A or not(B) or C
A= $@B= not $responseC= $response->{"status"} ne 200
ABCresult
1XX1
01X1
0011
0000
43 0 die qq{request error\n};
44 }
45  
46 2 alarm 0;
47  
48 2 my $content = $response->{content};
49  
50 # if $file is provided, contents directly from the GET are
51 # written
52 2 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 0 2 2
53 0 ▶ open my $fh, q{>}, $file || die qq{Can't open '$file': $!\n};
Condition0%0/2
$file || die("Can't open '${file}': $!\n")
l!l
00
MC/DC0%0/1
$file || die("Can't open '${file}': $!\n")
$file
Truth table0%0/2
A
A= $file
Aresult
11
00
54 0 print $fh $content;
55 0 close $fh;
56 }
57  
58 2 my $ref = eval { JSON::XS::decode_json $content };
59  
60 2 ▶ if ( $@ or not $ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $ref) 0 2 2
Condition33%1/3
$@ or not $ref
l!l&&r!l&&!r
002
MC/DC0%0/2
$@ or not $ref
$@ not $ref
Truth table33%1/3
A or not(B)
A= $@B= not $ref
ABresult
1X1
011
000
61 0 die qq{JSON decode error\n};
62 }
63  
64 # add accessors based on elements in returned hash ref
65 2 $ref = h2o -recurse, $ref;
66  
67 2 $self->{_obj} = $ref;
68  
69 # reset and update storms cache
70 2 $self->_update_storm_cache;
71  
72 2 return $self;
73 }
74  
75 sub active_storms {
76 2 ▶ my $self = shift;
Subroutine active_storms: called CC=1 SCAR=0.0
Pod documented
77 2 return [ values %{ $self->{_storms} } ];
78 }
79  
80 # there is no checking, if the storm is not in the cache,
81 # an undefined value is returned
82 sub get_storm_by_id {
83 0 ▶ my ( $self, $id ) = @_;
Subroutine get_storm_by_id: not called CC=1 SCAR=6.9
Pod documented
84 0 return $self->{_storms}->{$id};
85 }
86  
87 # returns storm Ids
88 sub get_storm_ids {
89 0 ▶ my $self = shift;
Subroutine get_storm_ids: not called CC=1 SCAR=6.9
Pod documented
90 0 return [ keys %{ $self->{_storms} } ];
91 }
92  
93 sub _update_storm_cache {
94 2 ▶ my $self = shift;
Subroutine _update_storm_cache: called CC=2 SCAR=6.9
95  
96 # purge cache
97 2 $self->{_storms} = {};
98  
99 REBUILD_STORMS_CACHE:
100 2 for my $storm ( @{ $self->{_obj}->{activeStorms} } ) {
101 4 my $s = Weather::NHC::TropicalCyclone::Storm->new($storm);
102 4 my $storm_id = $s->id;
103  
104 # key storm by id (e.g., al182020, etc)
105 4 $self->{_storms}->{$storm_id} = $s;
106 }
107  
108 }
109  
110 sub fetch_rss_atlantic {
111 1 ▶ my ( $self, $local_file ) = @_;
Subroutine fetch_rss_atlantic: called CC=1 SCAR=0.0
Pod undocumented
112 1 return $self->_fetch_rss( $DEFAULT_RSS_ATLANTIC, $local_file );
113 }
114  
115 sub fetch_rss_east_pacific {
116 1 ▶ my ( $self, $local_file ) = @_;
Subroutine fetch_rss_east_pacific: called CC=1 SCAR=0.0
Pod undocumented
117 1 return $self->_fetch_rss( $DEFAULT_RSS_EAST_PACIFIC, $local_file );
118 }
119  
120 sub fetch_rss_central_pacific {
121 1 ▶ my ( $self, $local_file ) = @_;
Subroutine fetch_rss_central_pacific: called CC=1 SCAR=0.0
Pod undocumented
122 1 return $self->_fetch_rss( $DEFAULT_RSS_CENTRAL_PACIFIC, $local_file );
123 }
124  
125 sub _fetch_rss {
126 3 ▶ my ( $self, $rss_url, $local_file ) = @_;
Subroutine _fetch_rss: called CC=5 SCAR=22.3
127  
128 3 my $http = HTTP::Tiny->new;
129  
130 3 my $response = $http->get($rss_url);
131  
132 3 ▶ if ( not $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 3 0 3
133 3 ▶ my $status = $response->{status} // q{Unknown};
Condition50%1/2
$response->{"status"} // "Unknown"
l!l
30
MC/DC0%0/1
$response->{"status"} // "Unknown"
$response->{"status"}
Truth table50%1/2
A
A= $response->{"status"}
Aresult
11
00
134 3 die qq{Fetching of $rss_url failed. HTTP status: $status\n};
135 }
136  
137 0 ▶ if ($local_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local_file) 0 0 0
138 0 ▶ open my $fh, q{>}, $local_file or die qq{Error writing RSS file, $local_file: $!\n};
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $local_file or die "Error writing RSS file, ${local_file}: $!\n" 0 0 0
139 0 print $fh $response->{content};
140 }
141  
142 0 return $response->{content};
143 }
144  
145 2 1;
146  
147 __END__
« TropicalCyclone/Storm.pm ↑ Summary TropicalCyclone/StormTable.pm »