use LWP::UserAgent;
use MIME::Base64;
+use Business::ISBN;
+
my $AC = __PACKAGE__;
my $keyhash;
my $cachekey;
+ $cachekey = ($keytype eq "isbn") ? $keyvalue : $keytype . '_' . $keyvalue;
+
+ child_init() unless $handler;
+
+ return Apache2::Const::NOT_FOUND unless $handler and $type and $format and $cachekey;
+
+ my $err;
+ my $data;
+ my $method = "${type}_${format}";
+
+ return Apache2::Const::NOT_FOUND unless $handler->can($method);
+ return $res if defined($res = $AC->serve_from_cache($type, $format, $cachekey));
+ return Apache2::Const::NOT_FOUND unless $AC->lookups_enabled;
+
if ($keytype eq "isbn") { # if this request uses isbn for the key
# craft a hash with the single isbn, because that's all we will have
$keyhash = {};
$keyhash->{"isbn"} = [$keyvalue];
- $cachekey = $keyvalue;
} else {
my $key_data = get_rec_keys($keyvalue);
- my @raw_isbns = grep {$_->{tag} eq '020'} @$key_data;
+ my @isbns = grep {$_->{tag} eq '020'} @$key_data;
my @upcs = grep {$_->{tag} eq '024'} @$key_data;
- my @isbns = [map {
- my $isbn_obj = Business::ISBN->new($_);
- return $isbn_obj->as_string([]);
- } @raw_isbns];
+ map {
+ my $isbn_obj = Business::ISBN->new($_->{value});
+ my $isbn_str;
+ $isbn_str = $isbn_obj->as_string([]) if defined($isbn_obj);
+ $_->{value} = $isbn_str;
+ undef $_ if !defined($_->{value});
+ } @isbns;
$keyhash = {
isbn => [map {$_->{value}} @isbns],
upc => [map {$_->{value}} @upcs]
};
-
- $cachekey = $keytype . '_' . $keyvalue;
}
- child_init() unless $handler;
-
- return Apache2::Const::NOT_FOUND unless $handler and $type and $format and $keyhash;
-
- my $err;
- my $data;
- my $method = "${type}_${format}";
-
- return Apache2::Const::NOT_FOUND unless $handler->can($method);
- return $res if defined($res = $AC->serve_from_cache($type, $format, $cachekey));
- return Apache2::Const::NOT_FOUND unless $AC->lookups_enabled;
-
- # XXX DEBUG
- use OpenSRF::Utils::JSON;
- $logger->info("Added Content Keys: " . OpenSRF::Utils::JSON->perl2JSON($keyhash));
+ return Apache2::Const::NOT_FOUND unless @{$keyhash->{isbn}} || @{$keyhash->{upc}};
try {
$data = $handler->$method($keyhash);
};
return Apache2::Const::NOT_FOUND if $err;
- next unless $data;
if(!$data) {
# if the AC lookup found no corresponding data, cache that information