API.pm

filter: Statementstmt 44.9% 40 / 89 Branchbran 5.5% 1 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 52.3% 11 / 21 Podpod 20.0% 2 / 10 totaltotal 37.7% 54 / 143 CC 12 SCAR 24.7
CC
12
Cov
37%
check_parameters
42.0
new
20.0
send_api
6.0
CRAP 11.8
↑ Summary API/Movie.pm »
1 package WWW::TMDB::API;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.04';
9 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our @namespaces = qw( Person Movie );
16 1 for (@namespaces) {
17 2 my $package = __PACKAGE__ . "::$_";
18 2 my $name = "\L$_";
19 0 ▶ eval qq(
Subroutine BEGIN: called CC=1 SCAR=3.6
Subroutine BEGIN: called CC=1 SCAR=3.6
Subroutine api: not called CC=1 SCAR=3.6
Subroutine api: not called CC=1 SCAR=3.6
Subroutine movie: not called CC=2 SCAR=13.2
Subroutine new: not called CC=1 SCAR=3.6
Subroutine new: not called CC=1 SCAR=3.6
Subroutine person: not called CC=2 SCAR=13.2
Pod undocumented
Pod undocumented
Pod documented
Pod undocumented
Pod undocumented
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_movie'}) { } 0 0 0
if ($self->{'_person'}) { } 0 0 0
20 use $package;
21 sub $name {
22 my \$self = shift;
23 if ( \$self->{'_$name'} ) {
24 return \$self->{'_$name'};
25 }else{
26 \$self->{'_$name'} = $package->new( api => \$self );
27 }
28 };
29  
30 package $package;
31 sub api {
32 return shift->{api};
33 };
34  
35 sub new {
36 my ( \$class, \%params ) = \@_;
37 my \$self = bless \\\%params, \$class;
38 \$self->{api} = \$params{api};
39 return \$self;
40 };
41  
42 1;
43 );
44 2 ▶ croak "Cannot create namespace $name: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
45 }
46  
47 0 ▶sub send_api {
Subroutine send_api: not called CC=2 SCAR=17.9
Pod undocumented
48 0 my ( $self, $command, $params_spec, $params ) = @_;
49  
50 0 $self->check_parameters( $params_spec, $params );
51 0 my $url = $self->url( $command, $params );
52 0 my $request = HTTP::Request->new( GET => $url );
53 0 $request->header( 'Accept' => 'application/json' );
54 0 my $json_response = $self->{ua}->request($request);
55 0 ▶ if ( $json_response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json_response->is_success) { } 0 0 0
56 0 return decode_json $json_response->content();
57 }
58 else {
59 0 croak
60 sprintf( "%s returned by %s", $json_response->status_line, $url );
61 }
62 }
63  
64 # Checks items that will be sent to the API($input)
65 # $params - an array that identifies valid parameters
66 # example :
67 # {'ID' => 1 }, 1- field is required, 0- field is optional
68 0 ▶sub check_parameters {
Subroutine check_parameters: not called CC=6 SCAR=37.4
Pod undocumented
69 0 my $self = shift;
70 0 my ( $params, $input ) = @_;
71  
72 0 foreach my $k ( keys(%$params) ) {
73 croak "Required parameter $k missing."
74 0 ▶ if ( $params->{$k} == 1 and !defined $input->{$k} );
Branch: 1 branch
BranchTrueFalseTotal
if $params->{$k} == 1 and not defined $input->{$k} 0 0 0
Condition0%0/3
$params->{$k} == 1 and not defined $input->{$k}
!ll&&!rl&&r
000
MC/DC0%0/2
$params->{$k} == 1 and not defined $input->{$k}
$params->{$k} == 1 not defined $input->{$k}
Truth table0%0/3
A and not(B)
A= $params->{$k} == 1B= not defined $input->{$k}
ABresult
0X0
100
111
75 }
76 0 foreach my $k ( keys(%$input) ) {
77 0 ▶ croak "Unknown parameter - $k." if ( !defined $params->{$k} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{$k} 0 0 0
78 }
79 }
80  
81 0 ▶sub url {
Subroutine url: not called CC=1 SCAR=6.9
Pod undocumented
82 0 my $self = shift;
83 0 my ( $command, $params ) = @_;
84 0 my $url = new URI( $self->{url} );
85 0 $url->path_segments( $self->{ver}, @$command );
86 0 $params->{api_key} = $self->{api_key};
87 0 $url->query_form($params);
88 0 return $url->as_string();
89 }
90  
91 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
92 0 my $class = shift;
93 0 my (%params) = @_;
94  
95 0 ▶ croak "Required parameter api_key not provided." unless $params{api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"api_key"} 0 0 0
96 0 ▶ if ( !defined $params{ua} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $params{'ua'}) { } 0 0 0
97 $params{ua} =
98 0 LWP::UserAgent->new( 'agent' => "Perl-WWW-TMDB-API/$VERSION", );
99 }
100 else {
101 croak "LWP::UserAgent expected."
102 0 ▶ unless $params{ua}->isa('LWP::UserAgent');
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"ua"}->isa("LWP::UserAgent") 0 0 0
103 }
104  
105 my $self = {
106 api_key => $params{api_key},
107 ua => $params{ua},
108 0 ver => '3',
109 url => 'http://api.themoviedb.org',
110 };
111  
112 0 bless $self, $class;
113 0 return $self;
114 }
115  
116 =head1 NAME
117  
118 WWW::TMDB::API - TMDb API (http://api.themoviedb.org) client
119  
120 =head1 VERSION
121  
122 Version 0.04
123  
124 =head1 SYNOPSIS
125  
126 use WWW::TMDB::API;
127  
128 # The constructor has 2 parameters - the api_key and the optional LWP::UserAgent object, ua.
129 my $tmdb_client = WWW::TMDB::API->new( 'api_key' => 'your tmdb api key' );
130  
131 # Retrieve information about the person with ID == 287
132 $tmdb_client->person->info( ID => 287 );
133  
134 # Searches the themoviedb.org database for an actor, actress or production member with name 'Brad+Pitt'
135 $tmdb_client->person->search( query => 'Brad+Pitt' );
136  
137 # Searches the themoviedb.org database for an actor, actress or production member with name 'Brad'
138 $tmdb_client->person->search( query => 'Brad' );
139  
140 # Determines the last movie created in the themoviedb.org database.
141 $tmdb_client->movie->latest();
142  
143  
144 =head1 DESCRIPTION
145  
146 This module implements version 3 of the TMDb API. See L<http://help.themoviedb.org/kb/api/about-3> for the documentation.
147 The module uses the same parameter names used by the API.
148 The method names have been slightly changed. Here's the mapping of the method names used by this this module and the corresponding method names in the TMDb API:
149  
150 TMDB API WWW::TMDB::API
151 ---------------------- -------------------
152 Search Movies search/movie movie->search()
153 Search People search/person person->search()
154 Movie Info movie/[TMDb ID] movie->info()
155 Movie Alternative Titles movie/[TMDb ID]/alternative_titles movie->alternative_titles()
156 Movie Casts movie/[TMDb ID]/casts movie->casts()
157 Movie Images movie/[TMDb ID]/images movie->images()
158 Movie Keywords movie/[TMDb ID]/keywords movie->keywords()
159 Movie Release Info movie/[TMDb ID]/releases movie->releases()
160 Movie Trailers movie/[TMDb ID]/trailers movie->trailers()
161 Movie Translations movie/[TMDb ID]/translations movie->translations()
162 Person Info person/[TMDb ID]/info person->info()
163 Person Credits person/[TMDb ID]/credits person->credits()
164 Person Images person/[TMDb ID]/images person->images()
165 Latest Movie latest/movie movie->latest()
166  
167  
168 The API requires an API key which can be generated from http://api.themoviedb.org.
169 This module converts the API output to Perl data structure using the module JSON.
170 This module does not support update the method, Movie Add Rating.
171  
172 =head1 SUBROUTINES/METHODS
173  
174 =head2 new( %params )
175  
176 Returns a new instance of the B<WWW::TMDB::API> class.
177  
178 =over 4
179  
180 =item * B<api_key>
181  
182 Required. This is the TMDb API key. Go to the L<http://api.themoviedb.org> to signup and generate an API key.
183  
184 =item * B<ua>
185  
186 Optional. The LWP::UserAgent used to communicate with the TMDb server.
187  
188  
189 my $tmdb_client = WWW::TMDB::API->new( 'api_key' => 'your tmdb api key' );
190  
191 require LWP::UserAgent;
192 $ua = LWP::UserAgent->new(
193 'agent' => "Perl-WWW-TMDB-API",
194 );
195  
196 my $tmdb_client =
197 WWW::TMDB::API->new( 'api_key' => 'your tmdb api key', 'ua' => $ua );
198  
199  
200 =back
201  
202 =head2 movie->search( %params )
203  
204 Searches for movies.
205  
206 =over 4
207  
208 =item * B<query>
209  
210 Required. This is the search text. The query can include the year the movie was released (e.g. B<Transformers+2007>) to narrow the search results.
211  
212 =item * B<page>
213  
214 Optional. Use this parameter to iterate through the search results. Search results that exceed 20 items are paginated.
215  
216 =item * B<language>
217  
218 Optional. This limits the result to items tagged with the specified language. The expected value is a ISO 639-1 code.
219  
220 =item * B<include_adult>
221  
222 Optional. [true/false, defaults to false if unspecified]. Set this to true to include adult items in the search results.
223  
224 =back
225 $result = $api->movie->search( 'query' => 'Cool Hand' );
226  
227  
228 =head2 person->search( %params )
229  
230 Searches for actors, actresses, or production members.
231  
232 =over 4
233  
234 =item * B<query>
235  
236 Required. This is the search text.
237  
238  
239 =item * B<page>
240  
241 Optional. Use this parameter to iterate through the search results. Search results that exceed 20 items are paginated.
242  
243 =back
244  
245 $result = $api->person->search( 'query' => 'Newman' );
246  
247  
248 =head2 movie->info( %params )
249  
250 Retrieves basic information about a movie.
251 Building image urls from the file_paths returned by this method is discussed in the document L<http://help.themoviedb.org/kb/api/configuration>.
252  
253 =over 4
254  
255 =item * B<ID>
256  
257 Required. The TMDb ID of the movie.
258  
259 =item * B<language>
260  
261 Optional. This limits the result to items tagged with the specified language. The expected value is a ISO 639-1 code.
262  
263 =back
264  
265 $result = $api->movie->info( ID => 903 );
266  
267  
268 =head2 movie->alternative_titles( %params )
269  
270 Retrieves a movie's alternative titles.
271  
272 =over 4
273  
274 =item * B<ID>
275  
276 Required. The TMDb ID of the movie.
277  
278 =item * B<country>
279  
280 Optional. This limits the result to items tagged with the specified country. The expected value is a ISO 3166-1 code.
281  
282 =back
283  
284 $result = $api->movie->alternative_titles( ID => 903 );
285 $result = $api->movie->alternative_titles( ID => 903, 'language' => 'fr' );
286  
287  
288 =head2 movie->casts( %params )
289  
290 Retrieves a movie's cast information.
291  
292 =over 4
293  
294 =item * B<ID>
295  
296 Required. The TMDb ID of the movie.
297  
298 =back
299  
300 $result = $api->movie->casts( ID => 903 );
301  
302  
303 =head2 movie->images( %params )
304  
305 Retrieves all of the images for a particular movie.
306 Building image urls from the file_paths returned by this method is discussed in the document:
307 L<http://help.themoviedb.org/kb/api/configuration>
308  
309 =over 4
310  
311 =item * B<ID>
312  
313 Required. The TMDb ID of the movie.
314  
315 =item * B<language>
316  
317 Optional. This limits the result to items tagged with the specified language. The expected value is a ISO 639-1 code.
318  
319 =back
320  
321 $result = $api->movie->images( ID => 903 );
322 $result = $api->movie->images( ID => 903, 'language' => 'en' );
323  
324  
325 =head2 movie->keywords( %params )
326  
327 Retrieves the keywords for a movie.
328  
329 =over 4
330  
331 =item * B<ID>
332  
333 Required. The TMDb ID of the movie.
334  
335 =back
336  
337 $result = $api->movie->keywords( ID => 903 );
338  
339 =head2 movie->releases( %params )
340  
341 Retrieves release and certification data for a specific movie.
342  
343 =over 4
344  
345 =item * B<ID>
346  
347 Required. The TMDb ID of the movie.
348  
349 =back
350  
351 $result = $api->movie->releases( ID => 903 );
352  
353  
354 =head2 movie->trailers( %params )
355  
356 Retrieves the trailers for a movie.
357  
358 =over 4
359  
360 =item * B<ID>
361  
362 Required. The TMDb ID of the movie.
363  
364 =item * B<language>
365  
366 Optional. This limits the result to items tagged with the specified language. The expected value is a ISO 639-1 code.
367  
368 =back
369  
370 $result = $api->movie->trailers( ID => 903 );
371  
372  
373 =head2 movie->translations( %params )
374  
375 Retrieves the list of translations for a movie.
376  
377 =over 4
378  
379 =item * B<ID>
380  
381 Required. The TMDb ID of the movie.
382  
383 =back
384  
385 $result = $api->movie->translations( ID => 903 );
386  
387  
388 =head2 person->info( %params )
389  
390 Retrieves basic information about a person.
391 Building image urls from the file_paths returned by this method is discussed in the document:
392 L<http://help.themoviedb.org/kb/api/configuration>
393  
394 =over 4
395  
396 =item * B<ID>
397  
398 Required. The TMDb ID of the person.
399  
400 =back
401  
402 $result = $api->person->info( ID => 3636 );
403  
404 =head2 person->credits( %params )
405  
406 Retrieves the movies that have cast or crew credits for a person.
407  
408 =over 4
409  
410 =item * B<ID>
411  
412 Required. The TMDb ID of the person.
413  
414 =item * B<language>
415  
416 Optional. This limits the result to items tagged with the specified language. The expected value is a ISO 639-1 code.
417  
418 =back
419  
420 $result = $api->person->credits( ID => 3636 );
421  
422  
423 =head2 person->images( %params )
424  
425 Retrieves all the profile images of the person.
426 Building image urls from the file_paths returned by this method is discussed in the document:
427 L<http://help.themoviedb.org/kb/api/configuration>]
428  
429 =over 4
430  
431 =item * B<ID>
432  
433 Required. The TMDb ID of the person.
434  
435 =back
436  
437 $result = $api->person->images( ID => 3636 );
438  
439  
440 =head2 movie->latest( )
441  
442 Returns the newest movie created in the themoviedb.org database.
443  
444  
445 =head1 AUTHOR
446  
447 Maria Celina Baratang, C<< <maria at zambale.com> >>
448  
449 =head1 BUGS
450  
451 Please report any bugs or feature requests to C<bug-www-tmdb-api at rt.cpan.org>, or through
452 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-TMDB-API>. I will be notified, and then you'll
453 automatically be notified of progress on your bug as I make changes.
454  
455  
456 =head1 SUPPORT
457  
458 You can find documentation for this module with the perldoc command.
459  
460 perldoc WWW::TMDB::API
461  
462  
463 You can also look for information at:
464  
465  
466 =over 4
467  
468 =item * TMDb The open movie database
469  
470 L<http://themoviedb.org/>
471  
472 =item * themoviedb.org API Documentation
473  
474 L<http://api.themoviedb.org/>
475 L<http://help.themoviedb.org/kb/api/about-3>
476  
477 =item * RT: CPAN's request tracker (report bugs here)
478  
479 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-TMDB-API>
480  
481 =item * AnnoCPAN: Annotated CPAN documentation
482  
483 L<http://annocpan.org/dist/WWW-TMDB-API>
484  
485 =item * CPAN Ratings
486  
487 L<http://cpanratings.perl.org/d/WWW-TMDB-API>
488  
489 =item * Search CPAN
490  
491 L<http://search.cpan.org/dist/WWW-TMDB-API/>
492  
493 =back
494  
495  
496 =head1 ACKNOWLEDGEMENTS
497  
498 =head1 LICENSE AND COPYRIGHT
499  
500 Copyright 2012 Maria Celina Baratang.
501  
502 This program is free software; you can redistribute it and/or modify it
503 under the terms of either: the GNU General Public License as published
504 by the Free Software Foundation; or the Artistic License.
505  
506 See http://dev.perl.org/licenses/ for more information.
507  
508  
509 =cut
510  
511 1 1; # End of WWW::TMDB::API
512  
↑ Summary API/Movie.pm »