ILS/OverDrive.pm

filter: Statementstmt 26.0% 39 / 150 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 37.0% 10 / 27 Podpod 70.0% 7 / 10 totaltotal 19.6% 56 / 285 CC 46 SCAR 43.5
CC
46
Cov
16%
search
272.0
_item_metadata_xlate
72.0
_error_from_json
42.0
CRAP 77.3
« ILS/RecordedBooks.pm ↑ Summary ILS.pm »
1 package WebService::ILS::OverDrive;
2  
3 3 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =encoding utf-8
6  
7 =head1 NAME
8  
9 WebService::ILS::OverDrive - WebService::ILS module for OverDrive services
10  
11 =head1 SYNOPSIS
12  
13 use WebService::ILS::OverDrive::Library;
14 or
15 use WebService::ILS::OverDrive::Patron;
16  
17 =head1 DESCRIPTION
18  
19 L<WebService::ILS::OverDrive::Library> - anonymous discovery
20 services - no individual user credentials required
21  
22 L<WebService::ILS::OverDrive::Patron> - discovery and circulation
23 services that require individual user credentials
24  
25 See L<WebService::ILS>
26  
27 =cut
28  
29 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 3 ▶use parent qw(WebService::ILS::JSON);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 3 ▶use constant API_VERSION => "v1";
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 3 ▶use constant DISCOVERY_API_URL => "http://api.overdrive.com/";
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use constant TEST_DISCOVERY_API_URL => "http://integration.api.overdrive.com/";
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 =head1 CONSTRUCTOR
41  
42 =head2 new (%params_hash or $params_hashref)
43  
44 =head3 Additional constructor params:
45  
46 =over 10
47  
48 =item C<test> => if set to true use OverDrive test API urls
49  
50 =back
51  
52 =cut
53  
54 3 ▶use Class::Tiny qw(
Subroutine BEGIN: called CC=1 SCAR=1.2
55 collection_token
56 test
57 ), {
58 0 ▶ _discovery_api_url => sub { $_[0]->test ? TEST_DISCOVERY_API_URL : DISCOVERY_API_URL },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->test ? : 0 0 0
59 3 };
60  
61 3 __PACKAGE__->_set_param_spec({
62 test => { required => 0 },
63 });
64  
65 =head1 DISCOVERY METHODS
66  
67 =head2 search ($params_hashref)
68  
69 =head3 Additional input params:
70  
71 =over 16
72  
73 =item C<no_details> => if true, no metadata calls will be made for result items;
74  
75 only id, title, rating and media will be available
76  
77 =back
78  
79 =cut
80  
81 3 my %SORT_XLATE = (
82 available_date => "dateadded",
83 rating => "starrating",
84 publication_date => undef, # not available
85 );
86 0 ▶sub search {
Subroutine search: not called CC=16 SCAR=56.1
Pod documented
87 0 my $self = shift;
88 0 ▶ my $params = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
89  
90 0 my $short_response = delete $params->{no_details};
91  
92 0 my $url = $self->products_url;
93  
94 0 ▶ if (my $query = delete $params->{query}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $query = delete $params->{"query"}) 0 0 0
95 0 ▶ $query = join " ", @$query if ref $query;
Branch: 1 branch
BranchTrueFalseTotal
if ref $query 0 0 0
96 0 $params->{q} = $query;
97 }
98 0 my $page_size = delete $params->{page_size};
99 0 ▶ $params->{limit} = $page_size if $page_size;
Branch: 1 branch
BranchTrueFalseTotal
if $page_size 0 0 0
100 0 ▶ if (my $page_number = delete $params->{page}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $page_number = delete $params->{"page"}) 0 0 0
101 0 ▶ croak "page_size must be specified for paging" unless $params->{limit};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"limit"} 0 0 0
102 0 $params->{offset} = ($page_number - 1)*$page_size;
103 }
104 0 ▶ if (my $sort = delete $params->{sort}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sort = delete $params->{"sort"}) 0 0 0
105 0 $params->{sort} = join ",", @{ $self->_parse_sort_string($sort, \%SORT_XLATE) };
106 }
107 0 ▶ $params->{formats} = join ",", @{$params->{formats}} if ref $params->{formats};
Branch: 1 branch
BranchTrueFalseTotal
if ref $params->{"formats"} 0 0 0
108  
109 0 my $res = $self->get_response($url, $params);
110 0 my @items;
111 0 ▶ foreach (@{$res->{products} || []}) {
Condition0%0/2
$res->{'products'} || []
l!l
00
MC/DC0%0/1
$res->{'products'} || []
$res->{'products'}
Truth table0%0/2
A
A= $res->{'products'}
Aresult
11
00
112 0 my $item;
113 0 ▶ if ($short_response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($short_response) { } 0 0 0
114 0 $item = $self->_item_xlate($_);
115 } else {
116 0 ▶ my $native_metadata = $self->native_item_metadata($_) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $native_metadata = $self->native_item_metadata($_) or next 0 0 0
117 0 $item = $self->_item_metadata_xlate($native_metadata);
118 }
119 0 ▶ next unless $item;
Branch: 1 branch
BranchTrueFalseTotal
unless $item 0 0 0
120 0 push @items, $item;
121 }
122 0 my $tot = $res->{totalItems};
123 0 my %ret = (
124 total => $tot,
125 items => \@items,
126 );
127 0 ▶ if (my $page_size = $res->{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $page_size = $res->{"limit"}) 0 0 0
128 0 my $pages = int($tot/$page_size);
129 0 ▶ $pages++ if $tot > $page_size*$pages;
Branch: 1 branch
BranchTrueFalseTotal
if $tot > $page_size * $pages 0 0 0
130 0 $ret{pages} = $pages;
131 0 $ret{page_size} = $page_size;
132 0 $ret{page} = $res->{offset}/$page_size + 1;
133 }
134 0 return \%ret;
135 }
136  
137 3 my %SEARCH_RESULT_ITEM_XLATE = (
138 id => "id",
139 title => "title",
140 subtitle => "subtitle",
141 starRating => "rating",
142 mediaType => "media",
143 );
144 0 ▶sub _item_xlate {
Subroutine _item_xlate: not called CC=3 SCAR=24.8
145 0 my $self = shift;
146 0 my $item = shift;
147  
148 0 my $std_item = $self->_result_xlate($item, \%SEARCH_RESULT_ITEM_XLATE);
149  
150 0 ▶ if (my $formats = $item->{formats}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $formats = $item->{"formats"}) 0 0 0
151 0 $std_item->{formats} = [map $_->{id}, @$formats];
152 }
153  
154 0 ▶ if (my $images = $item->{images}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $images = $item->{"images"}) 0 0 0
155 0 $std_item->{images} = {map { $_ => $images->{$_}{href} } keys %$images};
156 }
157  
158 # XXX
159 #if (my $details = $item->{contentDetails}) {
160 # $std_item->{details_url} = $details->{href};
161 #}
162  
163 0 return $std_item;
164 }
165  
166 3 my %METADATA_XLATE = (
167 id => "id",
168 mediaType => "media",
169 title => "title",
170 publisher => "publisher",
171 shortDescription => "subtitle",
172 starRating => "rating",
173 popularity => "popularity",
174 );
175 0 ▶sub item_metadata {
Subroutine item_metadata: not called CC=2 SCAR=17.9
Pod documented
176 0 my $self = shift;
177 0 ▶ my $id = shift or croak "No item id";
Branch: 1 branch
BranchTrueFalseTotal
my $id = shift() or croak("No item id") 0 0 0
178 0 my $native_metadata = $self->get_response($self->products_url."/$id/metadata");
179 0 return $self->_item_metadata_xlate($native_metadata);
180 }
181  
182 0 ▶sub _item_metadata_xlate {
Subroutine _item_metadata_xlate: not called CC=8 SCAR=42.8
183 0 my $self = shift;
184 0 ▶ my $metadata = shift or croak "No native metadata";
Branch: 1 branch
BranchTrueFalseTotal
my $metadata = shift() or croak("No native metadata") 0 0 0
185  
186 0 my $item = $self->_result_xlate($metadata, \%METADATA_XLATE);
187  
188 0 my @authors;
189 0 foreach (@{ $metadata->{creators} }) {
190 0 ▶ push @authors, $_->{name} if $_->{role} eq "Author";
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"role"} eq "Author" 0 0 0
191 }
192 0 $item->{author} = join ", ", @authors;
193  
194 0 ▶ if (my $images = $metadata->{images}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $images = $metadata->{"images"}) 0 0 0
195 0 $item->{images} = {map { $_ => $images->{$_}{href} } keys %$images};
196 }
197  
198 0 ▶ if (my $languages = $metadata->{languages}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $languages = $metadata->{"languages"}) 0 0 0
199 0 $item->{languages} = [map $_->{name}, @$languages];
200 }
201  
202 0 ▶ if (my $subjects = $metadata->{subjects}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subjects = $metadata->{"subjects"}) 0 0 0
203 0 $item->{subjects} = [map $_->{value}, @$subjects];
204 }
205  
206 0 ▶ if (my $formats = $metadata->{formats}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $formats = $metadata->{"formats"}) 0 0 0
207 0 $item->{formats} = [map $_->{id}, @$formats];
208 }
209  
210 0 return $item;
211 }
212  
213 3 my %AVAILABILITY_RESULT_XLATE = (
214 id => "id",
215 available => "available",
216 copiesAvailable => "copies_available",
217 copiesOwned => "copies_owned",
218 availabilityType => "type",
219 );
220 0 ▶sub item_availability {
Subroutine item_availability: not called CC=2 SCAR=17.9
Pod documented
221 0 my $self = shift;
222 0 ▶ my $id = shift or croak "No item id";
Branch: 1 branch
BranchTrueFalseTotal
my $id = shift() or croak("No item id") 0 0 0
223 0 return $self->_result_xlate(
224 $self->get_response($self->products_url."/$id/availability"),
225 \%AVAILABILITY_RESULT_XLATE
226 );
227 }
228  
229 0 ▶sub is_item_available {
Subroutine is_item_available: not called CC=5 SCAR=34.0
Pod documented
230 0 my $self = shift;
231 0 ▶ my $id = shift or croak "No item id";
Branch: 1 branch
BranchTrueFalseTotal
my $id = shift() or croak("No item id") 0 0 0
232 0 my $type = shift;
233  
234 0 ▶ my $availability = $self->item_availability($id) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $availability = $self->item_availability($id) or return 0 0 0
235 0 ▶ return unless $availability->{available};
Branch: 1 branch
BranchTrueFalseTotal
unless $availability->{"available"} 0 0 0
236 0 ▶ return !$type || $type eq $availability->{type};
Condition0%0/3
!$type || $type eq $availability->{'type'}
l!l&&r!l&&!r
000
MC/DC0%0/2
!$type || $type eq $availability->{'type'}
!$type $type eq $availability->{'type'}
Truth table0%0/3
not(A) || B
A= !$typeB= $type eq $availability->{'type'}
ABresult
1X1
011
000
237 }
238  
239 =head1 NATIVE METHODS
240  
241 =head2 native_search ($params_hashref)
242  
243 See L<https://developer.overdrive.com/apis/search>
244  
245 =head2 native_search_[next|prev|first|last] ($data_as returned_by_native_search*)
246  
247 For iterating through search result pages. Each native_search_*() method
248 accepts record returned by any native_search*() method as input.
249  
250 Example:
251  
252 my $res = $od->native_search({q => "Dogs"});
253 while ($res) {
254 do_something($res);
255 $res = $od->native_search_next($res);
256 }
257 or
258 my $res = $od->native_search({q => "Dogs"});
259 my $last = $od->native_search_last($res);
260 my $next_to_last = $od->native_search_prev($last);
261 my $first = $od->native_search_first($next_to_last)
262 # Same as $od->native_search_first($last)
263 # Same as $res
264  
265 =cut
266  
267 # params: q, limit, offset, formats, sort ? availability
268 0 ▶sub native_search {
Subroutine native_search: not called CC=1 SCAR=6.9
Pod documented
269 0 my $self = shift;
270 0 my $search_params = shift;
271  
272 0 return $self->get_response($self->products_url, $search_params);
273 }
274  
275 3 foreach my $f (qw(next prev first last)) {
276 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
277 12 my $method = "native_search_$f";
278 0 ▶ *$method = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
279 0 my $self = shift;
280 0 ▶ my $search_data = shift or croak "No search result data";
Branch: 1 branch
BranchTrueFalseTotal
my $search_data = shift() or croak("No search result data") 0 0 0
281 0 ▶ my $url = _extract_link($search_data, $f) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $url = _extract_link($search_data, $f) or return 0 0 0
282 0 return $self->get_response($url);
283 }
284 12 }
285  
286 # Item API
287  
288 =head2 native_item_metadata ($item_data as returned by native_search*)
289  
290 =head2 native_item_availability ($item_data as returned by native_search*)
291  
292 Example:
293  
294 my $res = $od->native_search({q => "Dogs"});
295 foreach (@{ $res->{products} }) {
296 my $meta = $od->native_item_metadata($_);
297 my $availability = $od->native_item_availability($_);
298 ...
299 }
300  
301 =cut
302  
303 0 ▶sub native_item_metadata {
Subroutine native_item_metadata: not called CC=3 SCAR=24.8
Pod documented
304 0 my $self = shift;
305 0 ▶ my $item = shift or croak "No item record";
Branch: 1 branch
BranchTrueFalseTotal
my $item = shift() or croak("No item record") 0 0 0
306  
307 0 ▶ my $url = _extract_link($item, 'metadata') or die "No metadata link\n";
Branch: 1 branch
BranchTrueFalseTotal
my $url = _extract_link($item, "metadata") or die "No metadata link\n" 0 0 0
308 0 return $self->get_response($url);
309 }
310  
311 0 ▶sub native_item_availability {
Subroutine native_item_availability: not called CC=2 SCAR=17.9
Pod documented
312 0 my $self = shift;
313 0 ▶ my $item = shift or croak "No item record";
Branch: 1 branch
BranchTrueFalseTotal
my $item = shift() or croak("No item record") 0 0 0
314 0 return $self->get_response(_extract_link($item, 'availability'));
315 }
316  
317 # Discovery helpers
318  
319 0 ▶sub discovery_action_url {
Subroutine discovery_action_url: not called CC=1 SCAR=6.9
Pod undocumented
320 0 my $self = shift;
321 0 my $action = shift;
322 0 return $self->_discovery_api_url.$self->API_VERSION.$action;
323 }
324  
325 0 ▶sub products_url {
Subroutine products_url: not called CC=3 SCAR=24.8
Pod undocumented
326 0 my $self = shift;
327  
328 0 ▶ my $collection_token = $self->collection_token or die "No collection token";
Branch: 1 branch
BranchTrueFalseTotal
my $collection_token = $self->collection_token or die "No collection token" 0 0 0
329  
330 0 ▶ if ($collection_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($collection_token) 0 0 0
331 0 return $self->_discovery_api_url.$self->API_VERSION."/collections/$collection_token/products";
332 }
333 }
334  
335 # API helpers
336  
337 0 ▶sub _extract_link {
Subroutine _extract_link: not called CC=2 SCAR=17.9
338 0 my ($data, $link) = @_;
339 my $href = $data->{links}{$link}{href}
340 0 ▶ or croak "No '$link' url in data";
Branch: 1 branch
BranchTrueFalseTotal
my $href = $data->{"links"}{$link}{"href"} or croak("No '${link}' url in data") 0 0 0
341 }
342  
343 # Utility methods
344  
345 0 ▶sub _basic_callback { return $_[0]; }
Subroutine _basic_callback: not called CC=1 SCAR=6.9
346  
347 # This is not exatly how we meant to use with_get_request()
348 # ie processing should be placed within the callback.
349 # However, if all goes well, it is faster (from the development perspective)
350 # this way.
351 0 ▶sub get_response {
Subroutine get_response: not called CC=2 SCAR=17.9
Pod undocumented
352 0 my $self = shift;
353 0 ▶ my $url = shift or croak "No url";
Branch: 1 branch
BranchTrueFalseTotal
my $url = shift() or croak("No url") 0 0 0
354 0 my $get_params = shift; # hash ref
355  
356 0 return $self->with_get_request(\&_basic_callback, $url, $get_params);
357 }
358  
359 0 ▶sub _error_from_json {
Subroutine _error_from_json: not called CC=6 SCAR=37.4
360 0 my $self = shift;
361 0 ▶ my $data = shift or croak "No json data";
Branch: 1 branch
BranchTrueFalseTotal
my $data = shift() or croak("No json data") 0 0 0
362 0 ▶ my $error = join " ", grep defined($_), $data->{errorCode}, $data->{error_description} || $data->{error} || $data->{message} || $data->{Message};
Condition0%0/9
$data->{'error_description'} || $data->{'error'} || $data->{'message'} || $data->{'Message'}
l!l&&r!l&&!r
000
$data->{"error_description"} or $data->{"error"} or $data->{"message"}
l!l&&r!l&&!r
000
$data->{"error_description"} or $data->{"error"}
l!l&&r!l&&!r
000
MC/DC0%0/4
$data->{'error_description'} || $data->{'error'} || $data->{'message'} || $data->{'Message'}
$data->{"error_description"} $data->{"error"} $data->{"message"} $data->{'Message'}
Truth table0%0/5
A or B or C || D
A= $data->{"error_description"}B= $data->{"error"}C= $data->{"message"}D= $data->{'Message'}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
363 0 ▶ $error = "$error\n" if $error; # strip code line when dying
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
364 0 return $error;
365 }
366  
367 3 1;
368  
369 __END__
« ILS/RecordedBooks.pm ↑ Summary ILS.pm »