line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::PerlWatcher::Watcher::Weather; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$App::PerlWatcher::Watcher::Weather::VERSION = '0.20'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
# ABSTRACT: Weather watches based around api.yr.no. Currenlty monitors only temperature and does no any notifications / level alerts. |
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
1673
|
use 5.12.0; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
80
|
|
8
|
2
|
|
|
2
|
|
32
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
58
|
|
9
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
47
|
|
10
|
2
|
|
|
2
|
|
1096
|
use utf8; |
|
2
|
|
|
|
|
12
|
|
|
2
|
|
|
|
|
108
|
|
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
1335
|
use App::PerlWatcher::EventItem; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
59
|
|
13
|
2
|
|
|
2
|
|
14
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
138
|
|
14
|
2
|
|
|
2
|
|
11
|
use Smart::Comments -ENV; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
25
|
|
15
|
2
|
|
|
2
|
|
1184
|
use Moo; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
13
|
|
16
|
2
|
|
|
2
|
|
2771
|
use XML::XPath; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $T_UNITS = { |
19
|
|
|
|
|
|
|
celcius => 'C°', |
20
|
|
|
|
|
|
|
}; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has 'latitude' => ( is => 'ro', required => 1); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has 'longitude' => ( is => 'ro', required => 1); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# for internal usage |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
has 'data' => ( is => 'rw', default => sub{ {}; } ); |
32
|
|
|
|
|
|
|
has 'url_generator' => ( is => 'lazy'); |
33
|
|
|
|
|
|
|
has 'url' => ( is => 'lazy'); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
with qw/App::PerlWatcher::Watcher::HTTP/; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub _build_url_generator { |
38
|
|
|
|
|
|
|
return sub { |
39
|
|
|
|
|
|
|
my ($lat, $lon) = @_; |
40
|
|
|
|
|
|
|
return "http://api.yr.no/weatherapi/locationforecast/1.8/?lat=$lat;lon=$lon"; |
41
|
|
|
|
|
|
|
}; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub _build_url { |
45
|
|
|
|
|
|
|
my $self = shift; |
46
|
|
|
|
|
|
|
return $self->url_generator->($self->latitude, $self->longitude); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub description { |
50
|
|
|
|
|
|
|
my $self = shift; |
51
|
|
|
|
|
|
|
my $desc = ""; |
52
|
|
|
|
|
|
|
my %data = %{ $self->data // {} }; |
53
|
|
|
|
|
|
|
if ( %data ) { |
54
|
|
|
|
|
|
|
$desc .= join(q{, } , |
55
|
|
|
|
|
|
|
map { $_ . ": " . $data{$_} } |
56
|
|
|
|
|
|
|
sort keys (%data) |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
return $desc; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub process_http_response { |
63
|
|
|
|
|
|
|
my ($self, $content, $headers) = @_; |
64
|
|
|
|
|
|
|
my $xp = XML::XPath->new(xml => $content); |
65
|
|
|
|
|
|
|
my $t_node = $xp->find('//time[1]/location/temperature'); |
66
|
|
|
|
|
|
|
if ($t_node) { |
67
|
|
|
|
|
|
|
my $t_item = $t_node->shift; |
68
|
|
|
|
|
|
|
my $value = $t_item->find('string(./@value)'); |
69
|
|
|
|
|
|
|
my $unit = $t_item->find('string(./@unit)'); |
70
|
|
|
|
|
|
|
$self->data->{t} = sprintf("%s%s", $value, $T_UNITS->{$unit}); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
$self->interpret_result(1, $self->callback); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub _invoke_callback { |
76
|
|
|
|
|
|
|
my ($self, $callback, $status) = @_; |
77
|
|
|
|
|
|
|
$callback->($status); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
1; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
__END__ |