line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: provides perl API to DarkSky |
2
|
|
|
|
|
|
|
package DarkSky::API; |
3
|
1
|
|
|
1
|
|
64241
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
36
|
|
4
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
5
|
1
|
|
|
1
|
|
552
|
use JSON::XS; |
|
1
|
|
|
|
|
7303
|
|
|
1
|
|
|
|
|
95
|
|
6
|
1
|
|
|
1
|
|
504
|
use HTTP::Tiny; |
|
1
|
|
|
|
|
40345
|
|
|
1
|
|
|
|
|
38
|
|
7
|
1
|
|
|
1
|
|
458
|
use Moo; |
|
1
|
|
|
|
|
13267
|
|
|
1
|
|
|
|
|
9
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '1.1.5'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my $json = JSON::XS->new->pretty->canonical; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $api = "https://api.darksky.net/forecast"; |
14
|
|
|
|
|
|
|
my $docs = "https://darksky.net/dev/"; |
15
|
|
|
|
|
|
|
my %units = ( |
16
|
|
|
|
|
|
|
si => 1, |
17
|
|
|
|
|
|
|
us => 1, |
18
|
|
|
|
|
|
|
auto => 1, |
19
|
|
|
|
|
|
|
ca => 1, |
20
|
|
|
|
|
|
|
uk => 1, |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
has key => ( is => 'ro' ); |
24
|
|
|
|
|
|
|
has units => ( |
25
|
|
|
|
|
|
|
is => 'ro', |
26
|
|
|
|
|
|
|
'isa' => sub { |
27
|
|
|
|
|
|
|
die "Invalid units specified: see $docs\n" |
28
|
|
|
|
|
|
|
unless exists( $units{ $_[0] } ); |
29
|
|
|
|
|
|
|
}, |
30
|
|
|
|
|
|
|
'default' => 'auto', |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
has latitude => ( is => 'ro' ); |
34
|
|
|
|
|
|
|
has longitude => ( is => 'ro' ); |
35
|
|
|
|
|
|
|
has 'time' => ( is => 'ro', default => '' ); |
36
|
|
|
|
|
|
|
has timezone => ( is => 'ro' ); |
37
|
|
|
|
|
|
|
has offset => ( is => 'ro' ); # Deprecated |
38
|
|
|
|
|
|
|
has currently => ( is => 'ro' ); |
39
|
|
|
|
|
|
|
has minutely => ( is => 'ro' ); |
40
|
|
|
|
|
|
|
has hourly => ( is => 'ro' ); |
41
|
|
|
|
|
|
|
has daily => ( is => 'ro' ); |
42
|
|
|
|
|
|
|
has alerts => ( is => 'ro' ); |
43
|
|
|
|
|
|
|
has flags => ( is => 'ro' ); |
44
|
|
|
|
|
|
|
has lang => ( is => 'ro' ); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub BUILDARGS { |
47
|
0
|
|
|
0
|
0
|
|
my ( $class, %args ) = @_; |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
my $url = ""; |
50
|
0
|
|
|
|
|
|
my @params; |
51
|
0
|
0
|
0
|
|
|
|
if ( exists( $args{time} ) && $args{time} ne '' ) { |
52
|
0
|
|
|
|
|
|
@params = ( $args{latitude}, $args{longitude}, $args{time} ); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
else { |
55
|
0
|
|
|
|
|
|
@params = ( $args{latitude}, $args{longitude} ); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
my $params = join( ',', @params ); |
59
|
|
|
|
|
|
|
|
60
|
0
|
0
|
|
|
|
|
if ( exists( $args{units} ) ) { |
61
|
|
|
|
|
|
|
$url |
62
|
|
|
|
|
|
|
= $api . '/' |
63
|
|
|
|
|
|
|
. $args{key} . '/' |
64
|
|
|
|
|
|
|
. $params |
65
|
|
|
|
|
|
|
. "?units=" |
66
|
0
|
|
|
|
|
|
. $args{units}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
else { |
69
|
0
|
|
|
|
|
|
$url = $api . '/' . $args{key} . '/' . $params . "?units=auto"; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
0
|
0
|
|
|
|
|
if ( exists( $args{lang} ) ) { |
73
|
0
|
|
|
|
|
|
$url = $url . "&lang=" . $args{lang}; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
my $response = HTTP::Tiny->new->get($url); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
die "Request to '$url' failed: $response->{status} $response->{reason}\n" |
79
|
0
|
0
|
|
|
|
|
unless $response->{success}; |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
return decode_json( $response->{content} ); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
0
|
0
|
|
sub TO_JSON { return { %{ shift() } }; } |
|
0
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
1; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=pod |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=encoding utf-8 |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 NAME |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
DarkSky::API - Provides Perl API to DarkSky |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 SYNOPSIS |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
use DarkSky::API; |
99
|
|
|
|
|
|
|
use JSON::XS; |
100
|
|
|
|
|
|
|
use feature 'say'; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
my $lat = 43.6667; |
103
|
|
|
|
|
|
|
my $long = -79.4167; |
104
|
|
|
|
|
|
|
my $time = "1475363709"; # example epoch time (optional) |
105
|
|
|
|
|
|
|
my $key = "c9ce1c59d139c3dc62961cbd63097d13"; # example DarkSky API key |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
my $forecast = DarkSky::API->new( |
108
|
|
|
|
|
|
|
key => $key, |
109
|
|
|
|
|
|
|
longitude => $long, |
110
|
|
|
|
|
|
|
latitude => $lat, |
111
|
|
|
|
|
|
|
time => $time |
112
|
|
|
|
|
|
|
); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
say "current temperature: " . $forecast->{currently}->{temperature}; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
my $json = JSON::XS->new->canonical->pretty; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# Allow blessed objects to be serialized |
119
|
|
|
|
|
|
|
# See http://search.cpan.org/~mlehmann/JSON-XS-3.02/XS.pm#OBJECT_SERIALISATION |
120
|
|
|
|
|
|
|
$json->convert_blessed([1]); |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
say $json->encode($forecast); |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 DESCRIPTION |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
This module is a wrapper around the DarkSky API. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 REFERENCES |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Git repository: L |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
DarkSky API docs: L |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Another Perl API to DarkSky: L |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 COPYRIGHT |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Copyright (c) 2017 L |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 LICENSE |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
This library is free software and may be distributed under the same terms |
143
|
|
|
|
|
|
|
as perl itself. See L. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |