File Coverage

blib/lib/WebService/IMDB/Name.pm
Criterion Covered Total %
statement 162 209 77.5
branch 36 48 75.0
condition n/a
subroutine 46 48 95.8
pod 14 14 100.0
total 258 319 80.8


line stmt bran cond sub pod time code
1             # $Id: Name.pm 7370 2012-04-09 01:17:33Z chris $
2              
3             =head1 NAME
4              
5             WebService::IMDB::Name
6              
7             =cut
8              
9             package WebService::IMDB::Name;
10              
11 2     2   13 use strict;
  2         4  
  2         78  
12 2     2   11 use warnings;
  2         5  
  2         92  
13              
14             our $VERSION = '0.05';
15              
16 2     2   11 use base qw(WebService::IMDB::Base);
  2         4  
  2         174  
17              
18 2     2   13 use Carp;
  2         3  
  2         168  
19             our @CARP_NOT = qw(WebService::IMDB);
20              
21 2     2   14 use HTTP::Request::Common;
  2         4  
  2         167  
22              
23 2     2   13 use URI;
  2         3  
  2         63  
24              
25 2     2   1189 use WebService::IMDB::Birth;
  2         4  
  2         25  
26 2     2   1502 use WebService::IMDB::Death;
  2         5  
  2         19  
27 2     2   1307 use WebService::IMDB::News;
  2         7  
  2         15  
28 2     2   1354 use WebService::IMDB::KnownFor;
  2         7  
  2         16  
29 2     2   1304 use WebService::IMDB::Trivium;
  2         5  
  2         16  
30 2     2   1061 use WebService::IMDB::WhereNow;
  2         5  
  2         16  
31              
32             __PACKAGE__->mk_accessors(qw(
33             __birth
34             __death
35             __image
36             __known_for
37             __news
38             __photos
39             __trivia
40             __where_now
41             ));
42              
43             use constant {
44 2         4753 PAGE_MAINDETAILS => 1,
45             PAGE_NEWS => 2,
46             PAGE_PHOTOS => 3,
47             PAGE_QUOTES => 4,
48             PAGE_TRIVIA => 5,
49              
50             PAGE_LAST => 5,
51 2     2   100 };
  2         4  
