TMDB/Session.pm

filter: Statementstmt 73.4% 69 / 94 Branchbran 35.0% 14 / 40 Conditioncond 26.3% 5 / 19 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 3 totaltotal 57.4% 104 / 181 CC 29 SCAR 31.0
CC
29
Cov
58%
paginate_results
44.7
_check_status
27.3
talk
23.3
CRAP 22.1
↑ Summary TMDB/TV.pm »
1 package TMDB::Session;
2  
3 #######################
4 # LOAD CORE MODULES
5 #######################
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #######################
11 # LOAD CPAN MODULES
12 #######################
13 3 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Encode qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use HTTP::Tiny qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use URI::Encode qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Params::Validate qw(validate_with :types);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Locale::Codes::Language qw(all_language_codes);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶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 3 our $VERSION = '1.3.0';
25  
26 #######################
27 # PACKAGE VARIABLES
28 #######################
29  
30 # Valid language codes
31 3 my %valid_lang_codes = map { $_ => 1 } all_language_codes('alpha-2');
32  
33 # Default Headers
34 3 my $default_headers = {
35 'Accept' => 'application/json',
36 'Content-Type' => 'application/json',
37 };
38  
39 # Default User Agent
40 3 my $default_ua = 'perl-tmdb-client';
41  
42 #######################
43 # PUBLIC METHODS
44 #######################
45  
46 ## ====================
47 ## Constructor
48 ## ====================
49 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
50 1 my $class = shift;
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 1 ▶ sub { $valid_lang_codes{ lc $_[0] } },
Subroutine __ANON__: called CC=1 SCAR=0.0
68 },
69 },
70 1 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 1 ▶ $opts{lang} = lc $opts{lang} if $opts{lang};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"lang"} 1 0 1
100 1 my $self = $class->SUPER::new(%opts);
101 1 return $self;
102 } ## end sub new
103  
104 ## ====================
105 ## Talk
106 ## ====================
107 75 ▶sub talk {
Subroutine talk: called CC=14 SCAR=31.5
Pod undocumented
108 75 my ( $self, $args ) = @_;
109  
110 # Build Call
111 my $url
112 75 = $self->apiurl . '/' . $args->{method} . '?api_key=' . $self->apikey;
113 # add language by default
114 75 ▶ $args->{params}->{language} = $self->lang unless (exists $args->{params}->{language});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"params"}{"language"} 42 33 75
115 75 ▶ if ( $args->{params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"params"}) 75 0 75
116 75 foreach
117 20 my $param ( sort { lc $a cmp lc $b } keys %{ $args->{params} } )
118 {
119 93 ▶ next unless defined $args->{params}->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"params"}{$param} 0 93 93
120 93 $url .= "&${param}=" . $args->{params}->{$param};
121 } ## end foreach my $param ( sort { ...})
122 } ## end if ( $args->{params} )
123  
124 # Encode
125 75 $url = $self->encoder->encode($url);
126  
127 # Talk
128 75 ▶ warn "DEBUG: GET -> $url\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 75 75
129 75 my $response = $self->client->get($url);
130  
131 # Debug
132 75 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 75 75
133 0 ▶ warn "DEBUG: Got a successful response\n" if $response->{success};
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"success"} 0 0 0
134 0 warn "DEBUG: Got Status -> $response->{status}\n";
135 warn "DEBUG: Got Reason -> $response->{reason}\n"
136 0 ▶ if $response->{reason};
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"reason"} 0 0 0
137 warn "DEBUG: Got Content -> $response->{content}\n"
138 0 ▶ if $response->{content};
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"content"} 0 0 0
139 } ## end if ( $self->debug )
140  
141 # Return
142 75 ▶ return unless $self->_check_status($response);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_status($response) 0 75 75
143 75 ▶ if ( $args->{want_headers} and exists $response->{headers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"want_headers"} and exists $response->{"headers"}) 4 71 75
Condition66%2/3
$args->{"want_headers"} and exists $response->{"headers"}
!ll&&!rl&&r
7104
MC/DC50%1/2
$args->{"want_headers"} and exists $response->{"headers"}
$args->{"want_headers"} exists $response->{"headers"}
Truth table66%2/3
A and B
A= $args->{"want_headers"}B= exists $response->{"headers"}
ABresult
0X0
100
111
144  
145 # Return headers only
146 4 return $response->{headers};
147 } ## end if ( $args->{want_headers...})
148 71 ▶ return unless $response->{content}; # Blank Content
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"content"} 0 71 71
149 return $self->json->decode(
150 71 Encode::decode( 'utf-8-strict', $response->{content} ) ); # Real Response
151 } ## end sub talk
152  
153 ## ====================
154 ## PAGINATE RESULTS
155 ## ====================
156 15 ▶sub paginate_results {
Subroutine paginate_results: called CC=10 SCAR=38.0
Pod undocumented
157 15 my ( $self, $args ) = @_;
158  
159 15 my $response = $self->talk($args);
160 15 ▶ my $results = $response->{results} || [];
Condition50%1/2
$response->{'results'} || []
l!l
150
MC/DC0%0/1
$response->{'results'} || []
$response->{'results'}
Truth table50%1/2
A
A= $response->{'results'}
Aresult
11
00
161  
162 # Paginate
163 15 ▶ if ( $response->{page}
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"page"} and $response->{"total_pages"} and $response->{"total_pages"} > $response->{"page"}) 0 15 15
Condition33%2/6
$response->{"page"} and $response->{"total_pages"} and $response->{"total_pages"} > $response->{"page"}
!ll&&!rl&&r
1500
$response->{"page"} and $response->{"total_pages"}
!ll&&!rl&&r
1500
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 table25%1/4
A and B and C
A= $response->{"page"}B= $response->{"total_pages"}C= $response->{"total_pages"} > $response->{"page"}
ABCresult
0XX0
10X0
1100
1111
164 and $response->{total_pages}
165 and ( $response->{total_pages} > $response->{page} ) )
166 {
167 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
168 0 my $current_page = $response->{page};
169 0 ▶ while ($page_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_limit) 0 0 0
170 0 ▶ last if ( $current_page == $page_limit );
Branch: 1 branch
BranchTrueFalseTotal
if $current_page == $page_limit 0 0 0
171 0 $current_page++;
172 0 $args->{params}->{page} = $current_page;
173 0 my $next_page = $self->talk($args);
174 0 push @$results, @{ $next_page->{results} },;
175 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
176 0 $page_limit--;
177 } ## end while ($page_limit)
178 } ## end if ( $response->{page}...)
179  
180 # Done
181 15 ▶ return @$results if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 15 15
182 15 return $results;
183 } ## end sub paginate_results
184  
185 #######################
186 # INTERNAL
187 #######################
188  
189 # Check Response status
190 75 ▶sub _check_status {
Subroutine _check_status: called CC=6 SCAR=33.1
191 75 my ( $self, $response ) = @_;
192  
193 75 ▶ if ( $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"success"}) 75 0 75
194 75 return 1;
195 }
196  
197 0 ▶ if ( $response->{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"content"}) 0 0 0
198 0 my ( $code, $message );
199 0 my $ok = eval {
200  
201 my $status = $self->json->decode(
202 0 Encode::decode( 'utf-8-strict', $response->{content} ) );
203  
204 0 $code = $status->{status_code};
205 0 $message = $status->{status_message};
206  
207 0 1;
208 };
209  
210 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
211 0 carp sprintf( 'TMDB API Error (%s): %s', $code, $message );
212 }
213 } ## end if ( $response->{content...})
214  
215 0 return;
216 } ## end sub _check_status
217  
218 #######################
219 3 1;
↑ Summary TMDB/TV.pm »