Safari/Response/Book.pm

filter: Statementstmt 39.6% 44 / 111 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 59.0% 13 / 22 Podpod 100% 4 / 4 totaltotal 39.6% 61 / 154 CC 11 SCAR 21.5
CC
11
Cov
35%
_set_sections
20.0
_set_subjects
20.0
_set_authors
20.0
CRAP 8.6
↑ Summary Safari/Response/Section.pm »
1 package Net::Safari::Response::Book;
2  
3 =head1 NAME
4  
5 Net::Safari::Response::Book - Book object returned by Safari
6  
7 =head1 SYNOPSIS
8  
9 use Net::Safari::Response::Book;
10 my $book = Net::Safari::Response::Book->new(%book_ref);
11  
12 print $book->title;
13  
14 my @sections = $book->sections
15  
16 =head1 DESCRIPTION
17  
18 See Net::Safari for general usage info.
19  
20 In most cases this object is created for you from the Net::Safari::Response::books() method after a Net::Safari->search() call.
21  
22 =head1 ACCESSORS
23 The accessor descriptions are mostly pulled from the official spec:
24 http://safari.oreilly.com/affiliates/?p=response
25  
26 =head2 title()
27  
28 Book title.
29  
30 =head2 isbn()
31  
32 Unformatted ISBN, i.e. there's no dashes, just numbers and occassionally an X.
33  
34 =head2 edition()
35  
36 A text description of the edition, i.e. "First," "Second" ...
37  
38 =head2 authors()
39  
40 Returns an array of Net::Safari::Response::Author objects.
41  
42 =head2 pagenums()
43  
44 Number of pages in book.
45  
46 =head2 pubdate()
47  
48 String representation of the pubdate. There's no guarantees about the format of
49 the date. At the very least expect "July 14, 2004" and "April 1978." If you
50 need a consistent format, use pub_timepiece().
51  
52 =head2 pub_timepiece
53  
54 Time::Piece object representing the publish date. Module tries to be smart
55 about parsing the date so that you can rely on a consistent format.
56  
57 =head2 subjects()
58  
59 An array of subject classification. TODO: get some more info on what these are.
60  
61 =head2 imprint()
62  
63 The name of the division publishing the book. This is what's on the cover. For
64 some books, like Programming Perl, imprint() and publisher() are the same
65 (O'Reilly). For other books, like Mac OS X: The Missing Manual, they're
66 different (publisher=O'Reilly and imprint=Pogue Press).
67  
68 =head2 publisher()
69  
70 The parent company publishing the book. Normally you want imprint().
71  
72 =head2 description()
73  
74 paragraph describing the book.
75  
76 =head2 msrp()
77  
78 Suggested retail price in the United States.
79  
80 =head2 slots()
81  
82 The number of slots that this book occupies in a Safari subscription.
83  
84 =head2 imagesmall()
85  
86 URL to small cover image.
87  
88 =head2 imagemedium()
89  
90 URL to medium cover image.
91  
92 =head2 imagelarge()
93  
94 URL to large cover image.
95 TODO: do these images have consistent sizes.
96  
97 =head2 buyprint()
98  
99 URL to buy a print copy of the book.
100  
101 =head2 sections()
102  
103 An array of Net::Safari::Response::Section objects.
104  
105 =head2 toc()
106  
107 TODO: not sure what this looks like.
108  
109 =head2 url()
110  
111 URL to the book on Safari. This isn't documented in the API, so I can't
112 guarantee that it will always work.
113  
114 =cut
115  
116  
117 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
118  
119 13 our $VERSION = 0.01;
120  
121 13 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 13 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 13 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 13 ▶use Class::Accessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
125 13 ▶use Class::Fields;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 13 ▶use Date::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
127 13 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
128 13 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 13 ▶use Net::Safari::Response::Section;
Subroutine BEGIN: called CC=1 SCAR=0.0
130 13 ▶use Net::Safari::Response::Author;
Subroutine BEGIN: called CC=1 SCAR=0.0
131  
132 13 ▶use base qw(Class::Accessor Class::Fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
133  
134 13 our @BASIC_FIELDS = qw(
135 pagenums
136 pubdate
137 pub_timepiece
138 imagemedium
139 url
140 edition
141 isbn
142 publisher
143 imprint
144 imagesmall
145 msrp
146 description
147 imagelarge
148 buyprint
149 title
150 slots
151 );
152  
153 13 our @CUSTOM_FIELDS = qw(
154 authors
155 sections
156 subjects
157 );
158  
159 13 ▶use fields @BASIC_FIELDS, @CUSTOM_FIELDS;
Subroutine BEGIN: called CC=1 SCAR=0.0
160 13 Net::Safari::Response::Book->mk_accessors(@BASIC_FIELDS);
161  
162 =head1 METHODS
163  
164 =head2 new()
165  
166 $book = Net::Safari::Response::Book->new($ref);
167  
168 Takes a hash represenation of the XML returned by Safari. Normally this is
169 taken care of by Net::Safari::Response.
170  
171 =cut
172  
173 sub new
174 0 ▶{
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
175 0 my ($class, %args) = @_;
176  
177 0 ▶ my $self = bless ({}, ref ($class) || $class);
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
178  
179 0 $self->_init(%args);
180  
181 0 return ($self);
182 }
183  
184 0 ▶sub _init {
Subroutine _init: not called CC=1 SCAR=6.9
185 0 my $self = shift;
186 0 my %args = @_;
187
188 #Title
189 0 $self->title($args{title});
190
191 #ISBN
192 0 $args{isbn} =~ s/-//g;
193 0 $self->isbn($args{isbn});
194  
195 #Edition
196 0 $self->edition($args{edition});
197
198 #Authors
199 0 $self->_set_authors($args{authorgroup});
200
201 #PageNums
202 0 $self->pagenums($args{pagenums});
203  
204 #PubDate
205 0 $self->pubdate($args{pubdate});
206
207 #Pub Time::Piece
208 0 $self->_set_pub_timepiece();
209
210 #Subjects
211 0 $self->_set_subjects($args{subjectset});
212  
213 #Publisher
214 0 $self->publisher($args{publisher}->{publishername});
215  
216 #Imprint
217 0 $self->imprint($args{publisher}->{imprint});
218  
219 #Description
220 0 $self->description($args{description});
221  
222 #MSRP
223 0 $self->msrp($args{msrp});
224  
225 #ImageSmall
226 0 $self->imagesmall($args{imagesmall});
227  
228 #ImageMedium
229 0 $self->imagemedium($args{imagemedium});
230  
231 #ImageLarge
232 0 $self->imagelarge($args{imagelarge});
233  
234 #BuyPrint
235 0 $self->buyprint($args{buyprint});
236  
237 #Sections
238 0 $self->_set_sections($args{section});
239  
240 #Table of Contents
241 #toc
242  
243 #URL
244 0 $self->url($args{url});
245 }
246  
247 0 ▶sub _set_sections {
Subroutine _set_sections: not called CC=4 SCAR=30.0
248 0 my $self = shift;
249 0 my $section_ref = shift;
250  
251 0 my @sections;
252 0 ▶ if (ref($section_ref) eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $section_ref eq 'HASH') { } 0 0 0
elsif (ref $section_ref eq 'ARRAY') { } 0 0 0
253 0 push(@sections, Net::Safari::Response::Section->new(%$section_ref));
254 }
255 elsif (ref($section_ref) eq "ARRAY") {
256 0 foreach my $section (@$section_ref) {
257 0 push(@sections, Net::Safari::Response::Section->new(%$section));
258 }
259 }
260  
261 0 $self->{sections} = \@sections;
262 }
263  
264 0 ▶sub sections {
Subroutine sections: not called CC=1 SCAR=6.9
Pod documented
265 0 my $self = shift;
266  
267 0 return @{$self->{sections}};
268 }
269  
270 0 ▶sub _set_subjects {
Subroutine _set_subjects: not called CC=4 SCAR=30.0
271 0 my $self = shift;
272 0 my $subjects_ref = shift;
273  
274 0 my @subjects;
275  
276 0 ▶ if (ref($subjects_ref) eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $subjects_ref eq 'HASH') { } 0 0 0
elsif (ref $subjects_ref eq 'ARRAY') { } 0 0 0
277 0 push(@subjects, @{$subjects_ref->{subject}});
278 }
279 elsif (ref($subjects_ref) eq "ARRAY") {
280 0 foreach my $subject (@$subjects_ref) {
281 0 push(@subjects, @{$subjects_ref->{subject}});
282 }
283 }
284 0 $self->{subjects} = \@subjects;
285 }
286  
287 0 ▶sub subjects {
Subroutine subjects: not called CC=1 SCAR=6.9
Pod documented
288 0 my $self = shift;
289  
290 0 return @{$self->{subjects}};
291 }
292  
293 0 ▶sub _set_pub_timepiece {
Subroutine _set_pub_timepiece: not called CC=1 SCAR=6.9
294 0 my $self = shift;
295 0 my $date_string = $self->pubdate;
296  
297 #Default to the first of the month
298 0 $date_string =~ s/^(\w+)\s*(\d{4})$/$1 1, $2/;
299  
300 #Date should be like: April 30, 1978
301 0 my $time = str2time($date_string);
302  
303 0 $self->pub_timepiece( Time::Piece->strptime($time, "%s") );
304 }
305  
306 0 ▶sub _set_authors {
Subroutine _set_authors: not called CC=4 SCAR=30.0
307 0 my $self = shift;
308 0 my $authorgroup = shift;
309  
310 0 my @authors;
311  
312 # TODO - why is author an array? Because we specified it in XMLin. But that
313 # code was taken from the Safari example. Why?
314 0 ▶ if (ref($authorgroup) eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $authorgroup eq 'HASH') { } 0 0 0
elsif (ref $authorgroup eq 'ARRAY') { } 0 0 0
315 push (@authors,
316 0 Net::Safari::Response::Author->new(%{$authorgroup->{author}->[0]}));
317 }
318 elsif (ref($authorgroup) eq "ARRAY") {
319 0 foreach my $author (@$authorgroup) {
320 push (@authors,
321 0 Net::Safari::Response::Author->new(%{$author->{author}->[0]}));
322 }
323 }
324 0 $self->{authors} = \@authors;
325 }
326  
327 0 ▶sub authors {
Subroutine authors: not called CC=1 SCAR=6.9
Pod documented
328 0 my $self = shift;
329  
330 0 return @{$self->{authors}};
331 }
332  
333 =head1 BUGS
334  
335 None yet.
336  
337 =head1 SUPPORT
338  
339 If you find a bug in the code or find that the code doesn't match Safari API, please send me a line.
340  
341 If the Safari API is down or has bugs, please contact Safari directly:
342 affiliates@safaribooksonline.com
343  
344 =head1 ACKNOWLEDGMENTS
345  
346 Adapted from the design of Net::Amazon by Mike Schilli.
347  
348 Some documentation based on the source Safari documentation:
349 http://safari.oreilly.com/affiliates/?p=web_services
350  
351 =head1 AUTHOR
352  
353 Tony Stubblebine
354 tonys@oreilly.com
355  
356 =head1 COPYRIGHT
357  
358 Copyright 2004 by Tony Stubblebine (tonys@oreilly.com)
359  
360 This program is free software; you can redistribute
361 it and/or modify it under the same terms as Perl itself.
362  
363 The full text of the license can be found in the
364 LICENSE file included with this module.
365  
366  
367 =head1 SEE ALSO
368  
369 perl(1).
370  
371 =cut
372  
373  
374  
375 13 1; #this line is important and will help the module return a true value
376 __END__
↑ Summary Safari/Response/Section.pm »