File Coverage

lib/WWW/Comic.pm
Criterion Covered Total %
statement 68 82 82.9
branch 8 16 50.0
condition 3 9 33.3
subroutine 16 19 84.2
pod 6 8 75.0
total 101 134 75.3


line stmt bran cond sub pod time code
1             ############################################################
2             #
3             # $Id: Comic.pm,v 1.5 2006/01/10 15:45:44 nicolaw Exp $
4             # WWW::Comic - Retrieve Comic of the day comic strip images
5             #
6             # Copyright 2006 Nicola Worthington
7             #
8             # Licensed under the Apache License, Version 2.0 (the "License");
9             # you may not use this file except in compliance with the License.
10             # You may obtain a copy of the License at
11             #
12             # http://www.apache.org/licenses/LICENSE-2.0
13             #
14             # Unless required by applicable law or agreed to in writing, software
15             # distributed under the License is distributed on an "AS IS" BASIS,
16             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17             # See the License for the specific language governing permissions and
18             # limitations under the License.
19             #
20             ############################################################
21              
22             package WWW::Comic;
23             # vim:ts=4:sw=4:tw=78
24              
25 3     3   43740 use strict;
  3         8  
  3         136  
26 3     3   18 use Carp qw(carp croak);
  3         8  
  3         220  
27 3     3   1430 use WWW::Comic::Plugin qw();
  3         21  
  3         104  
28             use Module::Pluggable(
29 3         29 search_path => [ __PACKAGE__.'::Plugin' ],
30             instantiate => 'new',
31             sub_name => '_plugins',
32 3     3   2692 );
  3         33170  
33              
34 3 50   3   321 use constant DEBUG => $ENV{DEBUG} ? 1 : 0;
  3         6  
  3         215  
35 3     3   17 use vars qw($VERSION $AUTOLOAD);
  3         6  
  3         3135  