52              
53              
54             =head1 METHODS
55              
56             =head2 nconst
57              
58             =head2 aka
59              
60             =head2 bio
61              
62             =head2 birth
63              
64             =head2 death
65              
66             =head2 image
67              
68             =head2 known_for
69              
70             =head2 name
71              
72             =head2 news
73              
74             =head2 photos
75              
76             =head2 quotes
77              
78             =head2 real_name
79              
80             =head2 trivia
81              
82             =head2 where_now
83              
84             =cut
85              
86              
87             ################################
88             #
89             # Primary properties
90             #
91             ################################
92              
93             sub _url {
94 11     11   22 my $self = shift;
95 11         23 my $page = shift;
96              
97 11         104 my $uri = URI->new();
98 11         550 $uri->scheme("http");
99 11         5403 $uri->host($self->_domain());
100 11 100       1012 if ($page == PAGE_MAINDETAILS) {
    100          
    100          
    100          
    50          
101 6         32 $uri->path(sprintf("/name/%s/maindetails", $self->_id()));
102             } elsif ($page == PAGE_NEWS) {
103 1         7 $uri->path(sprintf("/name/%s/news", $self->_id()));
104             } elsif ($page == PAGE_PHOTOS) {
105 1         7 $uri->path("/name/photos");
106 1         30 $uri->query_form('nconst' => $self->_id());
107             } elsif ($page == PAGE_QUOTES) {
108 1         6 $uri->path("/name/quotes");
109 1         33 $uri->query_form('nconst' => $self->_id());
110             } elsif ($page == PAGE_TRIVIA) {
111 2         10 $uri->path("/name/trivia");
112 2         62 $uri->query_form('nconst' => $self->_id());
113             }
114              
115 11         630 return $uri->as_string();
116             }
117              
118              
119             sub nconst {
120 5     5 1 5417 my $self = shift;
121 5         33 return $self->_content(PAGE_MAINDETAILS)->{'nconst'};
122             }
123              
124             sub aka {
125 3     3 1 10 my $self = shift;
126 3 50       14 if (exists $self->_content(PAGE_MAINDETAILS)->{'aka'}) {
127 3         13 return $self->_content(PAGE_MAINDETAILS)->{'aka'};
128             } else {
129 0         0 return [];
130             }
131             }
132              
133             sub bio {
134 1     1 1 3 my $self = shift;
135 1 50       5 if (exists $self->_content(PAGE_MAINDETAILS)->{'bio'}) {
136 1         5 return $self->_content(PAGE_MAINDETAILS)->{'bio'};
137             } else {
138 0         0 return undef;
139             }
140             }
141              
142             sub birth {
143 3     3 1 1054 my $self = shift;
144 3         10 return $self->_birth();
145             }
146              
147             sub death {
148 4     4 1 1436 my $self = shift;
149 4         11 return $self->_death();
150             }
151              
152             sub image {
153 1     1 1 444 my $self = shift;
154 1         7 return $self->_image();
155             }
156              
157             sub known_for {
158 7     7 1 3177 my $self = shift;
159 7         22 return $self->_known_for()
160             }
161              
162             sub name {
163 3     3 1 9 my $self = shift;
164 3         19 return $self->_content(PAGE_MAINDETAILS)->{'name'};
165             }
166              
167             sub news {
168 3     3 1 1547 my $self = shift;
169 3         13 return $self->_news();
170             }
171              
172             sub photos {
173 3     3 1 1881 my $self = shift;
174 3         12 return $self->_photos();
175             }
176              
177             sub quotes {
178 1     1 1 456 my $self = shift;
179 1 50       11 if (exists $self->_content(PAGE_QUOTES)->{'quotes'}) { # TODO: Think about how to handle this best.
180 1         4 return [ @{$self->_content(PAGE_QUOTES)->{'quotes'}} ];
  1         5  
181             } else {
182 0         0 return [];
183             }
184             }
185              
186             sub real_name {
187 1     1 1 3 my $self = shift;
188 1 50       6 if (exists $self->_content(PAGE_MAINDETAILS)->{'real_name'}) {
189 1         5 return $self->_content(PAGE_MAINDETAILS)->{'real_name'};
190             } else {
191 0         0 return undef;
192             }
193             }
194              
195             sub trivia {
196 2     2 1 819 my $self = shift;
197 2         10 return $self->_trivia();
198             }
199              
200             sub where_now {
201 2     2 1 1358 my $self = shift;
202 2         12 return $self->_where_now();
203             }
204              
205              
206             ################################
207             #
208             # Caching accessors
209             #
210             ################################
211              
212             sub _flush {
213 0     0   0 my $self = shift;
214              
215 0         0 $self->SUPER::_flush();
216              
217 0         0 $self->__birth(undef);
218 0         0 $self->__death(undef);
219 0         0 $self->__image(undef);
220 0         0 $self->__known_for(undef);
221 0         0 $self->__news(undef);
222 0         0 $self->__photos(undef);
223 0         0 $self->__trivia(undef);
224 0         0 $self->__where_now(undef);
225             }
226              
227             sub _birth {
228 3     3   5 my $self = shift;
229              
230 3 100       12 if (!defined $self->__birth()) { $self->__birth([$self->_get_birth()]); } # Wrap in array, because we need to cache undef too
  1         50  
231 3         34 return $self->__birth()->[0];
232             }
233              
234             sub _death {
235 4     4   9 my $self = shift;
236              
237 4 100       17 if (!defined $self->__death()) { $self->__death([$self->_get_death()]); } # Wrap in array, because we need to cache undef too
  1         18  
238 4         47 return $self->__death()->[0];
239             }
240              
241             sub _image {
242 1     1   3 my $self = shift;
243              
244 1 50       8 if (!defined $self->__image()) { $self->__image([$self->_get_image()]); } # Wrap in array, because we need to cache undef too
  1         18  
245 1         14 return $self->__image()->[0];
246             }
247              
248             sub _known_for {
249 7     7   9 my $self = shift;
250              
251 7 100       25 if (!defined $self->__known_for()) { $self->__known_for($self->_get_known_for()); }
  1         19  
252 7         85 return $self->__known_for();
253             }
254              
255             sub _news {
256 3     3   8 my $self = shift;
257              
258 3 100       14 if (!defined $self->__news()) { $self->__news($self->_get_news()); }
  1         19  
259 3         43 return $self->__news();
260             }
261              
262             sub _photos {
263 3     3   6 my $self = shift;
264              
265 3 100       15 if (!defined $self->__photos()) { $self->__photos($self->_get_photos()); }
  1         18  
266 3         51 return $self->__photos();
267             }
268              
269             sub _trivia {
270 2     2   5 my $self = shift;
271              
272 2 100       10 if (!defined $self->__trivia()) { $self->__trivia($self->_get_trivia()); }
  1         18  
273 2         27 return $self->__trivia();
274             }
275              
276             sub _where_now {
277 2     2   4 my $self = shift;
278              
279 2 100       9 if (!defined $self->__where_now()) { $self->__where_now($self->_get_where_now()); }
  1         16  
280 2         31 return $self->__where_now();
281             }
282              
283              
284             ################################
285             #
286             # Parsing methods
287             #
288             ################################
289              
290             sub _get_id {
291 6     6   14 my $self = shift;
292              
293 6         34 my $nconst;
294              
295 6 100       20 if (exists $self->_q()->{'nconst'}) {
    50          
296 4         52 $nconst = $self->_q()->{'nconst'}
297             } elsif (exists $self->_q()->{'imdbid'}) {
298 2         39 my $imdbid = $self->_q()->{'imdbid'};
299 2 50       30 my ($id) = $imdbid =~ m/^(?:nm)?(\d+)$/ or die "Failed to parse '$imdbid'";
300 2         14 $nconst = sprintf("nm%07d", $id);
301             } else {
302 0         0 croak "No valid search criteria";
303             }
304              
305             # See comments in WebService::IMDB::Title::_get_id()
306              
307 6         92 my $uri = URI->new();
308 6         343 $uri->scheme("http");
309 6         496 $uri->host($self->_domain());
310 6         566 $uri->path(sprintf("/name/%s/maindetails", $nconst));
311              
312 6         182 my $content = $self->_ws()->_response_decoded_json(GET $uri->as_string());
313              
314 6 50       50 if ($content->{'nconst'} ne $nconst) {
315 0         0 die "nconst failed round trip"
316             }
317              
318 6         142 return $nconst;
319              
320             }
321              
322             sub _get_birth {
323 1     1   3 my $self = shift;
324              
325 1 50       5 if (exists $self->_content(PAGE_MAINDETAILS)->{'birth'}) {
326 1         6 return WebService::IMDB::Birth->_new($self->_ws(), $self->_content(PAGE_MAINDETAILS)->{'birth'});
327             } else {
328 0         0 return undef;
329             }
330              
331             }
332              
333             sub _get_death {
334 1     1   3 my $self = shift;
335              
336 1 50       5 if (exists $self->_content(PAGE_MAINDETAILS)->{'death'}) {
337 1         6 return WebService::IMDB::Death->_new($self->_ws(), $self->_content(PAGE_MAINDETAILS)->{'death'});
338             } else {
339 0         0 return undef;
340             }
341              
342             }
343              
344             sub _get_image {
345 1     1   3 my $self = shift;
346              
347 1 50       6 if (exists $self->_content(PAGE_MAINDETAILS)->{'image'}) {
348 1         6 return WebService::IMDB::Image->_new($self->_ws(), $self->_content(PAGE_MAINDETAILS)->{'image'});
349             } else {
350 0         0 return undef;
351             }
352              
353             }
354              
355             sub _get_known_for {
356 1     1   4 my $self = shift;
357              
358 1         3 return [map { WebService::IMDB::KnownFor->_new($self->_ws(), $_) } @{$self->_content(PAGE_MAINDETAILS)->{'known_for'}}];
  4         14  
  1         6  
359              
360             }
361              
362             sub _get_news {
363 1     1   2 my $self = shift;
364              
365 1         6 return WebService::IMDB::News->_new($self->_ws(), $self->_content(PAGE_NEWS));
366              
367             }
368              
369             sub _get_photos {
370 1     1   2 my $self = shift;
371              
372 1         3 return [map { WebService::IMDB::Photo->_new($self->_ws(), $_) } @{$self->_content(PAGE_PHOTOS)->{'photos'}}];
  359         928  
  1         7  
373              
374             }
375              
376             sub _get_trivia {
377 1     1   3 my $self = shift;
378              
379 1         3 return [map { WebService::IMDB::Trivium->_new($self->_ws(), $_) } @{$self->_content(PAGE_TRIVIA)->{'trivia'}}];
  90         232  
  1         7  
380              
381             }
382              
383             sub _get_where_now {
384 1     1   3 my $self = shift;
385              
386 1         2 return [map { WebService::IMDB::WhereNow->_new($self->_ws(), $_) } @{$self->_content(PAGE_TRIVIA)->{'where_now'}}];
  0         0  
  1         5  
387              
388             }
389              
390              
391             ################################
392             #
393             # Debug / dev code
394             #
395             ################################
396              
397             sub _unparsed {
398 0     0     my $self = shift;
399              
400 2     2   20 use Storable qw(dclone);
  2         4  
  2         1240  
401 0           my $d = { map {$_ => dclone($self->_content($_))} (1..PAGE_LAST) };
  0            
402              
403 0           delete $d->{PAGE_MAINDETAILS()}->{'nconst'};
404 0           delete $d->{PAGE_MAINDETAILS()}->{'aka'};
405 0           delete $d->{PAGE_MAINDETAILS()}->{'bio'};
406 0           delete $d->{PAGE_MAINDETAILS()}->{'birth'};
407 0           delete $d->{PAGE_MAINDETAILS()}->{'death'};
408 0           delete $d->{PAGE_MAINDETAILS()}->{'image'};
409 0           delete $d->{PAGE_MAINDETAILS()}->{'known_for'};
410 0           delete $d->{PAGE_MAINDETAILS()}->{'name'};
411 0           $d->{PAGE_NEWS()} = {};
412 0           delete $d->{PAGE_PHOTOS()}->{'photos'};
413 0           delete $d->{PAGE_QUOTES()}->{'quotes'};
414 0           delete $d->{PAGE_MAINDETAILS()}->{'real_name'};
415 0           delete $d->{PAGE_TRIVIA()}->{'trivia'};
416 0           delete $d->{PAGE_TRIVIA()}->{'where_now'};
417              
418             # TODO: Check that these really aren't required
419 0           delete $d->{PAGE_MAINDETAILS()}->{'has'};
420              
421             # TODO: Check that there's nothing in these that doesn't occur in the dedicated pages
422 0           delete $d->{PAGE_MAINDETAILS()}->{'news'};
423 0           delete $d->{PAGE_MAINDETAILS()}->{'photos'};
424              
425 0           delete $d->{PAGE_PHOTOS()}->{'name'};
426 0           delete $d->{PAGE_PHOTOS()}->{'nconst'};
427              
428 0           delete $d->{PAGE_QUOTES()}->{'name'};
429 0           delete $d->{PAGE_QUOTES()}->{'nconst'};
430              
431 0           delete $d->{PAGE_TRIVIA()}->{'name'};
432 0           delete $d->{PAGE_TRIVIA()}->{'nconst'};
433              
434 0           return $d;
435             }
436              
437             1;