| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Weather::YR::Base; |
|
2
|
3
|
|
|
3
|
|
2700
|
use Moose; |
|
|
3
|
|
|
|
|
8
|
|
|
|
3
|
|
|
|
|
19
|
|
|
3
|
3
|
|
|
3
|
|
19862
|
use namespace::autoclean; |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
19
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
3
|
|
|
3
|
|
185
|
use DateTime::Format::ISO8601; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
110
|
|
|
6
|
3
|
|
|
3
|
|
32
|
use DateTime::TimeZone; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
104
|
|
|
7
|
3
|
|
|
3
|
|
22
|
use DateTime; |
|
|
3
|
|
|
|
|
9
|
|
|
|
3
|
|
|
|
|
116
|
|
|
8
|
3
|
|
|
3
|
|
5371
|
use LWP::UserAgent; |
|
|
3
|
|
|
|
|
152527
|
|
|
|
3
|
|
|
|
|
124
|
|
|
9
|
3
|
|
|
3
|
|
27
|
use Mojo::URL; |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
53
|
|
|
10
|
3
|
|
|
3
|
|
5116
|
use XML::LibXML; |
|
|
3
|
|
|
|
|
123026
|
|
|
|
3
|
|
|
|
|
21
|
|
|
11
|
3
|
|
|
3
|
|
4480
|
use XML::Simple; |
|
|
3
|
|
|
|
|
28797
|
|
|
|
3
|
|
|
|
|
30
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has 'service_url' => ( |
|
14
|
|
|
|
|
|
|
isa => 'Mojo::URL', |
|
15
|
|
|
|
|
|
|
is => 'ro', |
|
16
|
|
|
|
|
|
|
lazy => 1, |
|
17
|
|
|
|
|
|
|
default => sub { Mojo::URL->new('https://api.met.no') }, |
|
18
|
|
|
|
|
|
|
); |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has [ 'lat', 'lon', 'msl' ] => ( |
|
21
|
|
|
|
|
|
|
isa => 'Maybe[Num]', |
|
22
|
|
|
|
|
|
|
is => 'rw', |
|
23
|
|
|
|
|
|
|
required => 0, |
|
24
|
|
|
|
|
|
|
default => 0, |
|
25
|
|
|
|
|
|
|
); |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has 'xml' => ( |
|
28
|
|
|
|
|
|
|
isa => 'Maybe[Str]', |
|
29
|
|
|
|
|
|
|
is => 'rw', |
|
30
|
|
|
|
|
|
|
required => 0, |
|
31
|
|
|
|
|
|
|
); |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
default => sub { DateTime::TimeZone->new( name => 'UTC' ); }, |
|
45
|
|
|
|
|
|
|
); |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has 'ua' => ( |
|
48
|
|
|
|
|
|
|
isa => 'Object', |
|
49
|
|
|
|
|
|
|
is => 'rw', |
|
50
|
|
|
|
|
|
|
required => 0, |
|
51
|
|
|
|
|
|
|
default => sub { LWP::UserAgent->new; }, |
|
52
|
|
|
|
|
|
|
); |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
has 'xml_ref' => ( |
|
55
|
|
|
|
|
|
|
isa => 'Maybe[HashRef]', |
|
56
|
|
|
|
|
|
|
is => 'ro', |
|
57
|
|
|
|
|
|
|
lazy_build => 1, |
|
58
|
|
|
|
|
|
|
); |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub _build_xml_ref { |
|
61
|
1
|
|
|
1
|
|
6
|
my $self = shift; |
|
62
|
|
|
|
|
|
|
|
|
63
|
1
|
50
|
|
|
|
32
|
unless ( length $self->xml ) { |
|
64
|
0
|
|
|
|
|
0
|
my $response = $self->ua->get( $self->url->to_string ); |
|
65
|
|
|
|
|
|
|
|
|
66
|
0
|
0
|
|
|
|
0
|
if ( $self->can('status_code') ) { |
|
67
|
0
|
|
|
|
|
0
|
$self->status_code( $response->code ); |
|
68
|
|
|
|
|
|
|
} |
|
69
|
|
|
|
|
|
|
|
|
70
|
0
|
0
|
|
|
|
0
|
if ( $response->is_success ) { |
|
71
|
0
|
|
|
|
|
0
|
$self->xml( $response->decoded_content ); |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
else { |
|
74
|
0
|
|
|
|
|
0
|
warn "Failed to GET data from " . $self->url->to_string; |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
1
|
50
|
|
|
|
25
|
if ( length $self->xml ) { |
|
79
|
1
|
50
|
|
|
|
6
|
if ( $self->can('schema_url') ) { |
|
80
|
1
|
|
|
|
|
4
|
eval { |
|
81
|
1
|
|
|
|
|
13
|
my $xml_doc = XML::LibXML->new->load_xml( string => $self->xml ); |
|
82
|
1
|
|
|
|
|
8498
|
my $response = $self->ua->get( $self->schema_url->to_string ); |
|
83
|
1
|
|
|
|
|
401073
|
my $schema = XML::LibXML::Schema->new( string => $response->decoded_content ); |
|
84
|
|
|
|
|
|
|
|
|
85
|
1
|
|
|
|
|
15878
|
$schema->validate( $xml_doc ); |
|
86
|
|
|
|
|
|
|
}; |
|
87
|
|
|
|
|
|
|
|
|
88
|
1
|
50
|
|
|
|
10
|
if ( $@ ) { |
|
89
|
0
|
|
|
|
|
0
|
warn "Failed to validate the XML returned from YR.no using schema URL '" . $self->schema_url . "'; $@"; |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
else { |
|
92
|
1
|
|
|
|
|
3
|
my $result = undef; |
|
93
|
|
|
|
|
|
|
|
|
94
|
1
|
|
|
|
|
3
|
eval { |
|
95
|
|
|
|
|
|
|
# $result = XML::Bare->new( text => $self->xml )->parse; |
|
96
|
1
|
|
|
|
|
49
|
$result = XML::Simple::XMLin( $self->xml, ForceArray => 0 ); |
|
97
|
|
|
|
|
|
|
}; |
|
98
|
|
|
|
|
|
|
|
|
99
|
1
|
50
|
|
|
|
309948
|
unless ( $@ ) { |
|
100
|
1
|
|
|
|
|
68
|
return $result; |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
} |
|
104
|
|
|
|
|
|
|
} |
|
105
|
|
|
|
|
|
|
else { |
|
106
|
0
|
|
|
|
|
0
|
warn "No XML to parse!"; |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Something failed! |
|
110
|
0
|
|
|
|
|
0
|
return undef; |
|
111
|
|
|
|
|
|
|
} |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub date_to_datetime { |
|
114
|
684
|
|
|
684
|
0
|
1254
|
my $self = shift; |
|
115
|
684
|
|
50
|
|
|
1891
|
my $date = shift // ''; |
|
116
|
|
|
|
|
|
|
|
|
117
|
684
|
50
|
|
|
|
1862
|
if ( length $date ) { |
|
118
|
684
|
|
|
|
|
2276
|
$date = DateTime::Format::ISO8601->parse_datetime( $date ); |
|
119
|
|
|
|
|
|
|
} |
|
120
|
|
|
|
|
|
|
else { |
|
121
|
0
|
|
|
|
|
0
|
$date = DateTime->now; |
|
122
|
|
|
|
|
|
|
} |
|
123
|
|
|
|
|
|
|
|
|
124
|
684
|
|
|
|
|
341311
|
$date->set_time_zone( $self->tz ); |
|
125
|
|
|
|
|
|
|
|
|
126
|
684
|
|
|
|
|
230230
|
return $date; |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub lat_as_string { |
|
130
|
1
|
|
|
1
|
0
|
3
|
my $self = shift; |
|
131
|
|
|
|
|
|
|
|
|
132
|
1
|
|
|
|
|
30
|
my $lat = $self->lat; |
|
133
|
1
|
50
|
|
|
|
15
|
return defined $lat ? sprintf "%.4f", $lat : ''; |
|
134
|
|
|
|
|
|
|
} |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub lon_as_string { |
|
137
|
1
|
|
|
1
|
0
|
4
|
my $self = shift; |
|
138
|
|
|
|
|
|
|
|
|
139
|
1
|
|
|
|
|
48
|
my $lon = $self->lon; |
|
140
|
1
|
50
|
|
|
|
35
|
return defined $lon ? sprintf "%.4f", $lon : ''; |
|
141
|
|
|
|
|
|
|
} |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
1; |