Bug 20473: Whitespace
[koha.git] / Koha / Illrequest / Availability.pm
1 package Koha::Illrequest::Availability;
2
3 # Copyright 2019 PTFS Europe Ltd
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use JSON;
23 use MIME::Base64 qw( encode_base64 );
24 use URI::Escape qw( uri_escape );
25 use Encode qw( encode );
26
27 use Koha::Plugins;
28
29 =head1 NAME
30
31 Koha::Illrequest::Availability - Koha ILL Availability Searching
32
33 =head1 SYNOPSIS
34
35 Object-oriented class that provides availability searching via
36 availability plugins
37
38 =head1 DESCRIPTION
39
40 This class provides the ability to identify and fetch API services
41 that can be used to search for item availability
42
43 =head1 API
44
45 =head2 Class Methods
46
47 =head3 new
48
49     my $availability = Koha::Illrequest::Logger->new($metadata);
50
51 Create a new Koha::Illrequest::Availability object.
52 We also store the metadata to be used for searching
53
54 =cut
55
56 sub new {
57     my ( $class, $metadata ) = @_;
58     my $self  = {};
59
60     $self->{metadata} = $metadata;
61
62     bless $self, $class;
63
64     return $self;
65 }
66
67 =head3 get_services
68
69     my $services = Koha::Illrequest::Availability->get_services($params);
70
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
75
76 =cut
77
78 sub get_services {
79     my ( $self, $params ) = @_;
80
81     my $plugin_filter = {
82         method => 'ill_availability_services'
83     };
84
85     if ($params->{metadata}) {
86         $plugin_filter->{metadata} = $params->{metadata};
87     }
88
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;
97     }
98
99     return \@services;
100 }
101
102 =head3 prep_metadata
103
104     my $prepared = Koha::Illrequest::Availability->prep_metadata($metadata);
105
106 Given our metadata, return a string representing that metadata that can be
107 passed in a URL (encoded in JSON then Base64 encoded)
108
109 =cut
110
111 sub prep_metadata {
112     my ( $self, $metadata ) = @_;
113
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))));
119 }
120
121 =head1 AUTHOR
122
123 Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
124
125 =cut
126
127 1;