File Coverage

blib/lib/Business/xISBN.pm
Criterion Covered Total %
statement 30 34 88.2
branch 5 10 50.0
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 42 52 80.7


line stmt bran cond sub pod time code
1 1     1   41830 use Carp qw(carp);
  1         2  
  1         75  
2             carp "The xisbn service was due to be turned off on March 15, 2016.";
3             package Business::xISBN;
4 1     1   6 use Business::ISBN 2;
  1         15  
  1         47  
5              
6             package # hide from PAUSE
7             Business::ISBN;
8              
9 1     1   42 use strict;
  1         2  
  1         52  
10              
11             =encoding utf8
12              
13             =head1 NAME
14              
15             Business::xISBN - access the xISBN service
16              
17             =head1 SYNOPSIS
18              
19             use Business::ISBN;
20             use Business::xISBN;
21              
22             # maybe you don't care what it is as long as everything works
23             my $isbn = Business::ISBN->new( $ARGV[0] );
24             my @xisbns = $isbn->xisbn;
25              
26              
27             =head1 DESCRIPTION
28              
29             This is a mixin for L. Although it looks like it has
30             a different package name, it really declares methods to L.
31             This means that you can use this module and not change code from the 2.x
32             major version of L.
33              
34             This is not a complete interface to xISBN. It merely retrieves related
35             ISBNs. If someone wants to expand this to handle other parts of the API,
36             send a pull request!
37              
38             =cut
39              
40             $VERSION = "1.002";
41              
42             =item xisbn
43              
44             In scalar context, returns an anonymous array of related ISBNs using xISBN.
45             In list context, returns a list. It does not include the original ISBN.
46              
47             This feature uses L or L depending on
48             which one it finds first.
49              
50             =cut
51              
52 1     1   6 no warnings qw(redefine);
  1         1  
  1         384  
53              
54             sub xisbn {
55 4     4 0 1811 my $self = shift;
56              
57 4         17 my $data = $self->_get_xisbn;
58 4         80 $data =~ tr/x/X/;
59              
60 4         8 my @isbns = do {
61 4 50       427 if( eval "require Mojo::DOM; 1" ) {
62 4         25 my $dom = Mojo::DOM->new( $data );
63 4         9295 $dom->find( 'isbn' )->map('text')->each;
64             }
65             else {
66 0         0 $data =~ m|(.*?)|g;
67             }
68             };
69              
70 4         8322 shift @isbns;
71 4 100       40 wantarray ? @isbns : \@isbns;
72             }
73              
74             sub _get_xisbn {
75 4     4   6 my $self = shift;
76              
77 4         5 my $data = eval {
78 4 50       257 if( eval "require Mojo::UserAgent; 1" ) {
    0          
79 4         20 Mojo::UserAgent->new->get( $self->_xisbn_url )->res->text;
80             }
81             elsif( eval "require LWP::Simple; 1" ) {
82 0         0 LWP::Simple::get( $self->_xisbn_url );
83             }
84             else {
85 0         0 carp "Could not load either Mojo::UserAgent or LWP::Simple to fetch xISBN\n";
86 0         0 return;
87             }
88             };
89              
90 4 50       1366257 carp "Could not fetch xISBN data" unless defined $data;
91              
92 4         1556 return $data;
93             }
94              
95             sub _xisbn_url {
96 6     6   3781 my $self = shift;
97 6         28 my $isbn = $self->as_string([]);
98              
99 6         137 return "http://xisbn.worldcat.org/xid/isbn/$isbn";
100             }
101              
102             1;
103              
104             __END__