YR/Base.pm

filter: Statementstmt 85.7% 60 / 70 Branchbran 37.5% 6 / 16 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 1 totaltotal 77.8% 81 / 104 CC 10 SCAR 18.1
CC
10
Cov
76%
_build_xml_ref
14.8
date_to_datetime
3.2
BEGIN
1.0
CRAP 6.1
↑ Summary YR/LocationForecast/Day.pm »
1 package Weather::YR::Base;
2 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use DateTime::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 has 'service_url' => (
14 isa => 'Mojo::URL',
15 is => 'ro',
16 lazy => 1,
17 3 ▶ default => sub { Mojo::URL->new('https://api.met.no') },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 3 );
19  
20 3 has [ 'lat', 'lon', 'msl' ] => (
21 isa => 'Maybe[Num]',
22 is => 'rw',
23 required => 0,
24 default => 0,
25 );
26  
27 3 has 'xml' => (
28 isa => 'Maybe[Str]',
29 is => 'rw',
30 required => 0,
31 );
32  
33 3 has 'lang' => (
34 isa => 'Maybe[Str]',
35 is => 'rw',
36 required => 0,
37 default => 'nb',
38 );
39  
40 has 'tz' => (
41 isa => 'DateTime::TimeZone',
42 is => 'rw',
43 required => 0,
44 1 ▶ default => sub { DateTime::TimeZone->new( name => 'UTC' ); },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 3 );
46  
47 has 'ua' => (
48 isa => 'Object',
49 is => 'rw',
50 required => 0,
51 2 ▶ default => sub { LWP::UserAgent->new; },
Subroutine __ANON__: called CC=1 SCAR=0.0
52 3 );
53  
54 3 has 'xml_ref' => (
55 isa => 'Maybe[HashRef]',
56 is => 'ro',
57 lazy_build => 1,
58 );
59  
60 sub _build_xml_ref {
61 1 ▶ my $self = shift;
Subroutine _build_xml_ref: called CC=8 SCAR=26.9
62  
63 1 ▶ unless ( length $self->xml ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $self->xml) 0 1 1
64 0 my $response = $self->ua->get( $self->url->to_string );
65  
66 0 ▶ if ( $self->can('status_code') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("status_code")) 0 0 0
67 0 $self->status_code( $response->code );
68 }
69  
70 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
71 0 $self->xml( $response->decoded_content );
72 }
73 else {
74 0 warn "Failed to GET data from " . $self->url->to_string;
75 }
76 }
77  
78 1 ▶ if ( length $self->xml ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->xml) { } 1 0 1
79 1 ▶ if ( $self->can('schema_url') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("schema_url")) 1 0 1
80 1 eval {
81 1 my $xml_doc = XML::LibXML->new->load_xml( string => $self->xml );
82 1 my $response = $self->ua->get( $self->schema_url->to_string );
83 1 my $schema = XML::LibXML::Schema->new( string => $response->decoded_content );
84  
85 1 $schema->validate( $xml_doc );
86 };
87  
88 1 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 1 1
89 0 warn "Failed to validate the XML returned from YR.no using schema URL '" . $self->schema_url . "'; $@";
90 }
91 else {
92 1 my $result = undef;
93  
94 1 eval {
95 # $result = XML::Bare->new( text => $self->xml )->parse;
96 1 $result = XML::Simple::XMLin( $self->xml, ForceArray => 0 );
97 };
98  
99 1 ▶ unless ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 1 0 1
100 1 return $result;
101 }
102 }
103 }
104 }
105 else {
106 0 warn "No XML to parse!";
107 }
108  
109 # Something failed!
110 0 return undef;
111 }
112  
113 sub date_to_datetime {
114 684 ▶ my $self = shift;
Subroutine date_to_datetime: called CC=3 SCAR=11.6
Pod undocumented
115 684 ▶ my $date = shift // '';
Condition50%1/2
shift() // ""
l!l
6840
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
116  
117 684 ▶ if ( length $date ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $date) { } 684 0 684
118 684 $date = DateTime::Format::ISO8601->parse_datetime( $date );
119 }
120 else {
121 0 $date = DateTime->now;
122 }
123  
124 684 $date->set_time_zone( $self->tz );
125  
126 684 return $date;
127 }
128  
129  
130 3 __PACKAGE__->meta->make_immutable;
131  
132 3 1;
↑ Summary YR/LocationForecast/Day.pm »