blib/lib/WWW/eiNetwork.pm

filter: Statementstmt 73.9% 68 / 92 Branchbran 46.6% 14 / 30 Conditioncond 52.6% 10 / 19 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 84.6% 11 / 13 Podpod 0.0% 0 / 3 totaltotal 62.7% 106 / 169 CC 27 SCAR 23.4
CC
27
Cov
65%
_get_html
30.0
_get_content
14.2
_login
6.0
CRAP 10.3
↑ Summary
1 package WWW::eiNetwork;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTML::TableContentParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.02';
10  
11 sub new
12 1 ▶{
Subroutine new: called CC=5 SCAR=16.8
Pod undocumented
13 1 my ($class, %args) = @_;
14  
15 1 ▶ croak "You must specify your library card number" unless $args{card_number};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"card_number"} 0 1 1
16 1 ▶ croak "You must specify your PIN number" unless $args{pin_number};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"pin_number"} 0 1 1
17  
18 # Strip trailing slash from URL prefix
19 1 ▶ my $prefix = $args{url_prefix} || '';
Condition50%1/2
$args{'url_prefix'} || ''
l!l
01
MC/DC0%0/1
$args{'url_prefix'} || ''
$args{'url_prefix'}
Truth table50%1/2
A
A= $args{'url_prefix'}
Aresult
11
00
20 1 $prefix =~ s/\/$//;
21  
22 my $self =
23 {
24 card_number => $args{card_number},
25 pin_number => $args{pin_number},
26 1 ▶ url_prefix => $prefix || 'https://iiisy1.einetwork.net/patroninfo~S1',
Condition50%1/2
$prefix || 'https://iiisy1.einetwork.net/patroninfo~S1'
l!l
01
MC/DC0%0/1
$prefix || 'https://iiisy1.einetwork.net/patroninfo~S1'
$prefix
Truth table50%1/2
A
A= $prefix
Aresult
11
00
27 };
28  
29 1 bless $self, $class;
30 1 return $self;
31 };
32  
33 sub _login
34 0 ▶{
Subroutine _login: not called CC=2 SCAR=17.9
35 0 my ($self, $name, $card) = @_;
36  
37 0 my $mech = WWW::Mechanize->new;
38 0 $mech->get($self->{url_prefix});
39 0 $mech->form_with_fields(qw(code pin));
40 0 $mech->field('code', $self->{card_number});
41 0 $mech->field('pin', $self->{pin_number});
42 0 $mech->click('submit');
43  
44 0 my $uri = $mech->uri;
45 0 ▶ if ($uri =~ /patroninfo~S1\/(\d+)\//)
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ m[patroninfo~S1/(\d+)/]) { } 0 0 0
46 {
47 0 $self->{patron_id} = $1;
48 0 $self->{mech} = $mech;
49 0 return $self->{mech};
50 }
51 else
52 {
53 0 croak "Couldn't log in to eiNetwork!";
54 0 return;
55 }
56 }
57  
58 sub holds
59 1 ▶{
Subroutine holds: called CC=2 SCAR=7.0
Pod undocumented
60 1 my ($self, %args) = @_;
61  
62 1 my @classes = ('Title', 'Status', 'Pickup', 'Cancel');
63 my @items = $self->_get_content(
64 page => 'holds',
65 classes => \@classes,
66 html => $args{html},
67 1 );
68  
69 1 ▶ return wantarray ? @items : \@items;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
70 }
71  
72 sub items
73 1 ▶{
Subroutine items: called CC=2 SCAR=7.0
Pod undocumented
74 1 my ($self, %args) = @_;
75  
76 1 my @classes = ('Title', 'Barcode', 'Status', 'CallNo');
77 my @items = $self->_get_content(
78 page => 'items',
79 classes => \@classes,
80 html => $args{html},
81 1 );
82  
83 1 ▶ return wantarray ? @items : \@items;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
84 }
85  
86 sub _get_content
87 2 ▶{
Subroutine _get_content: called CC=13 SCAR=26.5
88 2 my ($self, %args) = @_;
89  
90 2 my $page = $args{page};
91 2 my $classes = $args{classes};
92 2 my $html = $args{html};
93  
94 # Hack to facilitate unit tests
95 2 ▶ $html ||= $self->_get_html($page);
Condition33%1/3
$html ||= $self->_get_html($page)
l!l&&r!l&&!r
200
MC/DC0%0/2
$html ||= $self->_get_html($page)
$html $self->_get_html($page)
Truth table33%1/3
A ||= B
A= $htmlB= $self->_get_html($page)
ABresult
1X1
011
000
96  
97 2 my $tables = $self->_get_tables($html);
98  
99 2 my @items;
100 2 for my $table (@$tables)
101 {
102 2 ▶ next unless ($table->{class} and $table->{class} eq 'patFunc');
Branch: 1 branch
BranchTrueFalseTotal
unless $table->{"class"} and $table->{"class"} eq "patFunc" 0 2 2
Condition33%1/3
$table->{"class"} and $table->{"class"} eq "patFunc"
!ll&&!rl&&r
002
MC/DC0%0/2
$table->{"class"} and $table->{"class"} eq "patFunc"
$table->{"class"} $table->{"class"} eq "patFunc"
Truth table33%1/3
A and B
A= $table->{"class"}B= $table->{"class"} eq "patFunc"
ABresult
0X0
100
111
103 2 for my $row (@{$table->{rows}})
104 {
105 8 ▶ next unless ($row->{class} and $row->{class} eq 'patFuncEntry');
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{"class"} and $row->{"class"} eq "patFuncEntry" 4 4 8
Condition66%2/3
$row->{"class"} and $row->{"class"} eq "patFuncEntry"
!ll&&!rl&&r
044
MC/DC50%1/2
$row->{"class"} and $row->{"class"} eq "patFuncEntry"
$row->{"class"} $row->{"class"} eq "patFuncEntry"
Truth table66%2/3
A and B
A= $row->{"class"}B= $row->{"class"} eq "patFuncEntry"
ABresult
0X0
100
111
106 4 my %record;
107 4 for my $cell (@{$row->{cells}})
108 {
109 20 for my $class (@$classes)
110 {
111 80 ▶ if ($cell->{class} and $cell->{class} eq "patFunc$class")
Branch: 1 branch
BranchTrueFalseTotal
if ($cell->{"class"} and $cell->{"class"} eq "patFunc$class") 16 64 80
Condition66%2/3
$cell->{"class"} and $cell->{"class"} eq "patFunc$class"
!ll&&!rl&&r
06416
MC/DC50%1/2
$cell->{"class"} and $cell->{"class"} eq "patFunc$class"
$cell->{"class"} $cell->{"class"} eq "patFunc$class"
Truth table66%2/3
A and B
A= $cell->{"class"}B= $cell->{"class"} eq "patFunc$class"
ABresult
0X0
100
111
112 {
113 16 my $data = $self->_cleanup_data($cell->{data});
114 16 $record{lc($class)} = $data;
115 16 next;
116 }
117 }
118 }
119
120 4 push @items, \%record;
121 }
122 }
123
124 2 ▶ return wantarray ? @items : \@items;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
125 }
126  
127 sub _get_html
128 0 ▶{
Subroutine _get_html: not called CC=5 SCAR=34.0
129 0 my ($self, $page) = @_;
130  
131 0 ▶ $self->_login or croak "Couldn't log in!";
Branch: 1 branch
BranchTrueFalseTotal
$self->_login or croak("Couldn't log in!") 0 0 0
132  
133 0 my $mech = $self->{mech};
134 0 ▶ return unless $mech;
Branch: 1 branch
BranchTrueFalseTotal
unless $mech 0 0 0
135  
136 0 my $patron_id = $self->{patron_id};
137 0 ▶ return unless $patron_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $patron_id 0 0 0
138  
139 0 my $prefix = $self->{url_prefix};
140 0 ▶ return unless $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix 0 0 0
141  
142 0 $mech->get("$prefix/$patron_id/$page");
143 0 return $mech->content;
144 }
145  
146 sub _get_tables
147 2 ▶{
Subroutine _get_tables: called CC=1 SCAR=0.0
148 2 my ($self, $html) = @_;
149  
150 2 my $tp = HTML::TableContentParser->new();
151 2 my $tables = $tp->parse($html);
152 2 return $tables;
153 }
154  
155 sub _cleanup_data
156 16 ▶{
Subroutine _cleanup_data: called CC=4 SCAR=13.9
157 16 my ($self, $data) = @_;
158
159 # If the result is a link, strip the link tags and use the title.
160 # Not the greatest regex, but works for these simple cases.
161 16 ▶ if ($data =~ /"\>\s*(.*)\s*<\/a>/m)
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ m["\>\s*(.*)\s*</a>]m) 4 12 16
162 {
163 4 $data = $1;
164 }
165
166 # If the data is a select and there's something selected, use the
167 # title of the selected option.
168 16 ▶ if ($data =~ /\<select/ and $data =~ /selected/)
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /\<select/ and $data =~ /selected/) 2 14 16
Condition66%2/3
$data =~ /\<select/ and $data =~ /selected/
!ll&&!rl&&r
1402
MC/DC50%1/2
$data =~ /\<select/ and $data =~ /selected/
$data =~ /\<select/ $data =~ /selected/
Truth table66%2/3
A and B
A= $data =~ /\<select/B= $data =~ /selected/
ABresult
0X0
100
111
169 {
170 2 $data =~ /selected="selected">\s*(.*)\s*<\/option>/m;
171 2 $data = $1;
172 }
173  
174 # Remove leading and trailing whitespace.
175 16 $data =~ s/^\s*//;
176 16 $data =~ s/\s*$//;
177  
178 16 return $data;
179 }
180  
181 1 1;
182  
183  
184 =head1 NAME
185  
186 WWW::eiNetwork - Perl interface to Allegheny County, PA libraries
187  
188 =head1 SYNOPSIS
189  
190 use WWW::eiNetwork;
191  
192 my $ein = WWW::eiNetwork->new(
193 card_number => '23456000000000',
194 pin_number => '1234',
195 url_prefix => 'https://iiisy1.einetwork.net/patroninfo~S1', #optional
196 );
197  
198 my @holds = $ein->holds;
199 my @items = $ein->items;
200  
201 for my $hold (@holds)
202 {
203 print qq(
204 Title: $hold->{title}
205 Status: $hold->{status}
206 Pickup at: $hold->{pickup}
207 Cancel if not picked up by: $hold->{cancel}\n\n
208 );
209 }
210
211 for my $item (@items)
212 {
213 print qq(
214 Title: $item->{title}
215 Barcode: $item->{barcode}
216 Status: $item->{status}
217 CallNo: $item->{callno}\n\n
218 );
219 }
220
221 =head1 DESCRIPTION
222  
223 This module provides an object-oriented Perl interface to eiNetwork libraries in Allegheny County, Pennsylvania.
224  
225 =head1 DEPENDENCIES
226  
227 WWW::Mechanize, HTML::TableContentParser, Crypt::SSLeay or IO::Socket::SSL
228  
229 =head1 BUGS
230  
231 The eiNetwork doesn't provide a public API - this module uses screen scraping to pull data directly from the HTML on their site. While I made an effort to code this module in such a way that small changes to the site layout and table arrangement won't break the module, any number of changes to the EIN's site could break this module.
232  
233 =head1 DISCLAIMER
234  
235 The author of this module is not affiliated in any way with the EINetwork or any Allegheny County library.
236  
237 =head1 ACKNOWLEDGMENTS
238  
239 Thanks to:
240  
241 Adam Foxson (L<http://search.cpan.org/~Fhoxh>) for the great newbie's tutorial to contributing to CPAN at the Pittsburgh Perl Workshop (L<http://pghpw.org/ppw2007/>).
242  
243 Bob O'Neill (L<http://search.cpan.org/~BOBO>) for sharing his CPAN know-how.
244  
245 =head1 COPYRIGHT AND LICENSE
246  
247 Copyright (C) 2008 Michael Aquilina. All rights reserved.
248  
249 This code is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
250  
251 =head1 AUTHOR
252  
253 Michael Aquilina, aquilina@cpan.org
254  
255 =cut
↑ Summary