TropicalCyclone.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« TropicalCyclone/Storm.pm ↑ Summary
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 Weather::NHC::TropicalCyclone::Storm ();
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 our $DEFAULT_URL = q{https://www.nhc.noaa.gov/CurrentStorms.json};
11 our $DEFAULT_TIMEOUT = 10;
12  
13 # container class for requesting JSON and providing
14 # iterator access and meta operations for the storms
15 # contained in the JSON returned by NHC
16  
17 sub new {
18 my $pkg = shift;
19 my $self = {
20 _obj => undef,
21 };
22 bless $self, $pkg;
23 return $self;
24 }
25  
26 sub fetch {
27 my ( $self, $timeout ) = @_;
28 my $http = HTTP::Tiny->new();
29  
30 local $SIG{ALRM} = sub { die "Request has timed out.\n" };
31  
32 alarm( $timeout // $DEFAULT_TIMEOUT );
33  
34 # get content via $DEFAULT_URL unless --file option is passed
35 local $@;
36 my $response = eval { $http->get($DEFAULT_URL) };
37 if ( $@ or not $response or $response->{status} ne HTTP_OK ) {
38 die qq{request error\n};
39 }
40  
41 alarm 0;
42  
43 my $content = $response->{content};
44  
45 my $ref = eval { JSON::XS::decode_json $content };
46  
47 if ( $@ or not $ref ) {
48 die qq{JSON decode error\n};
49 }
50  
51 $self->{_obj} = $ref;
52  
53 return $self;
54 }
55  
56 sub active_storms {
57 my $self = shift;
58  
59 my @storms = ();
60  
61 for my $storm ( @{ $self->{_obj}->{activeStorms} } ) {
62 my $s = Weather::NHC::TropicalCyclone::Storm->new($storm);
63 push @storms, $s;
64 }
65  
66 return \@storms;
67 }
68  
69 1;
70  
71 __END__
« TropicalCyclone/Storm.pm ↑ Summary