File Coverage

blib/lib/WebService/ForecastIO/Request.pm
Criterion Covered Total %
statement 12 23 52.1
branch 0 4 0.0
condition n/a
subroutine 4 5 80.0
pod 0 1 0.0
total 16 33 48.4


line stmt bran cond sub pod time code
1 2     2   39956 use 5.014;
  2         8  
  2         116  
2              
3             package WebService::ForecastIO::Request;
4             {
5             $WebService::ForecastIO::Request::VERSION = '0.01';
6             }
7              
8 2     2   11 use Moo::Role;
  2         3  
  2         14  
9 2     2   4677 use HTTP::Tiny;
  2         138556  
  2         100  
10 2     2   3228 use JSON;
  2         30609  
  2         30  
11              
12             # ABSTRACT: Request role for WebService::ForecaseIO
13              
14              
15             has 'base_url' => (
16             is => 'ro',
17             default => sub { "https://api.forecast.io/forecast" },
18             );
19              
20             has 'api_key' => (
21             is => 'ro',
22             required => 1
23             );
24              
25              
26             has 'ua' => (
27             is => 'ro',
28             default => sub {
29             HTTP::Tiny->new(
30             agent => "WebService::ForecastIO/$WebService::ForecastIO::VERSION ",
31             SSL_options => {
32             SSL_hostname => "",
33             SSL_verify_mode => 0
34             },
35             );
36             },
37             lazy => 1,
38             );
39              
40              
41             has 'decoder' => (
42             is => 'ro',
43             default => sub {
44             JSON->new();
45             },
46             lazy => 1,
47             );
48              
49             sub request {
50 0     0 0   my $self = shift;
51              
52 0           my $url = $self->base_url . "/" . $self->api_key . "/" . (join ",", @_);
53              
54 0           my $qp = join "&", (map {; "$_=" . $self->$_() }
55 0           grep {; defined $self->$_() } qw(exclude units));
  0            
56              
57 0 0         if ( $qp ) {
58 0           $url .= "?$qp";
59             }
60              
61 0           my $response = $self->ua->get($url);
62              
63 0 0         if ( $response->{success} ) {
64 0           $self->decoder->decode($response->{content});
65             }
66             else {
67 0           die "Request to $url returned $response->{status}: $response->{content}\n";
68             }
69             }
70              
71             1;
72              
73             __END__