blib/lib/TMDB/Session.pm

filter: Statementstmt 38.7% 36 / 93 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 66.6% 10 / 15 Podpod 0.0% 0 / 3 totaltotal 25.8% 46 / 178 SCAR 59.2
CC
28
Cov
24%
talk
182.0
paginate_results
110.0
_check_status
42.0
CRAP 372.2
« blib/lib/TMDB/Search.pm ↑ Summary blib/lib/TMDB/Person.pm »
1 package TMDB::Session;
2  
3 #######################
4 # LOAD CORE MODULES
5 #######################
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #######################
11 # LOAD CPAN MODULES
12 #######################
13 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Encode qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use HTTP::Tiny qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use URI::Encode qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Params::Validate qw(validate_with :types);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Locale::Codes::Language qw(all_language_codes);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Object::Tiny qw(apikey apiurl lang debug client encoder json);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 #######################
22 # VERSION
23 #######################
24 1 our $VERSION = '1.2.1';
25  
26 #######################
27 # PACKAGE VARIABLES
28 #######################
29  
30 # Valid language codes
31 1 my %valid_lang_codes = map { $_ => 1 } all_language_codes('alpha-2');
32  
33 # Default Headers
34 1 my $default_headers = {
35 'Accept' => 'application/json',
36 'Content-Type' => 'application/json',
37 };
38  
39 # Default User Agent
40 1 my $default_ua = 'perl-tmdb-client';
41  
42 #######################
43 # PUBLIC METHODS
44 #######################
45  
46 ## ====================
47 ## Constructor
48 ## ====================
49 sub new {
50 0 ▶ my $class = shift;
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
51 my %opts = validate_with(
52 params => \@_,
53 spec => {
54 apikey => {
55 type => SCALAR,
56 },
57 apiurl => {
58 type => SCALAR,
59 optional => 1,
60 default => 'https://api.themoviedb.org/3',
61 },
62 lang => {
63 type => SCALAR,
64 optional => 1,
65 callbacks => {
66 'valid language code' =>
67 0 ▶ sub { $valid_lang_codes{ lc $_[0] } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 },
69 },
70 0 client => {
71 type => OBJECT,
72 isa => 'HTTP::Tiny',
73 optional => 1,
74 default => HTTP::Tiny->new(
75 agent => $default_ua,
76 default_headers => $default_headers,
77 ),
78 },
79 encoder => {
80 type => OBJECT,
81 isa => 'URI::Encode',
82 optional => 1,
83 default => URI::Encode->new(),
84 },
85 json => {
86 type => OBJECT,
87 can => [qw(decode)],
88 optional => 1,
89 default => JSON::MaybeXS->new(),
90 },
91 debug => {
92 type => BOOLEAN,
93 optional => 1,
94 default => 0,
95 },
96 },
97 );
98  
99 0 ▶ $opts{lang} = lc $opts{lang} if $opts{lang};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"lang"} 0 0 0
100 0 my $self = $class->SUPER::new(%opts);
101 0 return $self;
102 } ## end sub new
103  
104 ## ====================
105 ## Talk
106 ## ====================
107 sub talk {
108 0 ▶ my ( $self, $args ) = @_;
Subroutine talk: not called CC=13 SCAR=52.0
Pod undocumented
109  
110 # Build Call
111 my $url
112 0 = $self->apiurl . '/' . $args->{method} . '?api_key=' . $self->apikey;
113 0 ▶ if ( $args->{params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"params"}) 0 0 0
114 0 foreach
115 0 my $param ( sort { lc $a cmp lc $b } keys %{ $args->{params} } )
116 {
117 0 ▶ next unless defined $args->{params}->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"params"}{$param} 0 0 0
118 0 $url .= "&${param}=" . $args->{params}->{$param};
119 } ## end foreach my $param ( sort { ...})
120 } ## end if ( $args->{params} )
121  
122 # Encode
123 0 $url = $self->encoder->encode($url);
124  
125 # Talk
126 0 ▶ warn "DEBUG: GET -> $url\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
127 0 my $response = $self->client->get($url);
128  
129 # Debug
130 0 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
131 0 ▶ warn "DEBUG: Got a successful response\n" if $response->{success};
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"success"} 0 0 0
132 0 warn "DEBUG: Got Status -> $response->{status}\n";
133 warn "DEBUG: Got Reason -> $response->{reason}\n"
134 0 ▶ if $response->{reason};
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"reason"} 0 0 0
135 warn "DEBUG: Got Content -> $response->{content}\n"
136 0 ▶ if $response->{content};
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"content"} 0 0 0
137 } ## end if ( $self->debug )
138  
139 # Return
140 0 ▶ return unless $self->_check_status($response);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_status($response) 0 0 0
141 0 ▶ if ( $args->{want_headers} and exists $response->{headers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"want_headers"} and exists $response->{"headers"}) 0 0 0
Condition0%0/3
$args->{"want_headers"} and exists $response->{"headers"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"want_headers"} and exists $response->{"headers"}
$args->{"want_headers"} exists $response->{"headers"}
Truth table0%0/3
A and B
A= $args->{"want_headers"}B= exists $response->{"headers"}
ABresult
0X0
100
111
142  
143 # Return headers only
144 0 return $response->{headers};
145 } ## end if ( $args->{want_headers...})
146 0 ▶ return unless $response->{content}; # Blank Content
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"content"} 0 0 0
147 return $self->json->decode(
148 0 Encode::decode( 'utf-8-strict', $response->{content} ) ); # Real Response
149 } ## end sub talk
150  
151 ## ====================
152 ## PAGINATE RESULTS
153 ## ====================
154 sub paginate_results {
155 0 ▶ my ( $self, $args ) = @_;
Subroutine paginate_results: not called CC=10 SCAR=47.0
Pod undocumented
156  
157 0 my $response = $self->talk($args);
158 0 ▶ my $results = $response->{results} || [];
Condition0%0/2
$response->{'results'} || []
l!l
00
MC/DC0%0/1
$response->{'results'} || []
$response->{'results'}
Truth table0%0/2
A
A= $response->{'results'}
Aresult
11
00
159  
160 # Paginate
161 0 ▶ if ( $response->{page}
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"page"} and $response->{"total_pages"} and $response->{"total_pages"} > $response->{"page"}) 0 0 0
Condition0%0/6
$response->{"page"} and $response->{"total_pages"} and $response->{"total_pages"} > $response->{"page"}
!ll&&!rl&&r
000
$response->{"page"} and $response->{"total_pages"}
!ll&&!rl&&r
000
MC/DC0%0/3
$response->{"page"} and $response->{"total_pages"} and $response->{"total_pages"} > $response->{"page"}
$response->{"page"} $response->{"total_pages"} $response->{"total_pages"} > $response->{"page"}
Truth table0%0/4
A and B and C
A= $response->{"page"}B= $response->{"total_pages"}C= $response->{"total_pages"} > $response->{"page"}
ABCresult
0XX0
10X0
1100
1111
162 and $response->{total_pages}
163 and ( $response->{total_pages} > $response->{page} ) )
164 {
165 0 ▶ my $page_limit = $args->{max_pages} || '1';
Condition0%0/2
$args->{'max_pages'} || '1'
l!l
00
MC/DC0%0/1
$args->{'max_pages'} || '1'
$args->{'max_pages'}
Truth table0%0/2
A
A= $args->{'max_pages'}
Aresult
11
00
166 0 my $current_page = $response->{page};
167 0 ▶ while ($page_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_limit) 0 0 0
168 0 ▶ last if ( $current_page == $page_limit );
Branch: 1 branch
BranchTrueFalseTotal
if $current_page == $page_limit 0 0 0
169 0 $current_page++;
170 0 $args->{params}->{page} = $current_page;
171 0 my $next_page = $self->talk($args);
172 0 push @$results, @{ $next_page->{results} },;
173 0 ▶ last if ( $next_page->{page} == $next_page->{total_pages} );
Branch: 1 branch
BranchTrueFalseTotal
if $next_page->{"page"} == $next_page->{"total_pages"} 0 0 0
174 0 $page_limit--;
175 } ## end while ($page_limit)
176 } ## end if ( $response->{page}...)
177  
178 # Done
179 0 ▶ return @$results if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
180 0 return $results;
181 } ## end sub paginate_results
182  
183 #######################
184 # INTERNAL
185 #######################
186  
187 # Check Response status
188 sub _check_status {
189 0 ▶ my ( $self, $response ) = @_;
Subroutine _check_status: not called CC=6 SCAR=37.4
190  
191 0 ▶ if ( $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"success"}) 0 0 0
192 0 return 1;
193 }
194  
195 0 ▶ if ( $response->{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"content"}) 0 0 0
196 0 my ( $code, $message );
197 0 my $ok = eval {
198  
199 my $status = $self->json->decode(
200 0 Encode::decode( 'utf-8-strict', $response->{content} ) );
201  
202 0 $code = $status->{status_code};
203 0 $message = $status->{status_message};
204  
205 0 1;
206 };
207  
208 0 ▶ if ( $ok and $code and $message ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok and $code and $message) 0 0 0
Condition0%0/6
$ok and $code and $message
!ll&&!rl&&r
000
$ok and $code
!ll&&!rl&&r
000
MC/DC0%0/3
$ok and $code and $message
$ok $code $message
Truth table0%0/4
A and B and C
A= $okB= $codeC= $message
ABCresult
0XX0
10X0
1100
1111
209 0 carp sprintf( 'TMDB API Error (%s): %s', $code, $message );
210 }
211 } ## end if ( $response->{content...})
212  
213 0 return;
214 } ## end sub _check_status
215  
216 #######################
217 1 1;
« blib/lib/TMDB/Search.pm ↑ Summary blib/lib/TMDB/Person.pm »