File Coverage

blib/lib/DarkSky/API.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package DarkSky::API;
2              
3 1     1   23669 use Moose;
  0            
  0            
4             use 5.010;
5             use Mojo::UserAgent;
6             use JSON;
7             use List::MoreUtils qw( natatime );
8              
9             =head1 NAME
10              
11             DarkSky::API - The Dark Sky API lets you query for short-term
12             precipitation forecast data at geographical points inside
13             the United States.
14              
15             =head1 VERSION
16              
17             Version 0.05
18              
19             =cut
20              
21             our $VERSION = '0.05';
22              
23             our $DARKSKY_API_URL = 'https://api.darkskyapp.com/v1';
24              
25             has 'api_key' => ( isa => 'Str', is => 'rw', required => 1 );
26              
27             =head1 SYNOPSIS
28              
29             Perl module for retrieving data from the Dark Sky API.
30             The Dark Sky API lets you query for short-term precipitation forecast data
31             at geographical points inside the United States.
32              
33             use DarkSky::API;
34              
35             my $darksky = DarkSky::API->new( api_key => '<your key here>' );
36            
37             # Returns a forecast for the next hour at a given location.
38             my $forecast = $darksky->forecast({ latitude => '42.7243', longitude => '-73.6927' });
39              
40             # Returns a brief forecast for the next hour at a given location.
41             my $brief_forecast = $darksky->brief_forecast({ latitude => '42.7243', longitude => '-73.6927' });
42              
43             # Returns forecasts for a collection of arbitrary points.
44             my $precipitation = $darksky->precipitation(['42.7','-73.6',1325607100,'42.0','-73.0',1325607791]);
45              
46             # Returns a list of interesting storms happening right now.
47             my $interesting_storms = $darksky->interesting();
48            
49             ...
50              
51             =head1 SUBROUTINES/METHODS
52              
53             =head2 forecast
54              
55             Returns a forecast for the next hour at a given location.
56              
57             my $forecast = $darksky->forecast({ latitude => '42.7243', longitude => '-73.6927' });
58            
59             =cut
60              
61             sub forecast {
62             my ( $self, $args ) = @_;
63             my $tx = Mojo::UserAgent->new()->get(
64             join( '/',
65             $DARKSKY_API_URL, "forecast",
66             $self->api_key, $args->{latitude} . ',' . $args->{longitude} )
67             );
68             return unless ( defined $tx );
69             return decode_json( $tx->res->body ) if ( $tx->res->code == 200 );
70             }
71              
72             =head2 brief_forecast
73              
74             Returns a brief forecast for the next hour at a given location.
75              
76             my $brief_forecast = $darksky->brief_forecast({ latitude => '42.7243', longitude => '-73.6927' });
77              
78             =cut
79              
80             sub brief_forecast {
81             my ( $self, $args ) = @_;
82             my $tx = Mojo::UserAgent->new()->get(
83             join( '/',
84             $DARKSKY_API_URL, "brief_forecast",
85             $self->api_key, $args->{latitude} . ',' . $args->{longitude} )
86             );
87             return unless ( defined $tx );
88             return decode_json( $tx->res->body ) if ( $tx->res->code == 200 );
89             }
90              
91             =head2 precipitation
92              
93             Returns forecasts for a collection of arbitrary points.
94              
95             my $precipitation = $darksky->precipitation(['42.7','-73.6',1325607100,'42.0','-73.0',1325607791]);
96              
97             =cut
98              
99             sub precipitation {
100             my ( $self, $latitudes_longitudes_times ) = @_;
101             return unless ( @{$latitudes_longitudes_times} % 3 == 0 );
102             my $it = natatime 3, @$latitudes_longitudes_times;
103             my @triplets;
104             while ( my @triplet = $it->() ) {
105             push @triplets, join( ',', @triplet );
106             }
107             my $url = join( '/',
108             $DARKSKY_API_URL, "precipitation",
109             $self->api_key, join( ';', @triplets ) );
110             my $tx = Mojo::UserAgent->new()->get($url);
111             unless ( defined $tx ) {
112             return;
113             }
114             if ( $tx->res->code == 200 ) {
115             return decode_json( $tx->res->body );
116             }
117             }
118              
119             =head2 interesting
120              
121             Returns a list of interesting storms happening right now.
122              
123             my $interesting_storms = $darksky->interesting();
124              
125             =cut
126              
127             sub interesting {
128             my ($self) = @_;
129             my $tx = Mojo::UserAgent->new()
130             ->get( join( '/', $DARKSKY_API_URL, "interesting", $self->api_key ) );
131              
132             unless ( defined $tx ) {
133             return;
134             }
135             return decode_json( $tx->res->body ) if ( $tx->res->code == 200 );
136             return $tx->res->code();
137             }
138              
139             =head1 AUTHOR
140              
141             Martin-Louis Bright, C<< <mlbright at gmail.com> >>
142              
143             =head1 BUGS
144              
145             Please report any bugs or feature requests to C<bug-darksky-api at rt.cpan.org>, or through
146             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DarkSky-API>. I will be notified, and then you'll
147             automatically be notified of progress on your bug as I make changes.
148              
149              
150              
151              
152             =head1 SUPPORT
153              
154             You can find documentation for this module with the perldoc command.
155              
156             perldoc DarkSky::API
157              
158              
159             You can also look for information at:
160              
161             =over 4
162              
163             =item * RT: CPAN's request tracker (report bugs here)
164              
165             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DarkSky-API>
166              
167             =item * AnnoCPAN: Annotated CPAN documentation
168              
169             L<http://annocpan.org/dist/DarkSky-API>
170              
171             =item * CPAN Ratings
172              
173             L<http://cpanratings.perl.org/d/DarkSky-API>
174              
175             =item * Search CPAN
176              
177             L<http://search.cpan.org/dist/DarkSky-API/>
178              
179             =back
180              
181              
182             =head1 ACKNOWLEDGEMENTS
183              
184              
185             =head1 LICENSE AND COPYRIGHT
186              
187             Copyright 2012 Martin-Louis Bright.
188              
189             This program is free software; you can redistribute it and/or modify it
190             under the terms of the the Artistic License (2.0). You may obtain a
191             copy of the full license at:
192              
193             L<http://www.perlfoundation.org/artistic_license_2_0>
194              
195             Any use, modification, and distribution of the Standard or Modified
196             Versions is governed by this Artistic License. By using, modifying or
197             distributing the Package, you accept this license. Do not use, modify,
198             or distribute the Package, if you do not accept this license.
199              
200             If your Modified Version has been derived from a Modified Version made
201             by someone other than you, you are nevertheless required to ensure that
202             your Modified Version complies with the requirements of this license.
203              
204             This license does not grant you the right to use any trademark, service
205             mark, tradename, or logo of the Copyright Holder.
206              
207             This license includes the non-exclusive, worldwide, free-of-charge
208             patent license to make, have made, use, offer to sell, sell, import and
209             otherwise transfer the Package with respect to any patent claims
210             licensable by the Copyright Holder that are necessarily infringed by the
211             Package. If you institute patent litigation (including a cross-claim or
212             counterclaim) against any party alleging that the Package constitutes
213             direct or contributory patent infringement, then this Artistic License
214             to you shall terminate on the date that such litigation is filed.
215              
216             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
217             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
218             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
219             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
220             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
221             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
222             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
223             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
224              
225              
226             =cut
227              
228             1; # End of DarkSky::API