File Coverage

blib/lib/Forecast/IO.pm
Criterion Covered Total %
statement 23 32 71.8
branch 2 8 25.0
condition 1 3 33.3
subroutine 6 6 100.0
pod 0 1 0.0
total 32 50 64.0


line stmt bran cond sub pod time code
1             # ABSTRACT: provides perl API to Forecast.io
2             package Forecast::IO;
3 1     1   29526 use strict;
  1         2  
  1         53  
4 1     1   5 use warnings;
  1         3  
  1         32  
5 1     1   1171 use JSON;
  1         17798  
  1         5  
6 1     1   2249 use HTTP::Tiny;
  1         61317  
  1         38  
7 1     1   856 use Moo;
  1         16164  
  1         7  
8              
9             my $api = "https://api.forecast.io/forecast";
10             my $docs = "https://developer.forecast.io/docs/v2";
11             my %units = (
12             si => 1,
13             us => 1,
14             auto => 1,
15             ca => 1,
16             uk => 1,
17             );
18              
19             has key => ( is => 'ro' );
20             has units => (
21             is => 'ro',
22             'isa' => sub {
23             die "Invalid units specified: see $docs\n"
24             unless exists( $units{ $_[0] } );
25             },
26             'default' => 'auto',
27             );
28              
29             has latitude => ( is => 'ro' );
30             has longitude => ( is => 'ro' );
31             has 'time' => ( is => 'ro', default => '' );
32             has timezone => ( is => 'ro' );
33             has offset => ( is => 'ro' );
34             has currently => ( is => 'ro' );
35             has minutely => ( is => 'ro' );
36             has hourly => ( is => 'ro' );
37             has daily => ( is => 'ro' );
38             has alerts => ( is => 'ro' );
39             has flags => ( is => 'ro' );
40              
41             sub BUILDARGS {
42 1     1 0 2549 my ( $class, %args ) = @_;
43              
44 1         2 my $url = "";
45 1         3 my @params;
46 1 50 33     9 if ( exists( $args{time} ) && $args{time} ne '' ) {
47 1         4 @params = ( $args{latitude}, $args{longitude}, $args{time} );
48             }
49             else {
50 0         0 @params = ( $args{latitude}, $args{longitude} );
51             }
52              
53 1         24 my $params = join( ',', @params );
54              
55 1 50       5 if ( exists( $args{units} ) ) {
56 0         0 $url =
57             $api . '/' . $args{key} . '/' . $params . "?units=" . $args{units};
58             }
59             else {
60 1         216 $url = $api . '/' . $args{key} . '/' . $params . "?units=auto";
61             }
62              
63 0           my $response = HTTP::Tiny->new->get($url);
64              
65 0 0         die "Request to '$url' failed: $response->{status} $response->{reason}\n"
66             unless $response->{success};
67              
68 0           my $forecast = decode_json( $response->{content} );
69              
70 0           while ( my ( $key, $val ) = each %args ) {
71 0 0         unless ( exists( $forecast->{$key} ) ) {
72 0           $forecast->{$key} = $val;
73             }
74             }
75 0           return $forecast;
76             }
77              
78             1;
79             =pod
80              
81             =encoding utf-8
82              
83             =head1 NAME
84              
85             Forecast::IO - Provides Perl API to Forecast.io
86              
87             =head1 SYNOPSIS
88              
89             use 5.016;
90             use Forecast::IO;
91             use Data::Dumper;
92              
93             my $lat = 43.6667;
94             my $long = -79.4167;
95             my $key = "c9ce1c59d139c3dc62961cbd63097d13"; # example Forecast.io API key
96              
97             my $forecast = Forecast::IO->new(
98             key => $key,
99             longitude => $long,
100             latitude => $lat,
101             );
102              
103             say "current temperature: " . $forecast->{currently}->{temperature};
104              
105             my @daily_data_points = @{ $forecast->{daily}->{data} };
106              
107             # Use your imagination about how to use this data.
108             # in the meantime, inspect it by dumping it.
109             for (@daily_data_points) {
110             print Dumper($_);
111             }
112              
113             =head1 DESCRIPTION
114              
115             This module is a wrapper around the Forecast.io API.
116              
117             =head1 REFERENCES
118              
119             Git repository: L
120              
121             Forecast.io API docs: L
122              
123             =head1 COPYRIGHT
124              
125             Copyright (c) 2013 L
126              
127             =head1 LICENSE
128              
129             This library is free software and may be distributed under the same terms
130             as perl itself. See L.
131              
132             =cut