| 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 | 3 | ▶ | use 5.008; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | 3 | ▶ | use vars qw($VERSION); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 30 | 3 | ▶ | use base 'Catalyst::Model'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 3 | ▶ | use NEXT; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 3 | ▶ | use WebService::ISBNDB::API; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 3 | $VERSION = '0.12'; | |||||||||||||||||||
| 36 | 3 | $VERSION = eval $VERSION; ## no critic | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | BEGIN | ||||||||||||||||||||
| 39 | 3 | ▶ | { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 40 | # Don't let Perl::Critic whinge about this; I don't want to add | ||||||||||||||||||||
| 41 | # Sub::Installer to my deps list... | ||||||||||||||||||||
| 42 | 3 | ▶ | no strict 'refs'; ## no critic | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 3 | my %map = ( Authors => 'author', | |||||||||||||||||||
| 45 | Books => 'book', | ||||||||||||||||||||
| 46 | Categories => 'category', | ||||||||||||||||||||
| 47 | Publishers => 'publisher', | ||||||||||||||||||||
| 48 | Subjects => 'subject' ); | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 3 | for my $method (keys %map) | |||||||||||||||||||
| 51 | { | ||||||||||||||||||||
| 52 | 15 | my $name = "find_$map{$method}"; | |||||||||||||||||||
| 53 | *$name = sub | ||||||||||||||||||||
| 54 | 6 | ▶ | { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 55 | 6 | my ($self, $id) = @_; | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 6 | my $api = $self->get_agent; | |||||||||||||||||||
| 58 | 6 | my $obj; | |||||||||||||||||||
| 59 | 6 | eval { $obj = $api->find($method => $id); }; | |||||||||||||||||||
| 60 | 6 | ▶ | die "$@" if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 6 | $obj; | |||||||||||||||||||
| 63 | 15 | }; | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 15 | $name = 'search_' . lc $method; | |||||||||||||||||||
| 66 | *$name = sub | ||||||||||||||||||||
| 67 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 68 | 1 | my ($self, $args) = @_; | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 1 | my $api = $self->get_agent; | |||||||||||||||||||
| 71 | 1 | my $iter; | |||||||||||||||||||
| 72 | 1 | eval { $iter = $api->search($method => $args); }; | |||||||||||||||||||
| 73 | 1 | ▶ | die "$@" if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 1 | $iter; | |||||||||||||||||||
| 76 | 15 | }; | |||||||||||||||||||
| 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 | 8 | ▶ | { | ||||||||||||||||||
|
Subroutine
get_agent: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 96 | 8 | my $self = shift; | |||||||||||||||||||
| 97 | 8 | my $config = $self->config; | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 8 | ▶ | $config->{agent} || $self->allocate_agent($config); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 2 | ▶ | { | ||||||||||||||||||
|
Subroutine
allocate_agent: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 121 | 2 | my ($self, $config) = @_; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | my $key = $config->{access_key} || | ||||||||||||||||||||
| 124 | 2 | ▶ | WebService::ISBNDB::API->get_default_api_key; | ||||||||||||||||||
|
Condition66%2/3
$config->{'access_key'} || 'WebService::ISBNDB::API'->get_default_api_key
MC/DC0%0/2
$config->{'access_key'} || 'WebService::ISBNDB::API'->get_default_api_key
$config->{'access_key'}
'WebService::ISBNDB::API'->get_default_api_key
Truth table66%2/3
A || B
A= $config->{'access_key'}B= 'WebService::ISBNDB::API'->get_default_api_key
| |||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 2 | $config->{agent} = WebService::ISBNDB::API->new({ api_key => $key }); | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 3 | 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 | ||||||||||||||||||||