blib/lib/WebService/Libris.pm

filter: Statementstmt 77.8% 116 / 149 Branchbran 57.1% 16 / 28 Conditioncond 20.0% 2 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 82.8% 29 / 35 Podpod 77.7% 7 / 9 totaltotal 72.1% 171 / 237 CC 20 SCAR 14.1
CC
20
Cov
72%
direct_search
20.0
new
7.1
search_for_isbn
6.0
CRAP 4.1
↑ Summary lib/WebService/Libris/Book.pm »
1 package WebService::Libris;
2 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 my %default_typemap = (
11 bib => 'Book',
12 book => 'Book',
13 auth => 'Author',
14 author => 'Author',
15 library => 'Library',
16 );
17  
18 2 has 'id';
19 2 has 'type';
20 2 has '_dom';
21 2 has 'cache';
22  
23 2 has 'type_map';
24  
25 =head1 NAME
26  
27 WebService::Libris - Access book meta data from libris.kb.se
28  
29 =head1 VERSION
30  
31 Version 0.08
32  
33 Note that the API is still subject to change.
34  
35 =cut
36  
37 2 our $VERSION = '0.08';
38  
39  
40 =head1 SYNOPSIS
41  
42 use WebService::Libris;
43 use 5.010;
44 binmode STDOUT, ':encoding(UTF-8)';
45  
46 my $book = WebService::Libris->new(
47 type => 'book',
48 # Libris ID
49 id => '9604288',
50 # optional but recommended:
51 cache_dir = '/tmp/webservice-libris/',
52 );
53 print $book->title;
54  
55 my $books = WebService::Libris->search(
56 term => 'Astrid Lindgren',
57 page => 1,
58 );
59 while (my $b = $books->next) {
60 say $b->title;
61 say ' isbn: ', $b->isbn;
62 say ' date: ', $b->date;
63 }
64  
65 =head1 DESCRIPTION
66  
67 The Swedish public libraries and the national library of Sweden have a common
68 catalogue containing meta data of the books they have available.
69  
70 This includes many contemporary as well as historical books.
71  
72 The catalogue is available online at L<http://libris.kb.se>, and can be
73 queried with a public API.
74  
75 This module is a wrapper around two of their APIs (xsearch and RDF responses).
76  
77 =head1 METHODS
78  
79 =head2 new
80  
81 my $obj = WebService::Libris->new(
82 type => 'author',
83 id => '246603',
84 );
85  
86 Creates an object of the C<WebService::Libris> class or a subclass thereof
87 (denoted by C<type> in the argument list). C<type> can currently be one of
88 (synonyms on one line)
89  
90 auth author
91 bib book
92 library
93  
94 The C<id> argument is mandatory, and must contain the Libris ID of the object
95 you want to retrieve. If you don't know the Libris ID, use one of the
96 C<search> functions instead.
97  
98 =head2 direct_search
99  
100 my $hashref = WebService::Libris->direct_search(
101 term => 'Your Searchterms Here',
102 page => 1, # page size is 200
103 full => 1, # return all available information
104 );
105  
106 Returns a hashref directly from the JSON response of the xsearch API
107 described at L<http://librishelp.libris.kb.se/help/xsearch_eng.jsp?open=tech>.
108  
109 This is more efficient than a C<< WebService::Libris->search >> call, because
110 it does only one query (whereas C<< ->search >> does one additional request
111 per result object), but it's not as convenient, and does not allow browsing of
112 related entities (such as authors and libraries).
113  
114 =head2 search
115  
116 my @books = WebService::Libris->search(
117 term => 'Your Search Term Here',
118 page => 1,
119 );
120 for my $book (@books) {
121 say $book->title;
122 }
123  
124 Searches the xsearch API for arbitrary search terms, and returns a
125 C<WebService::Libris::Collection> of books.
126  
127 See the C<direct_search> method above for a short discussion.
128  
129 =head2 search_for_isbn
130  
131 my $book = WebService::Libris->search_for_isbn('9170370192');
132  
133 Looks up a book by ISBN
134  
135 =head1 Less interesting methods
136  
137 The following methods aren't usually useful for the casual user, more
138 for those who want to extend or subclass this module.
139  
140 =head2 rdf_url
141  
142 Returns the RDF resource URL for the current object. Mostly useful for internal purposes.
143  
144 =head2 dom
145  
146 Returns the L<Mojo::DOM> object from the web services response.
147 Does a request to the web service if no DOM was stored previously.
148  
149 Only useful for you if you want to extract more data from a response
150 than the object itself provides.
151  
152 =head2 id
153  
154 Returns the libris ID of the object. Only makes sense for subclasses.
155  
156 =head2 type
157  
158 Returns the short type name (C<bib>, C<auth>, C<library>). Only makes sense
159 for subclasses.
160  
161 =head2 fragments
162  
163 Must be overridden in a subclass to return a list of
164 the last two junks of the RDF resource URL, that is the short
165 type name and the libris ID.
166  
167 =head1 AUTHOR
168  
169 Moritz Lenz, C<< <moritz at faui2k3.org> >>
170  
171 =head1 BUGS
172  
173 Please report any bugs or feature requests at
174 L<https://github.com/moritz/WebService-Libris/issues>
175  
176 =head1 SUPPORT
177  
178 You can find documentation for this module with the perldoc command.
179  
180 perldoc WebService::Libris
181  
182 You can also look for information at:
183  
184 =over 4
185  
186 =item * Bug tracker:
187  
188 L<https://github.com/moritz/WebService-Libris/issues>
189  
190 =item * AnnoCPAN: Annotated CPAN documentation
191  
192 L<http://annocpan.org/dist/WebService-Libris>
193  
194 =item * CPAN Ratings
195  
196 L<http://cpanratings.perl.org/d/WebService-Libris>
197  
198 =item * Search CPAN
199  
200 L<http://search.cpan.org/dist/WebService-Libris/>
201  
202 =back
203  
204 =head1 BUGS AND LIMITATIONS
205  
206 Nearly no error checking is done. So beware!
207  
208 =head1 ACKNOWLEDGEMENTS
209  
210 Thanks go to the Kungliga biblioteket (National Library of Sweden) for
211 providing the libris.kb.se service and API.
212  
213 =head1 LICENSE AND COPYRIGHT
214  
215 Copyright 2011 Moritz Lenz.
216  
217 This program is free software; you can redistribute it and/or modify it
218 under the terms of either: the GNU General Public License as published
219 by the Free Software Foundation; or the Artistic License.
220  
221 See L<http://dev.perl.org/licenses/> for more information.
222  
223 =cut
224  
225 15 ▶sub new {
Subroutine new: called CC=7 SCAR=19.6
Pod documented
226 15 my ($class, %opts) = @_;;
227 15 my $c;
228 15 ▶ if ($opts{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"type"}) 15 0 15
229 15 ▶ if ($opts{type_map}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'type_map'}) { } 0 15 15
230 $c = $opts{type_map}{lc $opts{type}}
231 0 ▶ // $default_typemap{lc $opts{type}};
Condition0%0/3
$opts{"type_map"}{lc $opts{"type"}} // $default_typemap{lc $opts{"type"}}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"type_map"}{lc $opts{"type"}} // $default_typemap{lc $opts{"type"}}
$opts{"type_map"}{lc $opts{"type"}} $default_typemap{lc $opts{"type"}}
Truth table0%0/3
A // B
A= $opts{"type_map"}{lc $opts{"type"}}B= $default_typemap{lc $opts{"type"}}
ABresult
1X1
011
000
232 } else {
233 15 $c = $default_typemap{lc $opts{type}};
234 }
235 }
236 15 ▶ if (my $cache_dir = delete $opts{cache_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cache_dir = delete $opts{"cache_dir"}) 1 14 15
237 1 require WebService::Libris::FileCache;
238 1 $opts{cache} = WebService::Libris::FileCache->new(
239 directory => $cache_dir,
240 );
241 }
242 15 ▶ if ($c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c) { } 15 0 15
243 15 $class = __PACKAGE__ . "::" . $c;
244 15 ▶ eval "use $class; 1" or die $@;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "use $class; 1" or die $@ 15 0 15
245 15 return bless \%opts, $class;
246 } else {
247 0 return bless \%opts, $class;
248 }
249 }
250  
251  
252 0 ▶sub rdf_url {
Subroutine rdf_url: not called CC=1 SCAR=6.9
Pod documented
253 0 my $self = shift;
254 0 my ($key, $id) = $self->fragments;
255 0 "http://libris.kb.se/data/$key/$id?format=application%2Frdf%2Bxml";
256 }
257  
258 19 ▶sub dom {
Subroutine dom: called CC=4 SCAR=15.5
Pod documented
259 19 my $self = shift;
260  
261 19 ▶ unless ($self->_dom) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_dom) 3 16 19
262 3 ▶ if ($self->cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache) { } 3 0 3
263 3 my $key = join '/', $self->fragments;
264 3 ▶ if (my $r = $self->cache->get($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $self->cache->get($key)) { } 3 0 3
265 3 $self->_dom($r);
266 } else {
267 0 my $dom = $self->_request_dom;
268 0 $self->cache->set($key, $dom);
269 0 $self->_dom($dom);
270 }
271 } else {
272 0 $self->_dom($self->_request_dom);
273 }
274 }
275 19 $self->_dom;
276 }
277  
278 0 ▶sub _request_dom {
Subroutine _request_dom: not called CC=1 SCAR=6.9
279 0 my $self = shift;
280 0 Mojo::UserAgent->new()->get($self->rdf_url)->res->dom;
281 }
282  
283 0 ▶sub direct_search {
Subroutine direct_search: not called CC=4 SCAR=30.0
Pod documented
284 0 my ($self, %opts) = @_;
285 0 ▶ my $terms = $opts{term} // die "Search term missing";
Condition0%0/2
$opts{"term"} // die("Search term missing")
l!l
00
MC/DC0%0/1
$opts{"term"} // die("Search term missing")
$opts{"term"}
Truth table0%0/2
A
A= $opts{"term"}
Aresult
11
00
286 0 ▶ my $page = $opts{page} // 1;
Condition0%0/2
$opts{"page"} // 1
l!l
00
MC/DC0%0/1
$opts{"page"} // 1
$opts{"page"}
Truth table0%0/2
A
A= $opts{"page"}
Aresult
11
00
287 0 my %q = (
288 query => $terms,
289 n => 200, # max. number of results
290 start => 1 + 200 * ($page - 1),
291 format => 'json',
292 );
293 0 ▶ $q{format_level} = 'full' if $opts{full};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"full"} 0 0 0
294 0 my $url = Mojo::URL->new('http://libris.kb.se/xsearch');
295 0 $url->query(%q);
296 0 my $res = Mojo::UserAgent->new()->get($url)->res;
297 0 $res->json;
298 }
299  
300 0 ▶sub search {
Subroutine search: not called CC=1 SCAR=6.9
Pod documented
301 0 my ($self, %opts) = @_;
302 0 my $json = $self->direct_search(%opts);
303 0 my @ids = map { (split '/', $_->{identifier})[-1] }
304 0 @{ $json->{xsearch}{list} };
305 0 WebService::Libris::Collection->new(
306 type => 'bib',
307 ids => \@ids,
308 cache => $self->cache,
309 );
310 }
311  
312 0 ▶sub search_for_isbn {
Subroutine search_for_isbn: not called CC=2 SCAR=17.9
Pod documented
313 0 my ($self, $isbn) = @_;
314 0 my $res = Mojo::UserAgent->new->max_redirects(1)
315 ->get("http://libris.kb.se/hitlist?q=linkisxn:$isbn");
316 0 my $url = $res->res->headers->location;
317 0 ▶ return unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 0 0
318 0 my ($type, $libris_id) = (split '/', $url)[-2, -1];
319 0 $self->new(type => $type, id => $libris_id, cache => $self->cache);
320 }
321  
322 0 ▶sub fragments {
Subroutine fragments: not called CC=1 SCAR=6.9
Pod documented
323 0 die "Must be overridden in subclasses";
324 }
325  
326 2 ▶sub list_from_dom {
Subroutine list_from_dom: called CC=1 SCAR=0.0
Pod undocumented
327 2 my ($self, $search_for) = @_;
328 2 my $key;
329 my @result;
330 2 my %seen;
331 16 ▶ $self->dom->find($search_for)->each(sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
332 16 my $d = shift;
333 16 ▶ my $resource_url = $d->attr('rdf:resource')
Condition66%2/3
$d->attr("rdf:resource") // $d->attr("rdf:about")
l!l&&r!l&&!r
1402
MC/DC50%1/2
$d->attr("rdf:resource") // $d->attr("rdf:about")
$d->attr("rdf:resource") $d->attr("rdf:about")
Truth table66%2/3
A // B
A= $d->attr("rdf:resource")B= $d->attr("rdf:about")
ABresult
1X1
011
000
334 // $d->attr('rdf:about');
335 16 ▶ return unless $resource_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $resource_url 2 14 16
336 14 my ($k, $id) = $self->fragment_from_resource_url($resource_url);
337 14 ▶ return if $seen{"$k/$id"}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{"$k/$id"}++ 0 14 14
338 14 push @result, __PACKAGE__->new(
339 type => $k,
340 id => $id,
341 cache => $self->cache,
342 );
343 2 });
344 2 @result;
345 }
346  
347 14 ▶sub fragment_from_resource_url {
Subroutine fragment_from_resource_url: called CC=1 SCAR=0.0
Pod undocumented
348 14 my ($self, $url) = @_;
349 14 (split '/', $url)[-2, -1];
350 }
351  
352 2 ▶sub _make_text_accessor {
Subroutine _make_text_accessor: called CC=3 SCAR=11.0
353 2 my $package = shift;
354 2 for (@_) {
355 8 my ($name, $look_for);
356 8 ▶ if (ref($_) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 1 7 8
357 1 ($name, $look_for) = @$_;
358 } else {
359 7 $name = $_;
360 7 $look_for = $_;
361 }
362 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
363 8 ▶ *{"${package}::$name"} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
364 6 my $thing;
365 6 ▶ ($thing = shift->dom->at($look_for)) && $thing->text;
Branch: 1 branch
BranchTrueFalseTotal
$thing = (shift())->dom->at($look_for) and $thing->text 6 0 6
366 8 };
367 }
368 }
369  
370  
371 2 1; # End of WebService::Libris
↑ Summary lib/WebService/Libris/Book.pm »