Bug 25172: Adjust tests
[koha.git] / Koha / Illrequest / Availability.pm
blob9013c1749ae53a90a95f25d9d8e98da56b02605f
1 package Koha::Illrequest::Availability;
3 # Copyright 2019 PTFS Europe Ltd
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use JSON;
23 use MIME::Base64 qw( encode_base64 );
24 use URI::Escape qw( uri_escape );
25 use Encode qw( encode );
27 use Koha::Plugins;
29 =head1 NAME
31 Koha::Illrequest::Availability - Koha ILL Availability Searching
33 =head1 SYNOPSIS
35 Object-oriented class that provides availability searching via
36 availability plugins
38 =head1 DESCRIPTION
40 This class provides the ability to identify and fetch API services
41 that can be used to search for item availability
43 =head1 API
45 =head2 Class Methods
47 =head3 new
49 my $availability = Koha::Illrequest::Logger->new($metadata);
51 Create a new Koha::Illrequest::Availability object.
52 We also store the metadata to be used for searching
54 =cut
56 sub new {
57 my ( $class, $metadata ) = @_;
58 my $self = {};
60 $self->{metadata} = $metadata;
62 bless $self, $class;
64 return $self;
67 =head3 get_services
69 my $services = Koha::Illrequest::Availability->get_services($params);
71 Given our metadata, iterate plugins with the right method and
72 check if they can service our request and, if so, return an arrayref
73 of services. Optionally accept a hashref specifying additional filter
74 parameters
76 =cut
78 sub get_services {
79 my ( $self, $params ) = @_;
81 my $plugin_filter = {
82 method => 'ill_availability_services'
85 if ($params->{metadata}) {
86 $plugin_filter->{metadata} = $params->{metadata};
89 my @candidates = Koha::Plugins->new()->GetPlugins($plugin_filter);
90 my @services = ();
91 foreach my $plugin(@candidates) {
92 my $valid_service = $plugin->ill_availability_services({
93 metadata => $self->{metadata},
94 ui_context => $params->{ui_context}
95 });
96 push @services, $valid_service if $valid_service;
99 return \@services;
102 =head3 prep_metadata
104 my $prepared = Koha::Illrequest::Availability->prep_metadata($metadata);
106 Given our metadata, return a string representing that metadata that can be
107 passed in a URL (encoded in JSON then Base64 encoded)
109 =cut
111 sub prep_metadata {
112 my ( $self, $metadata ) = @_;
114 # We sort the metadata hashref by key before encoding it, primarily
115 # so this function returns something predictable that we can test!
116 my $json = JSON->new;
117 $json->canonical([1]);
118 return uri_escape(encode_base64(encode('utf-8',$json->encode($metadata))));
121 =head1 AUTHOR
123 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
125 =cut