File Coverage

blib/lib/Catalyst/Model/ISBNDB.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             ###############################################################################
2             #
3             # This file copyright (c) 2008-2009 by Randy J. Ray, all rights reserved
4             #
5             # Copying and distribution are permitted under the terms of the Artistic
6             # License 2.0 (http://www.opensource.org/licenses/artistic-license-2.0.php) or
7             # the GNU LGPL (http://www.opensource.org/licenses/lgpl-license.php).
8             #
9             ###############################################################################
10             #
11             # Description: A Catalyst model for providing access to the isbndb.com
12             # web service.
13             #
14             # Functions: get_agent
15             # allocate_agent
16             #
17             # Libraries: WebService::ISBNDB::API
18             # NEXT
19             #
20             # Global Consts: $VERSION
21             #
22             ###############################################################################
23              
24             package Catalyst::Model::ISBNDB;
25              
26 2     2   35590 use 5.008;
  2         8  
  2         76  
27 2     2   11 use strict;
  2         3  
  2         69  
28 2     2   11 use warnings;
  2         9  
  2         68  
29 2     2   10 use vars qw($VERSION);
  2         4  
  2         96  
30 2     2   16 use base 'Catalyst::Model';
  2         4  
  2         1198  
31              
32 2     2   3518 use NEXT;
  2         8073  
  2         50  
33 2     2   960 use WebService::ISBNDB::API;
  0            
  0            
