File Coverage

blib/lib/WWW/Scraper/ISBN.pm
Criterion Covered Total %
statement 56 66 84.8
branch 11 14 78.5
condition 4 9 44.4
subroutine 11 12 91.6
pod 5 5 100.0
total 87 106 82.0


line stmt bran cond sub pod time code
1             package WWW::Scraper::ISBN;
2              
3 6     6   103537 use strict;
  6         10  
  6         227  
4 6     6   27 use warnings;
  6         9  
  6         268  
5              
6             our $VERSION = '1.03';
7              
8             #----------------------------------------------------------------------------
9             # Library Modules
10              
11 6     6   29 use Carp;
  6         14  
  6         464  
12 6     6   2357 use WWW::Scraper::ISBN::Record;
  6         11  
  6         166  
13 6     6   2405 use WWW::Scraper::ISBN::Driver;
  6         11  
  6         216  
14              
15 6     6   3109 use Module::Pluggable search_path => ['WWW::Scraper::ISBN'];
  6         49814  
  6         36  
16              
17 6     6   1225 eval "use Business::ISBN";
  0         0  
  0         0  
18             my $business_isbn_loaded = ! $@;
19              
20             #----------------------------------------------------------------------------
21             # Public API
22              
23             # Preloaded methods go here.
24             sub new {
25 2     2 1 373 my $proto = shift;
26 2   66     9 my $class = ref($proto) || $proto;
27              
28 2         5 my $self = {
29             DRIVERS => []
30             };
31              
32 2         4 bless ($self, $class);
33 2         3 return $self;
34             }
35              
36             sub available_drivers {
37 0     0 1 0 my $self = shift;
38 0         0 my @plugins = $self->plugins();
39 0         0 my @drivers = map { s/WWW::Scraper::ISBN::// } grep { /_Driver$/ } @plugins;
  0         0  
  0         0  
40 0         0 return @drivers;
41             }
42              
43             sub drivers {
44 6     6 1 560 my $self = shift;
45 6         14 while ($_ = shift) { push @{$self->{DRIVERS}}, $_; }
  2         2  
  2         10  
46 6         4 for my $driver ( @{ $self->{DRIVERS} }) {
  6         15  
47 5         270 require "WWW/Scraper/ISBN/".$driver."_Driver.pm";
48             }
49 6         235 return @{ $self->{DRIVERS} };
  6         153  
50             }
51              
52             sub reset_drivers {
53 1     1 1 774 my $self = shift;
54 1         3 $self->{DRIVERS} = [];
55 1         2 return @{ $self->{DRIVERS} };
  1         17  
56             }
57              
58             sub search {
59 6     6 1 1908 my ($self,$isbn) = @_;
60              
61 6 100       112 croak("Invalid ISBN specified [].\n") unless($isbn);
62              
63 5 50       10 if($business_isbn_loaded) {
64             # Business::ISBN has strong validation algorithms
65 0         0 my $isbn_object = Business::ISBN->new($isbn);
66 0 0 0     0 croak("Invalid ISBN specified [$isbn].\n") unless($isbn_object && $isbn_object->is_valid);
67             } else {
68             # our fallback just validates it looks like an ISBN
69 5         16 my $isbn_object = WWW::Scraper::ISBN::Driver->new();
70 5 100 66     19 croak("Invalid ISBN specified [$isbn].\n") unless($isbn_object && $isbn_object->is_valid($isbn));
71             }
72              
73 4 100       8 croak("No search drivers specified.\n")
74             if( $self->drivers == 0 );
75              
76 3         11 my $record = WWW::Scraper::ISBN::Record->new();
77 3         8 $record->isbn($isbn);
78 3         3 for my $driver_name (@{ $self->{DRIVERS} }) {
  3         5  
79 3         12 my $driver = "WWW::Scraper::ISBN::${driver_name}_Driver"->new();
80 3         6 $driver->search($record->isbn);
81 3 100       8 if ($driver->found) {
82 1         2 $record->found("1");
83 1         3 $record->found_in("$driver_name");
84 1         3 $record->book($driver->book);
85 1         6 last;
86             }
87              
88 2 100       9 $record->error($record->error.$driver->error)
89             if ($driver->error);
90             }
91              
92 3         8 return $record;
93             }
94              
95             1;
96              
97             __END__