File Coverage

blib/lib/Museum/Rijksmuseum/Object.pm
Criterion Covered Total %
statement 27 59 45.7
branch 0 12 0.0
condition n/a
subroutine 9 14 64.2
pod 3 3 100.0
total 39 88 44.3


line stmt bran cond sub pod time code
1             package Museum::Rijksmuseum::Object;
2              
3 1     1   146535 use strictures 2;
  1         2163  
  1         49  
4              
5 1     1   517 use Carp;
  1         2  
  1         92  
6 1     1   503 use JSON::MaybeXS;
  1         19686  
  1         129  
7 1     1   1078 use LWP::UserAgent;
  1         79464  
  1         38  
8 1     1   969 use Moo;
  1         6107  
  1         6  
9 1     1   1315 use URI;
  1         1  
  1         22  
10 1     1   495 use URI::Encode qw( uri_encode );
  1         15358  
  1         60  
11 1     1   487 use URI::QueryParam;
  1         102  
  1         24  
12              
13 1     1   523 use namespace::clean;
  1         14559  
  1         6  
14              
15             =head1 NAME
16              
17             Museum::Rijksmuseum::Object - Access the Rijksmuseum object metadata API
18              
19             =head1 VERSION
20              
21             Version 0.05
22              
23             =cut
24              
25             our $VERSION = '0.05';
26              
27             =head1 DEPRECATION ALERT
28              
29             The Rijksmuseum has removed the API that this object API module uses, the best replacement is to use the OAI-PMH API directly, perhaps using C.
30             C continues to work as it was already using OAI-PMH (though has been updated with a new base URL.)
31              
32             =head1 SYNOPSIS
33              
34             Access the collection, collection details, and collection image endpoints of the Rijksmuseum "Rijksdata" API.
35              
36             use Museum::Rijksmuseum::Object;
37              
38             my $api = Museum::Rijksmuseum::Object->new( key => 'abc123xyz', culture => 'en' );
39             my $search_result = $api->search( involvedMaker => 'Rembrandt van Rijn' );
40             die $search_result->{error} if $search_result->{error};
41             print "Results: $search_result->{count}\n";
42             for my $item ( $search_result->{artObjects}->@* ) {
43             print "Id: $item->{id}\n";
44             }
45              
46             Refer to the L
47             for information on query and response formats. Be aware that the API expects
48             camelCase on its parameters and this module follows this convention.
49              
50             =head1 SUBROUTINES/METHODS
51              
52             =head2 new
53              
54             my $api = Museum::Rijksmuseum::Object->new(
55             key => 'abc123xyz', # the API key supplied by the Rijksmuseum
56             culture => 'en', # the 'culture' parameter, determines the
57             # language for searching and results
58             );
59              
60             Creates a new object instance. The C and C parameters are required.
61              
62             =cut
63              
64             =head2 search
65              
66             my $result = $api->search(
67             p => 3, # page 3
68             ps => 10, # 10 results per page
69             q => 'vermeer', # a general search term
70             imgonly => 1, # image only?
71             );
72              
73             Searches the collection. See the API documentation for parameter details.
74              
75             =cut
76              
77             sub search {
78 0     0 1   my ( $self, %params ) = @_;
79              
80 0           my $url = $self->_build_url( {}, \%params );
81 0           return $self->_fetch_url($url);
82             }
83              
84             =head2 fetch
85              
86             my $result = $api->fetch('SK-C-5');
87              
88             Fetches an individual item from the collection. The parameter is the
89             C as returned by the C call.
90              
91             =cut
92              
93             sub fetch {
94 0     0 1   my ( $self, $object_number ) = @_;
95              
96 0 0         carp "An object number must be provided to 'fetch'" unless defined $object_number;
97 0           my $url = $self->_build_url( { object_number => $object_number }, {} );
98 0           return $self->_fetch_url($url);
99             }
100              
101             =head2 image_info
102              
103             my $result = $api->image_info('SK-C-5');
104              
105             Fetches the information required to build the image tiles for a particular
106             C.
107              
108             =cut
109              
110             sub image_info {
111 0     0 1   my ( $self, $object_number ) = @_;
112              
113 0 0         carp "An object number must be provided to 'image_info'" unless defined $object_number;
114 0           my $url = $self->_build_url(
115             {
116             object_number => $object_number,
117             tiles => 1,
118             },
119             {}
120             );
121 0           return $self->_fetch_url($url);
122             }
123              
124             sub _build_url {
125 0     0     my ( $self, $path, $params ) = @_;
126              
127             # Determine which URL form we need
128 0           my $url;
129 0 0         if ( $path->{tiles} ) {
    0          
130             $url = sprintf(
131             'https://www.rijksmuseum.nl/api/%s/collection/%s/tiles',
132             uri_encode( $self->culture ),
133             uri_encode( $path->{object_number} )
134 0           );
135             } elsif ( $path->{object_number} ) {
136             $url = sprintf(
137             'https://www.rijksmuseum.nl/api/%s/collection/%s',
138             uri_encode( $self->culture ),
139             uri_encode( $path->{object_number} )
140 0           );
141             } else {
142 0           $url =
143             sprintf( 'https://www.rijksmuseum.nl/api/%s/collection', uri_encode( $self->culture ) );
144             }
145 0           $url = URI->new($url);
146              
147             # Add query parameters
148 0           $url->query_param( key => $self->key );
149 0 0         if ($params) {
150 0           for my $p ( keys %$params ) {
151 0           $url->query_param( $p => $params->{$p} );
152             }
153             }
154              
155 0           return $url->as_string;
156             }
157              
158             sub _fetch_url {
159 0     0     my ( $self, $url ) = @_;
160              
161 0           my $ua = LWP::UserAgent->new;
162 0           $ua->agent("Museum::Rijksmuseum::Object/$VERSION");
163              
164 0           my $req = HTTP::Request->new( GET => $url );
165              
166 0           my $res = $ua->request($req);
167 0 0         if ( $res->is_success ) {
168 0           my $json = JSON::MaybeXS->new();
169 0           return $json->decode( $res->content );
170             } else {
171 0           return { error => $res->status_line };
172             }
173             }
174              
175             =head1 ATTRIBUTES
176              
177             =head2 key
178              
179             The API key provided by the Rijksmuseum.
180              
181             =cut
182              
183             has key => (
184             is => 'rw',
185             required => 1,
186             );
187              
188             =head2 culture
189              
190             The 'culture' that the API will return data to you in, and perform searches in.
191             Typically 'en' or 'nl'.
192              
193             =cut
194              
195             has culture => (
196             is => 'rw',
197             required => 1
198             );
199              
200             =head1 SEE ALSO
201              
202             For bulk-ingestion of data using the Rijksmuseum OAI-PMH API, see
203             L.
204              
205             =head1 AUTHOR
206              
207             Robin Sheat, C<< >>
208              
209             =head1 TODO
210              
211             =over 4
212              
213             =item Make a heavier interface
214              
215             At the moment this is a very thin interface over the Rijksmuseum API. It could
216             be improved by having helpers to do things, for example optionally
217             automatically fetching and stitching images.
218              
219             =back
220              
221             =cut
222              
223             =head1 BUGS
224              
225             Please report any bugs or feature requests to C, or through
226             the web interface at L. I will be notified, and then you'll
227             automatically be notified of progress on your bug as I make changes.
228              
229             Alternately, use the tracker on the repository page at L.
230              
231              
232             =head1 SUPPORT
233              
234             You can find documentation for this module with the perldoc command.
235              
236             perldoc Museum::Rijksmuseum::Object
237              
238              
239             You can also look for information at:
240              
241             =over 4
242              
243             =item * Repository page (report bugs here)
244              
245             L
246              
247             =item * RT: CPAN's request tracker (or here)
248              
249             L
250              
251             =item * Search CPAN
252              
253             L
254              
255              
256             =back
257              
258              
259             =head1 ACKNOWLEDGEMENTS
260              
261              
262             =head1 LICENSE AND COPYRIGHT
263              
264             This software is Copyright (c) 2023 by Robin Sheat.
265              
266             This is free software, licensed under:
267              
268             The Artistic License 2.0 (GPL Compatible)
269              
270              
271             =cut
272              
273             1; # End of Museum::Rijksmuseum::Object