34              
35             $VERSION = '0.12';
36             $VERSION = eval $VERSION; ## no critic
37              
38             BEGIN
39             {
40             # Don't let Perl::Critic whinge about this; I don't want to add
41             # Sub::Installer to my deps list...
42             no strict 'refs'; ## no critic
43              
44             my %map = ( Authors => 'author',
45             Books => 'book',
46             Categories => 'category',
47             Publishers => 'publisher',
48             Subjects => 'subject' );
49              
50             for my $method (keys %map)
51             {
52             my $name = "find_$map{$method}";
53             *$name = sub
54             {
55             my ($self, $id) = @_;
56              
57             my $api = $self->get_agent;
58             my $obj;
59             eval { $obj = $api->find($method => $id); };
60             die "$@" if $@;
61              
62             $obj;
63             };
64              
65             $name = 'search_' . lc $method;
66             *$name = sub
67             {
68             my ($self, $args) = @_;
69              
70             my $api = $self->get_agent;
71             my $iter;
72             eval { $iter = $api->search($method => $args); };
73             die "$@" if $@;
74              
75             $iter;
76             };
77             }
78             }
79              
80             ###############################################################################
81             #
82             # Sub Name: get_agent
83             #
84             # Description: Get the agent from the config for this class. If none has
85             # been set yet, thread through to the allocation of one.
86             #
87             # Arguments: NAME IN/OUT TYPE DESCRIPTION
88             # $self in ref Object
89             #
90             # Returns: Success: agent instance
91             # Failure: dies
92             #
93             ###############################################################################
94             sub get_agent
95             {
96             my $self = shift;
97             my $config = $self->config;
98              
99             $config->{agent} || $self->allocate_agent($config);
100             }
101              
102             ###############################################################################
103             #
104             # Sub Name: allocate_agent
105             #
106             # Description: Allocate an agent. Store it on the config and return the
107             # new object. Uses the value from calling
108             # WebService::ISBNDB::API::get_default_agent().
109             #
110             # Arguments: NAME IN/OUT TYPE DESCRIPTION
111             # $self in ref Object
112             # $config in hashref The config info for this class
113             # (it was already available)
114             #
115             # Returns: Success: new agent
116             # Failure: dies
117             #
118             ###############################################################################
119             sub allocate_agent
120             {
121             my ($self, $config) = @_;
122              
123             my $key = $config->{access_key} ||
124             WebService::ISBNDB::API->get_default_api_key;
125              
126             $config->{agent} = WebService::ISBNDB::API->new({ api_key => $key });
127             }
128              
129             1;
130              
131             =pod
132              
133             =head1 NAME
134              
135             Catalyst::Model::ISBNDB - Provide Catalyst access to isbndb.com
136              
137             =head1 SYNOPSIS
138              
139             package MyApp::Model::ISBNDB;
140             use base 'Catalyst::Model::ISBNDB';
141              
142             __PACKAGE__->config(access_key => 'XXX');
143              
144             # Within a Catalyst application:
145             my $book = $c->model('ISBNDB')->find_book($isbn);
146              
147             =head1 DESCRIPTION
148              
149             This package provides a Catalyst model that makes requests of the
150             B<isbndb.com> web service, using their published REST interface. The model
151             creates an instance of the B<WebService::ISBNDB::API> class and uses it as
152             a factory for making requests for data in any of the classes supported
153             (Authors, Books, Categories, Publishers and Subjects).
154              
155             =head1 CONFIGURATION
156              
157             The model requires the application to configure a valid access key
158             from the B<isbndb.com> service:
159              
160             __PACKAGE__->config(access_key => $KEY)
161              
162             No other configuration is needed. If you choose to load and manipulate the
163             B<WebService::ISBNDB::API> class directly, you may also use the
164             C<set_default_api_key> method of that class to set your access key.
165              
166             =head1 METHODS
167              
168             The following methods are available to users or sub-classes:
169              
170             =over 4
171              
172             =item find_author($ID)
173              
174             =item find_book($ISBN|$ID)
175              
176             =item find_category($ID)
177              
178             =item find_publisher($ID)
179              
180             =item find_subject($ID)
181              
182             Find a single record of the respective type, using the ID. When searching for
183             a book, the ISBN may be specified instead of an ID. Returns the matching
184             object, C<undef> if no match is found, or throws an exception if an error
185             occurs.
186              
187             =item search_authors($ARGS)
188              
189             =item search_books($ARGS)
190              
191             =item search_categories($ARGS)
192              
193             =item search_publishers($ARGS)
194              
195             =item search_subjects($ARGS)
196              
197             Perform a search for the respective type of records. The parameter is a
198             hash-reference of search terms, as defined in the corresponding manual pages
199             for the types that derive from B<WebService::ISBNDB::API>. The return value
200             is a B<WebService::ISBNDB::Iterator> instance. An exception is thrown on
201             error.
202              
203             =item get_agent
204              
205             This method is called by the previous methods to retrieve the internal API
206             factory agent. If one has not yet been created, it is first created and
207             then returned.
208              
209             =item allocate_agent($CONFIG)
210              
211             The first time get_agent() is called, it calls this method to allocate an
212             instance of B<WebService::ISBNDB::API>. The single parameter is the
213             configuration information from Catalyst. This is used to look up the access
214             key that would have been configured when the application started. The return
215             value is either an API object instance, or an exception is thrown to signal
216             an error.
217              
218             =back
219              
220             =head1 CAVEATS
221              
222             In order to access the B<isbndb.com> service, you must register on their site
223             and create an API access key.
224              
225             =head1 SEE ALSO
226              
227             L<WebService::ISBNDB::API>, L<WebService::ISBNDB::Iterator>
228              
229             =head1 BUGS
230              
231             Please report any bugs or feature requests to C<bug-catalyst-model-isbndb at
232             rt.cpan.org>, or through the web interface at
233             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Model-ISBNDB>. I will
234             be notified, and then you'll automatically be notified of progress on your bug
235             as I make changes.
236              
237             =head1 SUPPORT
238              
239             =over 4
240              
241             =item * RT: CPAN's request tracker
242              
243             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Model-ISBNDB>
244              
245             =item * AnnoCPAN: Annotated CPAN documentation
246              
247             L<http://annocpan.org/dist/Catalyst-Model-ISBNDB>
248              
249             =item * CPAN Ratings
250              
251             L<http://cpanratings.perl.org/d/Catalyst-Model-ISBNDB>
252              
253             =item * Search CPAN
254              
255             L<http://search.cpan.org/dist/Catalyst-Model-ISBNDB>
256              
257             =item * Source code on GitHub
258              
259             L<http://github.com/rjray/catalyst-model-isbndb/tree/master>
260              
261             =back
262              
263             =head1 COPYRIGHT & LICENSE
264              
265             This file and the code within are copyright (c) 2009 by Randy J. Ray.
266              
267             Copying and distribution are permitted under the terms of the Artistic
268             License 2.0 (L<http://www.opensource.org/licenses/artistic-license-2.0.php>) or
269             the GNU LGPL 2.1 (L<http://www.opensource.org/licenses/lgpl-2.1.php>).
270              
271             =head1 AUTHOR
272              
273             Randy J. Ray C<< <rjray@blackperl.com> >>
274              
275             =cut