blib/lib/WebService/LibraryThing/ThingISBN.pm

filter: Statementstmt 66.3% 79 / 119 Branchbran 39.2% 22 / 56 Conditioncond 33.3% 6 / 18 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 88.2% 15 / 17 Podpod 100% 1 / 1 totaltotal 55.8% 124 / 222 CC 36 SCAR 23.9
CC
36
Cov
55%
_clean_lccn_input
20.0
_clean_oclc_input
20.0
_thing_isbn_all
19.1
CRAP 10.9
↑ Summary
1 package WebService::LibraryThing::ThingISBN;
2  
3 3 ▶use Business::ISBN;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp qw( carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our @EXPORT_OK = qw( thing_isbn_list );
12  
13 =head1 NAME
14  
15 WebService::LibraryThing::ThingISBN - Get ISBNs for all editions of a book
16  
17 =head1 VERSION
18  
19 Version 0.503
20  
21 =cut
22  
23 3 our $VERSION = '0.503';
24  
25 =head1 SYNOPSIS
26  
27 This is a Perl interface to the LibraryThing social cataloging
28 website's thingISBN web service, which "takes an ISBN and returns a
29 list of ISBNs from the same 'work' (ie., other editions and
30 translations)." The web service is freely available for noncommercial
31 use, per the terms at L<http://www.librarything.com/api>.
32  
33 use WebService::LibraryThing::ThingISBN qw( thing_isbn_list );
34 my @alternate_isbns = thing_isbn_list( isbn => '0060987049' );
35  
36 =head1 EXPORT
37  
38 Exports nothing by default. C<thing_isbn_list> can be optionally exported:
39  
40 use WebService::LibraryThing::ThingISBN qw( thing_isbn_list );
41  
42 =head1 FUNCTIONS
43  
44 =head2 C<thing_isbn_list>
45  
46 This function takes a single ISBN as an argument, and queries
47 LibraryThing's thingISBN web service to get a list of ISBNs
48 corresponding to other editions of the same book, based on
49 LibraryThing's work definitions.
50  
51 ISBNs can be either strings (hyphenated or unhyphenated; ISBN-10 or
52 ISBN-13) or Business::ISBN objects.
53  
54 C<thing_isbn_list> returns a list of unhyphenated ISBN strings. If
55 there's an error, it returns an empty list.
56  
57 Per LibraryThing's API terms of use, requests are limited to run no
58 more than once per second. (Users hitting the service over 1000 times
59 a day are required to notify LibraryThing.)
60  
61 =head1 AUTHOR
62  
63 Anirvan Chatterjee, C<< <anirvan at cpan.org> >>
64  
65 =head1 BUGS
66  
67 Please report any bugs or feature requests to
68 C<bug-webservice-librarything-thingisbn at rt.cpan.org>, or through
69 the web interface at
70 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WebService-LibraryThing-ThingISBN>.
71 I will be notified, and then you'll automatically be notified of
72 progress on your bug as I make changes.
73  
74 Bug reports about the underlying thingISBN API should be sent to
75 LibraryThing. Contact them at L<http://www.librarything.com/contact>.
76  
77 =head1 SUPPORT
78  
79 The thingISBN API is documented at:
80  
81 =over 4
82  
83 =item * LibraryThing APIs
84
85 L<http://www.librarything.com/api>
86  
87 =item * "Introducing thingISBN"
88  
89 L<http://www.librarything.com/thingology/2006/06/introducing-thingisbn_14.php>
90  
91 =back
92  
93 You can find documentation for this module with the perldoc command.
94  
95 perldoc WebService::LibraryThing::ThingISBN
96  
97 You can also look for information at:
98  
99 =over 4
100  
101 =item * RT: CPAN's request tracker
102  
103 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WebService-LibraryThing-ThingISBN>
104  
105 =item * AnnoCPAN: Annotated CPAN documentation
106  
107 L<http://annocpan.org/dist/WebService-LibraryThing-ThingISBN>
108  
109 =item * CPAN Ratings
110  
111 L<http://cpanratings.perl.org/d/WebService-LibraryThing-ThingISBN>
112  
113 =item * Search CPAN
114  
115 L<http://search.cpan.org/dist/WebService-LibraryThing-ThingISBN/>
116  
117 =back
118  
119 =head1 ACKNOWLEDGEMENTS
120  
121 Thanks to LibraryThing for making thingISBN available.
122  
123 =head1 COPYRIGHT & LICENSE
124  
125 Copyright 2009 Anirvan Chatterjee.
126  
127 This program is free software; you can redistribute it and/or modify it
128 under the terms of either: the GNU General Public License as published
129 by the Free Software Foundation; or the Artistic License.
130  
131 See http://dev.perl.org/licenses/ for more information.
132  
133 Use of the LibraryThing thingISBN API is governed by the terms of use
134 listed at L<http://www.librarything.com/api>.
135  
136 =cut
137  
138 9 ▶sub thing_isbn_list {
Subroutine thing_isbn_list: called CC=1 SCAR=0.0
Pod documented
139 9 my $isbn = shift;
140 9 my @isbns = _thing_isbn_all( isbn => $isbn );
141 9 return @isbns;
142 }
143  
144 9 ▶sub _thing_isbn_all {
Subroutine _thing_isbn_all: called CC=9 SCAR=29.5
145  
146 9 my ( $input_type, $input_value ) = @_;
147  
148 9 my %valid_input_types = ( isbn => 1, lccn => 1, oclc => 1 );
149 9 ▶ unless ( $input_type and $valid_input_types{$input_type} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($input_type and $valid_input_types{$input_type}) 0 9 9
Condition33%1/3
$input_type and $valid_input_types{$input_type}
!ll&&!rl&&r
009
MC/DC0%0/2
$input_type and $valid_input_types{$input_type}
$input_type $valid_input_types{$input_type}
Truth table33%1/3
A and B
A= $input_typeB= $valid_input_types{$input_type}
ABresult
0X0
100
111
150 0 ▶ my $input_type_printable
Branch: 1 branch
BranchTrueFalseTotal
defined $input_type ? : 0 0 0
151 = ( defined $input_type ? $input_type : '[undef]' );
152 0 carp
153 qq{Expected input type "isbn", "lccn", or "oclc", got "$input_type_printable"};
154 0 return ();
155 }
156  
157 9 my $input_value_clean = $input_value;
158 9 ▶ if ( $input_type eq 'isbn' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($input_type eq 'isbn') { } 9 0 9
elsif ($input_type eq 'lccn') { } 0 0 0
elsif ($input_type eq 'oclc') { } 0 0 0
159 9 $input_value_clean = _clean_isbn_input($input_value_clean);
160 } elsif ( $input_type eq 'lccn' ) {
161 0 $input_value_clean = _clean_lccn_input($input_value_clean);
162 } elsif ( $input_type eq 'oclc' ) {
163 0 $input_value_clean = _clean_oclc_input($input_value_clean);
164 }
165  
166 9 ▶ unless ( defined $input_value_clean and length $input_value_clean ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $input_value_clean and length $input_value_clean) 4 5 9
Condition66%2/3
defined $input_value_clean and length $input_value_clean
!ll&&!rl&&r
405
MC/DC50%1/2
defined $input_value_clean and length $input_value_clean
defined $input_value_clean length $input_value_clean
Truth table66%2/3
A and B
A= defined $input_value_cleanB= length $input_value_clean
ABresult
0X0
100
111
167 4 return ();
168 }
169  
170 return
171 5 _internal_lookup( type => $input_type,
172 value => $input_value_clean );
173 }
174  
175 # internal functions
176  
177 3 my $next_lookup_ok_time = 0;
178 3 my $have_time_hi_res;
179  
180 # http://www.librarything.com/api
181 # http://www.librarything.com/thingology/2008/02/thingisbn-adds-lccns-oclc-numbers.php
182  
183 5 ▶sub _internal_lookup {
Subroutine _internal_lookup: called CC=9 SCAR=25.0
184 5 my %args = @_;
185  
186 5 ▶ unless ( defined $have_time_hi_res ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $have_time_hi_res) 1 4 5
187 1 ▶ $have_time_hi_res = eval q{ use Time::HiRes; 1} || 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
Condition50%1/2
eval ' use Time::HiRes; 1' || 0
l!l
10
MC/DC0%0/1
eval ' use Time::HiRes; 1' || 0
eval ' use Time::HiRes; 1'
Truth table50%1/2
A
A= eval ' use Time::HiRes; 1'
Aresult
11
00
188 }
189  
190 5 my $type = $args{type};
191 5 my $value = $args{value};
192 5 ▶ my $ua = $args{ua} ||= _default_ua();
Condition33%1/3
$args{"ua"} ||= _default_ua()
l!l&&r!l&&!r
050
MC/DC0%0/2
$args{"ua"} ||= _default_ua()
$args{"ua"} _default_ua()
Truth table33%1/3
A ||= B
A= $args{"ua"}B= _default_ua()
ABresult
1X1
011
000
193  
194 5 my $arg;
195 5 ▶ if ( $type eq 'isbn' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'isbn') { } 5 0 5
elsif ($type eq 'lccn') { } 0 0 0
elsif ($type eq 'oclc') { } 0 0 0
196 5 $arg = $value;
197 } elsif ( $type eq 'lccn' ) {
198 0 $arg = "lccn$value";
199 } elsif ( $type eq 'oclc' ) {
200 0 $arg = "ocm$value";
201 }
202  
203 5 my $request = HTTP::Request->new(
204 GET => "http://www.librarything.com/api/thingISBN/$arg&allids=1" );
205  
206 5 ▶ if ( _get_time() < $next_lookup_ok_time ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_get_time() < $next_lookup_ok_time) 4 1 5
207 4 _sleep_until_time($next_lookup_ok_time);
208 }
209  
210 5 my $result = $ua->request($request);
211  
212 5 $next_lookup_ok_time = _get_time() + 1;
213  
214 5 ▶ if ( $result->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_success) { } 0 5 5
215 0 my @isbns = $result->content =~ m|<isbn>(.*?)</isbn>|ig;
216 0 shift @isbns; # remove argument from list
217 0 return @isbns;
218 } else {
219 5 return ();
220 }
221 }
222  
223 9 ▶sub _clean_isbn_input {
Subroutine _clean_isbn_input: called CC=6 SCAR=21.4
224 9 my $isbn_input = shift;
225  
226 9 my $isbn;
227 9 ▶ if ( !defined $isbn_input ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $isbn_input) { } 0 9 9
elsif (ref $isbn_input and eval { do { $isbn_input->isa('Business::ISBN') } }) { } 0 9 9
elsif (not length $isbn_input) { } 0 9 9
228 0 return;
229 ▶ } elsif (
Condition33%1/3
ref $isbn_input and eval { do { $isbn_input->isa("Business::ISBN") } }
!ll&&!rl&&r
900
MC/DC0%0/2
ref $isbn_input and eval { do { $isbn_input->isa("Business::ISBN") } }
ref $isbn_input eval { do { $isbn_input->isa("Business::ISBN") } }
Truth table33%1/3
A and B
A= ref $isbn_inputB= eval { do { $isbn_input->isa("Business::ISBN") } }
ABresult
0X0
100
111
230 ref $isbn_input
231 and eval {
232 0 $isbn_input->isa('Business::ISBN');
233 }
234 ) {
235 0 return $isbn_input->as_string( [] );
236 } elsif ( !length $isbn_input ) {
237 0 return;
238 } else {
239 9 my $isbn_object = Business::ISBN->new($isbn_input);
240 9 ▶ if ($isbn_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isbn_object) 5 4 9
241 5 return $isbn_object->as_string( [] );
242 }
243 }
244 4 return;
245 }
246  
247 0 ▶sub _clean_lccn_input {
Subroutine _clean_lccn_input: not called CC=4 SCAR=30.0
248 0 my $lccn_input = shift;
249  
250 0 my $lccn;
251 0 ▶ if ( !defined $lccn_input ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $lccn_input) { } 0 0 0
elsif (not length $lccn_input) { } 0 0 0
252 0 return;
253 } elsif ( !length $lccn_input ) {
254 0 return;
255 } else {
256 0 $lccn = $lccn_input;
257 0 $lccn =~ s/\s+//g;
258 0 ▶ if ( $lccn !~ m/\d/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $lccn =~ /\d/) { } 0 0 0
259 0 return;
260 } else {
261 0 return $lccn;
262 }
263 }
264 }
265  
266 0 ▶sub _clean_oclc_input {
Subroutine _clean_oclc_input: not called CC=4 SCAR=30.0
267 0 my $oclc_input = shift;
268  
269 0 my $oclc;
270 0 ▶ if ( !defined $oclc_input ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $oclc_input) { } 0 0 0
elsif (not length $oclc_input) { } 0 0 0
271 0 return;
272 } elsif ( !length $oclc_input ) {
273 0 return;
274 } else {
275 0 $oclc = $oclc_input;
276 0 $oclc =~ s/\s+//g;
277 0 $oclc =~ s/^ocm//;
278 0 ▶ if ( $oclc !~ m/\d/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $oclc =~ /\d/) { } 0 0 0
279 0 return;
280 } else {
281 0 return $oclc;
282 }
283 }
284 }
285  
286 my $_default_ua;
287  
288 5 ▶sub _default_ua {
Subroutine _default_ua: called CC=2 SCAR=6.9
289 5 ▶ unless ($_default_ua) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_default_ua) 1 4 5
290 1 $_default_ua = new LWP::UserAgent;
291  
292 # set ua agent string
293 1 my $lwp_agent = $_default_ua->agent();
294 1 $_default_ua->agent(
295 "WebService::LibraryThing::ThingISBN/$VERSION ($lwp_agent)");
296 }
297 5 return $_default_ua;
298 }
299  
300 18 ▶sub _get_time {
Subroutine _get_time: called CC=4 SCAR=18.9
301 18 ▶ unless ( defined $have_time_hi_res ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $have_time_hi_res) 0 18 18
302 0 ▶ $have_time_hi_res = eval q{ use Time::HiRes; 1} || 0;
Condition0%0/2
eval ' use Time::HiRes; 1' || 0
l!l
00
MC/DC0%0/1
eval ' use Time::HiRes; 1' || 0
eval ' use Time::HiRes; 1'
Truth table0%0/2
A
A= eval ' use Time::HiRes; 1'
Aresult
11
00
303 }
304 18 ▶ if ($have_time_hi_res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_time_hi_res) { } 18 0 18
305 18 return Time::HiRes::time();
306 } else {
307 0 return time;
308 }
309 }
310  
311 4 ▶sub _sleep_until_time {
Subroutine _sleep_until_time: called CC=5 SCAR=19.7
312 4 my $time_to_sleep_until = shift;
313 4 ▶ if ( _get_time >= $time_to_sleep_until ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_get_time() >= $time_to_sleep_until) { } 0 4 4
314 0 return;
315 } else {
316 4 ▶ unless ( defined $have_time_hi_res ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $have_time_hi_res) 0 4 4
317 0 ▶ $have_time_hi_res = eval q{ use Time::HiRes; 1} || 0;
Condition0%0/2
eval ' use Time::HiRes; 1' || 0
l!l
00
MC/DC0%0/1
eval ' use Time::HiRes; 1' || 0
eval ' use Time::HiRes; 1'
Truth table0%0/2
A
A= eval ' use Time::HiRes; 1'
Aresult
11
00
318 }
319 4 my $seconds_to_sleep = $time_to_sleep_until - _get_time;
320 4 ▶ if ($have_time_hi_res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_time_hi_res) { } 4 0 4
321 4 Time::HiRes::sleep($seconds_to_sleep);
322 } else {
323 0 sleep $seconds_to_sleep;
324 }
325 }
326 4 return;
327 }
328  
329 3 1; # End of WebService::LibraryThing::ThingISBN
330  
331 # Local Variables:
332 # mode: perltidy
333 # End:
↑ Summary