36             $VERSION = '1.06' || sprintf('%d.%02d', q$Revision$ =~ /(\d+)/g);
37              
38              
39             #################################
40             # Public methods
41              
42             sub new {
43 1 50   1 1 720 ref(my $class = shift) && croak 'Class name required';
44 1         10 my $self = { plugins => [ __PACKAGE__->_plugins() ] };
45 1         26 bless $self, $class;
46 1         5 DUMP('$self',$self);
47 1         9 return $self;
48             }
49              
50             sub comics {
51 1     1 1 3 my $self = shift;
52 1         6 my $comics = $self->_comics_to_plugins(@_);
53 1         2 return sort(keys(%{$comics}));
  1         11  
54             }
55              
56             sub strip_url {
57 6     6 1 664 my $self = shift;
58 6         78 my %param = $self->_parse_params(@_);
59 6         28 my $plugin = $self->_plugin_to_handle_comic($param{comic});
60 6         34 return $plugin->strip_url(%param);
61             }
62              
63             sub get_strip {
64 12     12 1 37 my $self = shift;
65 12         63 my %param = $self->_parse_params(@_);
66 12         63 my $plugin = $self->_plugin_to_handle_comic($param{comic});
67 12         80 return $plugin->get_strip(%param);
68             }
69              
70             sub mirror_strip {
71 6     6 1 14 my $self = shift;
72 6         26 my %param = $self->_parse_params(@_);
73 6         26 my $plugin = $self->_plugin_to_handle_comic($param{comic});
74 6         56 return $plugin->mirror_strip(%param);
75             }
76              
77             sub plugins {
78 1     1 1 498 my $self = shift;
79 1         3 my @plugins = ();
80 1         3 push @plugins, map { ref($_) } @{$self->{plugins}};
  5         14  
  1         153  
81 1         7 return @plugins;
82             }
83              
84             sub AUTOLOAD {
85 0     0   0 my $self = shift;
86 0         0 my %param = $self->_parse_params(@_);
87 0         0 my $plugin = $self->_plugin_to_handle_comic($param{comic});
88              
89 0         0 (my $name = $AUTOLOAD) =~ s/.*://;
90 0 0       0 if (UNIVERSAL::can($plugin,$name)) {
91 0         0 return $plugin->$name(%param);
92             }
93              
94 0         0 croak "Plugin ".ref($plugin)." does not support method ${name}()";
95             }
96              
97 0     0   0 sub DESTROY {}
98              
99              
100              
101              
102             #################################
103             # Private methods
104              
105             sub _plugin_to_handle_comic {
106 24     24   58 my ($self,$comic) = @_;
107              
108 24         39 my $plugin = undef;
109 24         193 my $comic_plugins = $self->_comics_to_plugins(@_);
110 24         46 while (my ($k,$v) = each %{$comic_plugins}) {
  87         265  
111 87 100       263 if (lc($k) eq lc($comic)) {
112 24         41 $plugin = $v;
113 24         48 last;
114             }
115             }
116              
117 24 50 33     302 croak "No plugin found for comic '$comic'"
      33        
118             unless (defined($plugin) && ref($plugin) &&
119             UNIVERSAL::isa($plugin, __PACKAGE__.'::Plugin'));
120              
121 24         103 return $plugin;
122             }
123              
124             sub _comics_to_plugins {
125 25     25   74 my $self = shift;
126              
127 25         47 my %comics;
128 25         52 for my $plugin (@{$self->{plugins}}) {
  25         96  
129 125         615 for my $comic ($plugin->comics(@_)) {
130 175 50       1046 $comics{$comic} = $plugin if defined $comic;
131             }
132             }
133              
134 25         86 return \%comics;
135             }
136              
137             sub _parse_params {
138 24     24   72 my $self = shift;
139 24 50       103 if (@_ % 2) {
140 0         0 croak "Odd number of paramaters passed when even expected";
141             } else {
142 24         123 my %params = @_;
143 24 50 33     272 croak "Missing mandatory 'comic' paramater"
144             unless (exists($params{comic}) && $params{comic} =~ /\S+/);
145             }
146 24         131 return @_;
147             }
148              
149             sub TRACE {
150 0     0 0 0 return unless DEBUG;
151 0         0 carp(shift());
152             }
153              
154             sub DUMP {
155 1     1 0 3 return unless DEBUG;
156 0           eval {
157 0           require Data::Dumper;
158 0           carp(shift().': '.Data::Dumper::Dumper(shift()));
159             }
160             }
161              
162              
163             1;
164              
165              
166             =pod
167              
168             =head1 NAME
169              
170             WWW::Comic - Retrieve comic strip images
171              
172             =head1 SYNOPSIS
173              
174             use strict;
175             use WWW::Comic qw();
176            
177             # Create a WWW::Comic object
178             my $wc = new WWW::Comic;
179            
180             # Get a list of supported comics
181             my @comics = $wc->comics;
182            
183             # Allow HTTP requests to retrieve a full list of supported
184             # comics if necessary (some plugins may not know what comics
185             # they support until they make an HTTP request)
186             my @comics = $wc->comics(probe => 1);
187            
188             for my $comic (@comics) {
189             # Get the most recent comic strip URL for $comic
190             my $url = $comic->strip_url(comic => $comic);
191            
192             # Download the most recent comic strip
193             # for $comic in to $blob
194             my $blob = $comic->get_strip(comic => $comic);
195            
196             # Write the most recent comic strip for
197             # $comic to disk
198             my $filename = $comic->mirror_strip(comic => $comic);
199             }
200            
201             =head1 DESCRIPTION
202              
203             This module will download cartoon comic strip images from various
204             websites and return a binary blob of the image, or write it to
205             disk. Multiple comic strips can be supported through subclassed
206             plugin modules.
207              
208             A number of plugin modules are bundled as part of this distrubution.
209             You may want to refer to their documentation for any additional
210             custom methods and features. Specifically, L
211             and L require use of the C
212             paramater with the C method on order to retrieve a list
213             of supported commics.
214              
215             To find out what plugin modules you currently have installed and
216             available, run the following:
217              
218             perl -MWWW::Comic -MData::Dumper -e"print Dumper([WWW::Comic->new->plugins]);"
219              
220             =head1 METHODS
221              
222             =head2 new
223              
224             my $wc = new WWW::Comic;
225              
226             Creates a new WWW::Comic object.
227              
228             =head2 comics
229              
230             my @comics = $wc->comics(probe => 1);
231            
232             Returns a list of available comics. The C paramater is
233             optional. (See below).
234              
235             =over 4
236              
237             =item probe
238              
239             This paramater is an optional boolean value supported by a few
240             plugins that do not automatically know what comics they support.
241             Specifying a boolean true value for this paramater will tell those
242             plugins that they should make HTTP requests to find out what comics
243             they can make available. Plugins should cache this information in
244             memory once they have performed an initial probe.
245              
246             =back
247              
248             =head2 strip_url
249              
250             # Get the URL of the most recent "mycomic" comic image
251             my $url = $wc->strip_url(comic => "mycomic");
252            
253             # Get the URl of a specific "mycomic" comic image
254             my $specificStripUrl = $wc->strip_url(
255             comic => "mycomic",
256             id => 990317
257             );
258              
259             Returns the URL of a comic strip. The C paramater is
260             mandatory and must be a valid supported comic as listed by the
261             C method. The most recent comic strip image URL will
262             be returned unless otherwise specified (see the C paramater
263             below).
264              
265             This method will return an C value upon failure.
266              
267             The C paramater is optional and can be used to specify a specific
268             comic (if supported by the plugin in question). Comic IDs are typically
269             date based in some way, but this is unique to each comic and follows
270             no special format for the purposes of this module. See each plugin
271             module's documentation for further information.
272              
273             =over 4
274              
275             =item comic
276              
277             This paramater is mandatory. It specifies the comic that this method
278             should process. See the C method.
279              
280             =item id
281              
282             This paramater is optional. It specifies a specfic comic that should
283             be processed.
284              
285             =back
286              
287             =head2 get_strip
288              
289             # Retrieve the most recent "mycomic" comic strip image
290             my $imageBlob = $wc->get_strip(comic => "mycomic");
291            
292             # Retrieve a specific "mycomic" comic strip image
293             my $image2 = $wc->get_strip(
294             comic => "mycomic",
295             id => "0042304973"
296             );
297              
298             Downloads a copy of a comic strip image and returns the binary data
299             as a scalar. The C paramater is mandatory and must be a valid
300             supported comic as listed by the C method. The most recent
301             comic strip image will be returned unless otherwise specified.
302              
303             This method will return an C value upon failure.
304              
305             =over 4
306              
307             =item comic
308              
309             This paramater is mandatory. It specifies the comic that this method
310             should process. See the C method.
311              
312             =item id
313              
314             This paramater is optional. It specifies a specfic comic that should
315             be processed.
316              
317             =item url
318              
319             This paramater is optional. It specifies a specific comic that should
320             be processed. If specified it must be a fully qualified and valid absolute
321             HTTP URL. This paramater is typically only used when being called
322             indirectly by the C method.
323              
324             =back
325              
326             =head2 mirror_strip
327              
328             # Write the most recent "mycomic" comic strip to disk
329             # and return the name of the file that was written
330             my $filename = $wc->mirror_strip(comic => "mycomic");
331            
332             # Write the "mycomic" comic strip image (reference 132)
333             # to disk, specifcally to mycomic.gif, and return the
334             # actual filename that was written to disk in to $file2
335             my $file2 = $wc->mirror_strip(
336             comic => "mycomic",
337             id => "132",
338             filename => "mycomic.gif"
339             );
340              
341             Download a copy of a comic strip image and write it to disk,
342             returning the name of the file that was actually written. This
343             method accepts the same paramaters as the C method,
344             with the addition of the C paramater.
345              
346             This method will return an C value upon failure.
347              
348             =over 4
349              
350             =item comic
351              
352             This paramater is mandatory. It specifies the comic that this method
353             should process. See the C method.
354              
355             =item id
356              
357             This paramater is optional. It specifies a specfic comic that should
358             be processed.
359              
360             =item url
361              
362             This paramater is optional. It specifies a specific comic that should
363             be processed. If specified it must be a fully qualified and valid absolute
364             HTTP URL.
365              
366             =item filename
367              
368             This paramater is optional. It specifiec the target filename that you
369             would like to be written to disk. If you do not supply an image file
370             extension, one will be added for you automatically. If you specify an
371             image file extension that differs to the file format of the file that
372             is to ultimately be written disk, it will be altered for you
373             automatically.
374              
375             =back
376              
377             =head2 plugins
378              
379             my @plugins = $wc->plugins;
380              
381             Return a list of loaded plugins.
382              
383             =head1 PLUGINS
384              
385             Support for different comics is handled through the L
386             superclass. See the POD for L on how to create a new
387             plugin.
388              
389             =head1 SEE ALSO
390              
391             L, L, L
392              
393             =head1 VERSION
394              
395             $Id: Comic.pm,v 1.5 2006/01/10 15:45:44 nicolaw Exp $
396              
397             =head1 AUTHOR
398              
399             Nicola Worthington
400              
401             L
402              
403             =head1 COPYRIGHT
404              
405             Copyright 2006 Nicola Worthington.
406              
407             This software is licensed under The Apache Software License, Version 2.0.
408              
409             L
410              
411             =cut
412              
413