File Coverage

blib/lib/WWW/Moviepilot/Movie.pm
Criterion Covered Total %
statement 21 65 32.3
branch 0 12 0.0
condition 0 9 0.0
subroutine 7 14 50.0
pod 6 6 100.0
total 34 106 32.0


line stmt bran cond sub pod time code
1             package WWW::Moviepilot::Movie;
2              
3 1     1   8 use warnings;
  1         2  
  1         39  
4 1     1   7 use strict;
  1         2  
  1         37  
5              
6 1     1   6 use Carp;
  1         2  
  1         128  
7 1     1   8 use JSON::Any;
  1         2  
  1         27  
8 1     1   177 use URI;
  1         3  
  1         22  
9 1     1   6 use URI::Escape;
  1         3  
  1         74  
10              
11 1     1   620 use WWW::Moviepilot::Person;
  1         3  
  1         655  
12              
13             =head1 NAME
14              
15             WWW::Moviepilot::Movie - Handle moviepilot.de movies
16              
17             =head1 SYNOPSIS
18              
19             my $movie = WWW::Moviepilot->new(...)->movie( 'matrix' );
20              
21             # all fields
22             my @fields = $movie->fields;
23              
24             # direct access to fields
25             print $movie->display_title; # "Matrix"
26             print $movie->title; # field does not exist => undef
27              
28             # *_lists in scalar context
29             print scalar $movie->emotions_list; # "Spannend,Aufregend"
30              
31             # *_lists in list context
32             print join ' +++ ', $movie->emotions_list # "Spannend +++ Aufregend"
33              
34             =head1 METHODS
35              
36             =head2 new
37              
38             Creates a blank WWW::Moviepilot::Movie object.
39              
40             my $movie = WWW::Moviepilot::Movie->new;
41              
42             =cut
43              
44             sub new {
45 0     0 1   my ($class, $args) = @_;
46 0           my $self = bless {
47             cast => [],
48             data => {},
49             name => undef,
50             m => $args->{m}
51             } => $class;
52 0           return $self;
53             }
54              
55             =head2 populate( $args )
56              
57             Populates an object with data, you should not use this directly.
58              
59             =cut
60              
61             sub populate {
62 0     0 1   my ($self, $args) = @_;
63 0           $self->{data} = $args->{data};
64 0 0         if ( $self->restful_url ) {
65 0           ($self->{name}) = $self->restful_url =~ m{/([^/]+)$};
66             }
67             }
68              
69             =head2 character
70              
71             If used together with a filmography search, you get the name of the character
72             the person plays in the movie.
73              
74             my @filmography = $person->filmography;
75             foreach my $movie (@filmography) {
76             printf "%s plays %s\n", $person->last_name, $movie->character;
77             }
78              
79             =cut
80              
81             sub character {
82 0     0 1   my $self = shift;
83 0           return $self->{data}{character};
84             }
85              
86             =head2 name
87              
88             Returns the internal moviepilot name for the movie.
89              
90             my @movies = WWW::Moviepilot->new(...)->search_movie( 'matrix' );
91             foreach my $movie (@movies) {
92             print $movie->name;
93             }
94             __END__
95             matrix
96             armitage-iii-dual-matrix
97             the-matrix-reloaded
98             the-matrix-revolutions
99             madrid
100             mourir-a-madrid
101             die-sieben-kleider-der-katrin
102             super-mario-bros
103             armitage-iii-polymatrix
104             rendezvous-in-madrid
105             herr-puntila-und-sein-knecht-matti
106             drei-maedchen-in-madrid
107             zwischen-madrid-und-paris
108             marie-antoinette-2
109             mario-und-der-zauberer
110             bezaubernde-marie-2
111             marie-lloyd
112             marie-line
113             marie-antoinette-3
114             maria-magdalena
115              
116             =cut
117              
118             sub name {
119 0     0 1   my $self = shift;
120 0           return $self->{name};
121             }
122              
123             =head2 cast
124              
125             Returns the cast for the movie.
126              
127             my $movie = WWW::Moviepilot->new(...)->movie(...);
128             my @cast = $movie->cast;
129              
130             Returned is a list of L objects.
131              
132             =cut
133              
134             sub cast {
135 0     0 1   my ($self, $movie) = @_;
136              
137             # we have already a cast
138 0 0         if ( @{ $self->{cast} } ) {
  0            
139 0           return @{ $self->{cast} };
  0            
140             }
141              
142 0 0 0       if ( !$movie && !$self->name ) {
143 0           croak "no movie name provided, can't fetch cast";
144             }
145              
146 0   0       $movie ||= $self->name;
147              
148 0           my $uri = URI->new( $self->{m}->host . '/movies/' . uri_escape($movie) . '/casts.json' );
149 0           $uri->query_form( api_key => $self->{m}->api_key );
150              
151 0           my $res = $self->{m}->ua->get( $uri->as_string );
152 0 0         if ( $res->is_error ) {
153 0           croak $res->status_line;
154             }
155              
156 0           my $o = JSON::Any->from_json( $res->decoded_content );
157 0           foreach my $entry ( @{ $o->{movies_people} } ) {
  0            
158 0           my $person = WWW::Moviepilot::Person->new({ m => $self->{m} });
159 0           $person->populate({ data => $entry });
160 0           push @{ $self->{cast} }, $person;
  0            
161             }
162              
163 0           return @{ $self->{cast} };
  0            
164             }
165              
166             =head2 fields
167              
168             Returns a list with all fields for this movie.
169              
170             my @fields = $movie->fields;
171              
172             # print all fields
173             foreach my $field ( @fields ) {
174             printf "%s: %s\n", $field. $movie->$field;
175             }
176              
177             As of 2009-10-13, these fields are supported:
178              
179             =over 4
180              
181             =item * alternative_identifiers
182              
183             =item * average_community_rating
184              
185             =item * average_critics_rating
186              
187             =item * cinema_start_date
188              
189             =item * countries_list
190              
191             =item * display_title
192              
193             =item * dvd_start_date
194              
195             =item * emotions_list
196              
197             =item * genres_list
198              
199             =item * homepage
200              
201             =item * long_description
202              
203             =item * on_tv
204              
205             =item * places_list
206              
207             =item * plots_list
208              
209             =item * poster
210              
211             =item * premiere_date
212              
213             =item * production_year
214              
215             =item * restful_url
216              
217             =item * runtime
218              
219             =item * short_description
220              
221             =item * times_list
222              
223             =back
224              
225             =cut
226              
227             sub fields {
228 0     0 1   my $self = shift;
229 0           return keys %{ $self->{data}{movie} };
  0            
230             }
231              
232             our $AUTOLOAD;
233             sub AUTOLOAD {
234 0     0     my $self = shift;
235 0           my $field = $AUTOLOAD;
236 0           $field =~ s/.*://;
237 0 0         if ( !exists $self->{data}{movie}{$field} ) {
238 0           return;
239             }
240              
241 0 0 0       if ( $field =~ /_list$/ && wantarray ) {
242 0           return split /,/, $self->{data}{movie}{$field};
243             }
244              
245 0           return $self->{data}{movie}{$field};
246             }
247              
248             1;
249             __END__