ZooTool/Utils.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 77 CC 3 SCAR 15.5
CC
3
Cov
0%
_fetch
12.0
_hash_to_query_string
2.0
BEGIN
2.0
CRAP 4.7
1 package Net::ZooTool::Utils;
2  
3 use Moose::Role;
4  
5 use JSON::XS;
6 use Carp;
7 use Data::Dumper;
8 use Digest::SHA1 qw/sha1_hex/;
9 use WWW::Curl::Easy;
10  
11 use namespace::autoclean;
12  
13 our $VERSION = '0.003';
14  
15 =head2
16 Fetches data
17 Returns hashref
18 =cut
19  
20 sub _fetch {
21 my $path = shift;
22 my $auth = shift;
23  
24 my $base_url = 'zootool.com/api';
25  
26 my $url = $base_url . $path;
27  
28 my $c = WWW::Curl::Easy->new();
29 $c->setopt( CURLOPT_HEADER, 0 );
30  
31 # HTTP Digest Authentication
32 if ( $path =~ /login=true/ and ( defined($auth) and $auth->user and $auth->password ) ) {
33 $c->setopt(CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
34 $c->setopt(CURLOPT_USERPWD, $auth->user . ':' . sha1_hex($auth->password));
35 }
36  
37 $c->setopt(CURLOPT_USERAGENT, 'Net::ZooTool v0.1');
38  
39 $c->setopt(CURLOPT_URL, $url );
40 $c->setopt(CURLOPT_VERBOSE,0);
41  
42  
43 my $response_body;
44 $c->setopt( CURLOPT_WRITEDATA, \$response_body );
45  
46 # Starts the actual request
47 my $retcode = $c->perform;
48  
49 if ( $retcode == 0 ) {
50 my $response_code = $c->getinfo(CURLINFO_HTTP_CODE);
51 # judge result and next action based on $response_code
52 return JSON::XS->new->utf8->decode($response_body);
53 }
54 else {
55 carp( "An error happened: $retcode "
56 . $c->strerror($retcode) . " "
57 . $c->errbuf
58 . "\n" );
59 }
60 }
61  
62  
63 =head2
64 Transforms hash to query string
65 Returns string
66 =cut
67 sub _hash_to_query_string {
68 my $args = shift;
69  
70 my @params = keys %$args;
71 my $first = shift @params;
72  
73 my $query_string .= "?$first=" . $args->{$first};
74 foreach (@params) {
75 $query_string .= "&$_=" . $args->{$_};
76 }
77  
78 return $query_string;
79 }
80  
81 1;