File Coverage

blib/lib/WWW/Scraper/ISBN.pm
Criterion Covered Total %
statement 64 68 94.1
branch 13 16 81.2
condition 4 9 44.4
subroutine 12 12 100.0
pod 5 5 100.0
total 98 110 89.0


line stmt bran cond sub pod time code
1             package WWW::Scraper::ISBN;
2              
3 6     6   319409 use strict;
  6         49  
  6         158  
4 6     6   26 use warnings;
  6         10  
  6         238  
5              
6             our $VERSION = '1.04';
7              
8             #----------------------------------------------------------------------------
9             # Library Modules
10              
11 6     6   33 use Carp;
  6         10  
  6         351  
12 6     6   2333 use WWW::Scraper::ISBN::Record;
  6         17  
  6         161  
13 6     6   2366 use WWW::Scraper::ISBN::Driver;
  6         14  
  6         242  
14              
15 6     6   2502 use Module::Pluggable search_path => ['WWW::Scraper::ISBN'];
  6         58199  
  6         42  
16              
17 6     6   945 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 727 my $proto = shift;
26 2   66     12 my $class = ref($proto) || $proto;
27              
28 2         6 my $self = {
29             DRIVERS => []
30             };
31              
32 2         4 bless ($self, $class);
33 2         6 return $self;
34             }
35              
36             sub available_drivers {
37 1     1 1 330 my $self = shift;
38 1         5 my @plugins = $self->plugins();
39 1         1820 my @drivers;
40 1         3 for my $plugin (@plugins) {
41 3 100       11 next unless($plugin =~ /_Driver$/);
42 1         6 $plugin =~ s/WWW::Scraper::ISBN::(\w+)_Driver/$1/;
43 1         3 push @drivers, $plugin;
44             }
45 1         5 return @drivers;
46             }
47              
48             sub drivers {
49 6     6 1 1826 my $self = shift;
50 6         18 while ($_ = shift) { push @{$self->{DRIVERS}}, $_; }
  2         4  
  2         12  
51 6         11 for my $driver ( @{ $self->{DRIVERS} }) {
  6         13  
52 5         35 require "WWW/Scraper/ISBN/".$driver."_Driver.pm";
53             }
54 6         10 return @{ $self->{DRIVERS} };
  6         189  
55             }
56              
57             sub reset_drivers {
58 1     1 1 1077 my $self = shift;
59 1         4 $self->{DRIVERS} = [];
60 1         2 return @{ $self->{DRIVERS} };
  1         4  
61             }
62              
63             sub search {
64 6     6 1 3669 my ($self,$isbn) = @_;
65              
66 6 100       98 croak("Invalid ISBN specified [].\n") unless($isbn);
67              
68 5 50       13 if($business_isbn_loaded) {
69             # Business::ISBN has strong validation algorithms
70 0         0 my $isbn_object = Business::ISBN->new($isbn);
71 0 0 0     0 croak("Invalid ISBN specified [$isbn].\n") unless($isbn_object && $isbn_object->is_valid);
72             } else {
73             # our fallback just validates it looks like an ISBN
74 5         22 my $isbn_object = WWW::Scraper::ISBN::Driver->new();
75 5 100 66     23 croak("Invalid ISBN specified [$isbn].\n") unless($isbn_object && $isbn_object->is_valid($isbn));
76             }
77              
78 4 100       11 croak("No search drivers specified.\n")
79             if( $self->drivers == 0 );
80              
81 3         12 my $record = WWW::Scraper::ISBN::Record->new();
82 3         9 $record->isbn($isbn);
83 3         4 for my $driver_name (@{ $self->{DRIVERS} }) {
  3         7  
84 3         15 my $driver = "WWW::Scraper::ISBN::${driver_name}_Driver"->new();
85 3         9 $driver->search($record->isbn);
86 3 100       12 if ($driver->found) {
87 1         4 $record->found("1");
88 1         5 $record->found_in("$driver_name");
89 1         3 $record->book($driver->book);
90 1         6 last;
91             }
92              
93 2 100       10 $record->error($record->error.$driver->error)
94             if ($driver->error);
95             }
96              
97 3         14 return $record;
98             }
99              
100             1;
101              
102             __END__