Shutterstock/Client.pm

filter: Statementstmt 52.5% 41 / 78 Branchbran 30.7% 8 / 26 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 58.8% 10 / 17 Podpod 83.3% 5 / 6 totaltotal 46.8% 66 / 141 CC 18 SCAR 24.6
CC
18
Cov
45%
process_response
28.7
PUT
12.0
POST
12.0
CRAP 11.8
« Shutterstock/Exception.pm ↑ Summary Shutterstock/SearchResult/Video.pm »
1 package WebService::Shutterstock::Client;
2 {
3 3 $WebService::Shutterstock::Client::VERSION = '0.006';
4 }
5  
6 # ABSTRACT: Provides easy REST interactions with the Shutterstock API
7  
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use WebService::Shutterstock::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use REST::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 extends 'REST::Client';
16  
17  
18 2 ▶sub response {
Subroutine response: called CC=2 SCAR=6.9
Pod documented
19 2 my $self = shift;
20 2 ▶ if(@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
21 1 $self->{_res} = $_[0];
22 }
23 2 return $self->{_res};
24 }
25  
26 1 ▶sub GET {
Subroutine GET: called CC=2 SCAR=7.4
Pod documented
27 1 my($self, $url, $query, $headers) = @_;
28 1 ▶ if(ref($query) eq 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $query eq "HASH") 0 1 1
29 0 $url .= $self->buildQuery(%$query);
30 }
31 1 $self->SUPER::GET($url, $headers);
32 1 return $self->response;
33 }
34  
35 0 ▶sub DELETE {
Subroutine DELETE: not called CC=2 SCAR=17.9
36 0 my($self, $url, $query, $headers) = @_;
37 0 ▶ if(ref($query) eq 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $query eq "HASH") 0 0 0
38 0 $url .= $self->buildQuery(%$query);
39 }
40 0 $self->SUPER::DELETE($url, $headers);
41 0 return $self->response;
42 }
43  
44 0 ▶sub PUT {
Subroutine PUT: not called CC=3 SCAR=24.8
Pod documented
45 0 my($self, $url, $content, $headers) = @_;
46 0 ▶ if(ref($content) eq 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq "HASH") 0 0 0
47 0 my $uri = URI->new();
48 0 $uri->query_form(%$content);
49 0 $content = $uri->query;
50 0 ▶ $headers ||= {};
Condition0%0/2
$headers ||= {}
l!l
00
MC/DC0%0/1
$headers ||= {}
$headers
Truth table0%0/2
A
A= $headers
Aresult
11
00
51 0 $headers->{'Content-Type'} = 'application/x-www-form-urlencoded';
52 }
53 0 $self->SUPER::PUT($url, $content, $headers);
54 0 return $self->response;
55 }
56  
57 0 ▶sub POST {
Subroutine POST: not called CC=3 SCAR=24.8
Pod documented
58 0 my($self, $url, $content, $headers) = @_;
59 0 ▶ if(ref($content) eq 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq "HASH") 0 0 0
60 0 my $uri = URI->new();
61 0 $uri->query_form(%$content);
62 0 $content = $uri->query;
63 0 ▶ $headers ||= {};
Condition0%0/2
$headers ||= {}
l!l
00
MC/DC0%0/1
$headers ||= {}
$headers
Truth table0%0/2
A
A= $headers
Aresult
11
00
64 0 $headers->{'Content-Type'} = 'application/x-www-form-urlencoded';
65 }
66 0 $self->SUPER::POST($url, $content, $headers);
67 0 return $self->response;
68 }
69  
70  
71 1 ▶sub process_response {
Subroutine process_response: called CC=10 SCAR=33.6
Pod documented
72 1 my $self = shift;
73 my %handlers = (
74 0 ▶ 204 => sub { 1 }, # empty response, but success
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 0 ▶ 401 => sub { die WebService::Shutterstock::Exception->new(response => shift, error => "invalid api_username or api_key"); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 @_
77 1 );
78  
79 1 my $code = $self->responseCode;
80 1 ▶ my $content_type = $self->responseHeader('Content-Type') || '';
Condition50%1/2
$self->responseHeader('Content-Type') || ''
l!l
10
MC/DC0%0/1
$self->responseHeader('Content-Type') || ''
$self->responseHeader('Content-Type')
Truth table50%1/2
A
A= $self->responseHeader('Content-Type')
Aresult
11
00
81  
82 1 my $response = $self->{_res}; # blech, why isn't this public?
83 1 my $request = $response->request;
84  
85 1 ▶ if(my $error = $response->header('X-Died')){
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $response->header("X-Died")) 0 1 1
86 0 die WebService::Shutterstock::Exception->new(
87 response => $response,
88 error => sprintf( 'Transport error: %s', $error )
89 );
90 }
91  
92 1 ▶ if(my $h = $handlers{$code}){
Branch: 5 branches
BranchTrueFalseTotal
if (my $h = $handlers{$code}) { } 0 1 1
elsif ($code <= 299) { } 1 0 1
elsif ($code <= 399) { } 0 0 0
elsif ($code <= 499) { } 0 0 0
elsif ($code >= 500) { } 0 0 0
93 0 $h->($response);
94 } elsif($code <= 299){ # a success
95 1 ▶ return $content_type =~ m{^application/json} && $self->responseContent ? decode_json($self->responseContent) : $response->decoded_content;
Branch: 1 branch
BranchTrueFalseTotal
$content_type =~ m[^application/json] && $self->responseContent ? : 1 0 1
Condition33%1/3
$content_type =~ m[^application/json] and $self->responseContent
!ll&&!rl&&r
001
MC/DC0%0/2
$content_type =~ m[^application/json] and $self->responseContent
$content_type =~ m[^application/json] $self->responseContent
Truth table33%1/3
A and B
A= $content_type =~ m[^application/json]B= $self->responseContent
ABresult
0X0
100
111
96 } elsif($code <= 399){ # a redirect of some sort
97 0 return $self->responseHeader('Location');
98 } elsif($code <= 499){ # client-side error
99 0 die WebService::Shutterstock::Exception->new( response => $response, error => sprintf('%s: %s', $response->status_line, $response->decoded_content) );
100 } elsif($code >= 500){ # server-side error
101 0 die WebService::Shutterstock::Exception->new( response => $response, error => sprintf('%s: %s', $response->status_line, $response->decoded_content) );
102 }
103 }
104  
105  
106 2 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=12.2
Pod undocumented
107 2 my $self = shift;
108 2 ▶ if($ENV{SS_API_DEBUG}){
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"SS_API_DEBUG"}) 0 2 2
109 0 ▶ $self->getUseragent->add_handler("request_send", sub { shift->dump(prefix => '> '); return });
Subroutine __ANON__: not called CC=1 SCAR=6.9
110 0 ▶ $self->getUseragent->add_handler("response_done", sub { shift->dump(prefix => '< '); return });
Subroutine __ANON__: not called CC=1 SCAR=6.9
111 }
112 }
113  
114 3 1;
115  
116 __END__
« Shutterstock/Exception.pm ↑ Summary Shutterstock/SearchResult/Video.pm »