| 1 | # ABSTRACT: provides perl API to DarkSky | ||||||||||||||||||||||||||||
| 2 | package DarkSky::API; | ||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = '1.1.5'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | my $json = JSON::XS->new->pretty->canonical; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | my $api = "https://api.darksky.net/forecast"; | |||||||||||||||||||||||||||
| 14 | 1 | my $docs = "https://darksky.net/dev/"; | |||||||||||||||||||||||||||
| 15 | 1 | my %units = ( | |||||||||||||||||||||||||||
| 16 | si => 1, | ||||||||||||||||||||||||||||
| 17 | us => 1, | ||||||||||||||||||||||||||||
| 18 | auto => 1, | ||||||||||||||||||||||||||||
| 19 | ca => 1, | ||||||||||||||||||||||||||||
| 20 | uk => 1, | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | has key => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 24 | has units => ( | ||||||||||||||||||||||||||||
| 25 | is => 'ro', | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | 'isa' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 27 | die "Invalid units specified: see $docs\n" | ||||||||||||||||||||||||||||
| 28 | 0 | ▶ | unless exists( $units{ $_[0] } ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | }, | ||||||||||||||||||||||||||||
| 30 | 1 | 'default' => 'auto', | |||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | has latitude => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 34 | 1 | has longitude => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 35 | 1 | has 'time' => ( is => 'ro', default => '' ); | |||||||||||||||||||||||||||
| 36 | 1 | has timezone => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 37 | 1 | has offset => ( is => 'ro' ); # Deprecated | |||||||||||||||||||||||||||
| 38 | 1 | has currently => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 39 | 1 | has minutely => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 40 | 1 | has hourly => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 41 | 1 | has daily => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 42 | 1 | has alerts => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 43 | 1 | has flags => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 44 | 1 | has lang => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | 0 | my ( $class, %args ) = @_; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | my $url = ""; | |||||||||||||||||||||||||||
| 50 | 0 | my @params; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | if ( exists( $args{time} ) && $args{time} ne '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $args{"time"} and $args{"time"} ne ""
MC/DC0%0/2
exists $args{"time"} and $args{"time"} ne ""
exists $args{"time"}
$args{"time"} ne ""
Truth table0%0/3
A and B
A= exists $args{"time"}B= $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 | ▶ | if ( exists( $args{units} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | if ( exists( $args{lang} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | unless $response->{success}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | return decode_json( $response->{content} ); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub TO_JSON { return { %{ shift() } }; } | ||||||||||||||||||||||||||
|
Subroutine
TO_JSON: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | 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<https://github.com/mlbright/DarkSky-API> | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | DarkSky API docs: L<https://darksky.net/dev/> | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Another Perl API to DarkSky: L<http://search.cpan.org/~mallen/WebService-ForecastIO-0.01> | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Copyright (c) 2017 L<Martin-Louis Bright> | ||||||||||||||||||||||||||||
| 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<http://dev.perl.org/licenses/>. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||||||||||