YR/Base.pm

filter: Statementstmt 71.0% 49 / 69 Branchbran 25.0% 4 / 16 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 1 totaltotal 64.0% 66 / 103 CC 10 SCAR 23.2
CC
10
Cov
61%
_build_xml_ref
23.4
date_to_datetime
12.0
BEGIN
1.0
CRAP 10.2
« YR/LocationForecast/Day.pm ↑ Summary YR/LocationForecast.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('http://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=31.5
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 $schema = XML::LibXML::Schema->new( location => $self->schema_url );
83  
84 0 $schema->validate( $xml_doc );
85 };
86  
87 1 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 1 0 1
88 1 warn "Failed to validate the XML returned from YR.no using schema URL '" . $self->schema_url . "'; $@";
89 }
90 else {
91 0 my $result = undef;
92  
93 0 eval {
94 # $result = XML::Bare->new( text => $self->xml )->parse;
95 0 $result = XML::Simple::XMLin( $self->xml, ForceArray => 0 );
96 };
97  
98 0 ▶ unless ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
99 0 return $result;
100 }
101 }
102 }
103 }
104 else {
105 0 warn "No XML to parse!";
106 }
107  
108 # Something failed!
109 1 return undef;
110 }
111  
112 sub date_to_datetime {
113 0 ▶ my $self = shift;
Subroutine date_to_datetime: not called CC=3 SCAR=24.8
Pod undocumented
114 0 ▶ my $date = shift // '';
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
115  
116 0 ▶ if ( length $date ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $date) { } 0 0 0
117 0 $date = DateTime::Format::ISO8601->parse_datetime( $date );
118 }
119 else {
120 0 $date = DateTime->now;
121 }
122  
123 0 $date->set_time_zone( $self->tz );
124  
125 0 return $date;
126 }
127  
128  
129 3 __PACKAGE__->meta->make_immutable;
130  
131 3 1;
« YR/LocationForecast/Day.pm ↑ Summary YR/LocationForecast.pm »