Directory.pm

filter: Statementstmt 92.8% 182 / 196 Branchbran 55.1% 32 / 58 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 96.8% 31 / 32 Podpod 100% 4 / 4 totaltotal 84.7% 250 / 295 CC 35 SCAR 17.2
CC
35
Cov
84%
_table_row_to_entry
11.6
_advanced_search_parameter_id
10.3
search
9.5
CRAP 5.6
↑ Summary Directory/Entry/Affiliation.pm »
1 package WWW::USF::Directory;
2  
3 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 ###########################################################################
8 # METADATA
9 2 our $AUTHORITY = 'cpan:DOUGDUDE';
10 2 our $VERSION = '0.003001';
11  
12 ###########################################################################
13 # MOOSE
14 2 ▶use Moose 0.89;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use MooseX::StrictConstructor 0.08;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 ###########################################################################
18 # MOOSE TYPES
19 2 ▶use MooseX::Types::Moose qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
20 Bool
21 2 );
22 2 ▶use MooseX::Types::URI qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
23 Uri
24 2 );
25  
26 ###########################################################################
27 # MODULE IMPORTS
28 2 ▶use HTML::HTML5::Parser 0.101;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use List::MoreUtils 0.07;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use Net::SAJAX 0.102;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use Const::Fast 0.004 qw(const);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use WWW::USF::Directory::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use WWW::USF::Directory::Entry::Affiliation;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use WWW::USF::Directory::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 ###########################################################################
37 # PRIVATE CONSTANTS
38 2 const my $FACULTY_BIT => 1;
39 2 const my $STAFF_BIT => 2;
40 2 const my $STUDENTS_BIT => 4;
41  
42 ###########################################################################
43 # ALL IMPORTS BEFORE THIS WILL BE ERASED
44 2 ▶use namespace::clean 0.04 -except => [qw(meta)];
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 ###########################################################################
47 # ATTRIBUTES
48 has 'directory_url' => (
49 is => 'rw',
50 isa => Uri,
51  
52 documentation => q{This is the URL of the directory page were the requests are made},
53 coerce => 1,
54 default => 'http://directory.acomp.usf.edu/',
55 0 ▶ trigger => sub { shift->_sajax->url(shift); }, # Update the SAJAX URL
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 2 );
57 2 has 'include_faculty' => (
58 is => 'rw',
59 isa => Bool,
60  
61 documentation => q{This determines if faculty should be returned in the search results},
62 default => 1,
63 );
64 2 has 'include_staff' => (
65 is => 'rw',
66 isa => Bool,
67  
68 documentation => q{This determines if staff should be returned in the search results},
69 default => 1,
70 );
71 2 has 'include_students' => (
72 is => 'rw',
73 isa => Bool,
74  
75 documentation => q{This determines if students should be returned in the search results},
76 default => 0,
77 );
78  
79 ###########################################################################
80 # PRIVATE ATTRIBUTES
81 2 has '_advanced_search_parameters' => (
82 is => 'rw',
83 isa => 'HashRef',
84  
85 builder => '_build_advanced_search_parameters',
86 lazy => 1,
87 );
88 2 has '_sajax' => (
89 is => 'rw',
90 isa => 'Net::SAJAX',
91  
92 builder => '_build_sajax',
93 lazy => 1,
94 handles => {
95 user_agent => 'user_agent',
96 },
97 );
98  
99 ###########################################################################
100 # METHODS
101 1 ▶sub campus_list {
Subroutine campus_list: called CC=1 SCAR=0.0
Pod documented
102 1 my ($self) = @_;
103  
104 # Return the list of campuses
105 1 return $self->_advanced_search_parameter_list('campus');
106 }
107 1 ▶sub college_list {
Subroutine college_list: called CC=1 SCAR=0.0
Pod documented
108 1 my ($self) = @_;
109  
110 # Return the list of colleges
111 1 return $self->_advanced_search_parameter_list('college');
112 }
113 1 ▶sub department_list {
Subroutine department_list: called CC=1 SCAR=0.0
Pod documented
114 1 my ($self) = @_;
115  
116 # Return the list of departments
117 1 return $self->_advanced_search_parameter_list('department');
118 }
119 4 ▶sub search {
Subroutine search: called CC=8 SCAR=22.5
Pod documented
120 4 my ($self, %args) = @_;
121  
122 # Unwrap the name from the arguments
123 4 my $name = $args{name};
124  
125 4 ▶ if (!defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 4 4
126 # "name" is a required argument
127 0 WWW::USF::Directory::Exception->throw(
128 class => 'MethodArguments',
129 message => 'The argument "name" is required',
130 argument => 'name',
131 method => 'search',
132 );
133 }
134  
135 4 ▶ if (length $name == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $name == 0) 0 4 4
136 # "name" cannot be empty
137 0 WWW::USF::Directory::Exception->throw(
138 class => 'MethodArguments',
139 message => 'The argument "name" cannot be an empty string',
140 argument => 'name',
141 argument_value => $name,
142 method => 'search',
143 );
144 }
145  
146 # Get the inclusion from the arguments
147 my ($include_faculty, $include_staff, $include_students) =
148 4 @args{qw(include_faculty include_staff include_students)};
149  
150 # Determine the inclusion of faculty
151 4 ▶ if (!defined $include_faculty) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $include_faculty) 4 0 4
152 4 $include_faculty = $self->include_faculty
153 }
154  
155 # Determine the inclusion of staff
156 4 ▶ if (!defined $include_staff) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $include_staff) 4 0 4
157 4 $include_staff = $self->include_staff;
158 }
159  
160 # Determine the inclusion of students
161 4 ▶ if (!defined $include_students) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $include_students) 4 0 4
162 4 $include_students = $self->include_students;
163 }
164  
165 # Get the bit mask for the inclusion to send
166 4 my $inclusion_bitmask = _inclusion_bitmask(
167 include_faculty => $include_faculty,
168 include_staff => $include_staff,
169 include_students => $include_students,
170 );
171  
172 # Get the advanced search parameters
173 my ($campus, $college, $department) =
174 12 ▶ map { length($_) ? $_ : $args{$_} } # Restore to original if it didn't exist
Branch: 1 branch
BranchTrueFalseTotal
length $_ ? : 0 12 12
175 4 map { $self->_advanced_search_parameter_id($_ => $args{$_}) }
176 qw(campus college department);
177  
178 # Make a SAJAX call for the results HTML
179 4 my $search_results = $self->_sajax->call(
180 function => 'liveSearch',
181 arguments => [$name, $inclusion_bitmask, $campus, $college, $department],
182 );
183  
184 4 ▶ if (ref $search_results ne q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $search_results ne "") 0 4 4
185 # The response was not a plain string
186 0 WWW::USF::Directory::Exception->throw(
187 class => 'UnknownResponse',
188 message => 'The response from the server was not a plain string',
189 ajax_response => $search_results,
190 );
191 }
192  
193 # Return the results
194 4 return _parse_search_results_table($search_results);
195 }
196  
197 ###########################################################################
198 # PRIVATE METHODS
199 12 ▶sub _advanced_search_parameter_id {
Subroutine _advanced_search_parameter_id: called CC=4 SCAR=23.3
200 12 my ($self, $category, $name) = @_;
201  
202 12 ▶ if (!defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 12 0 12
203 # Undefined parameter name has a blank value
204 12 return q{};
205 }
206  
207 # Get the category list
208 0 my $list = $self->_advanced_search_parameters->{$category};
209  
210 0 ▶ if (!defined $list) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $list) 0 0 0
211 # The category doesn't exist
212 0 WWW::USF::Directory::Exception->throw(
213 class => 'MethodArguments',
214 message => 'The category provided for the advanced search parameter does not exist',
215 argument => 'category',
216 argument_value => $category,
217 method => '_advanced_search_parameter_id',
218 );
219 }
220  
221 0 ▶ if (!exists $list->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $list->{$name}) 0 0 0
222 # The name doesn't exist
223 0 WWW::USF::Directory::Exception->throw(
224 class => 'MethodArguments',
225 message => sprintf 'Unable to locate the given %s', $category,
226 argument => 'name',
227 argument_value => $name,
228 method => '_advanced_search_parameter_id',
229 );
230 }
231  
232 # Return the lookup
233 0 return $list->{$name};
234 }
235 3 ▶sub _advanced_search_parameter_list {
Subroutine _advanced_search_parameter_list: called CC=1 SCAR=0.0
236 3 my ($self, $category) = @_;
237  
238 # Get the sorted list of names
239 3 my @names = sort keys %{$self->_advanced_search_parameters->{$category}};
240  
241 # Return the list nof keys in the category
242 3 return @names;
243 }
244 1 ▶sub _get_advanced_categories {
Subroutine _get_advanced_categories: called CC=5 SCAR=16.6
245 1 my ($self) = @_;
246  
247 # Make a SAJAX call for the results HTML
248 1 my $advanced_menu_html = $self->_sajax->call(
249 function => 'advSearch',
250 arguments => [q{}, q{}, q{}],
251 );
252  
253 1 ▶ if (ref $advanced_menu_html ne q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $advanced_menu_html ne "") 0 1 1
254 # The response was not a plain string
255 0 WWW::USF::Directory::Exception->throw(
256 class => 'UnknownResponse',
257 message => 'The response from the server was not a plain string',
258 ajax_response => $advanced_menu_html,
259 );
260 }
261  
262 # Create a new HTML parser
263 1 my $parser = HTML::HTML5::Parser->new;
264  
265 # Parse the HTML into a document
266 1 my $document = $parser->parse_string($advanced_menu_html);
267  
268 # Select ID -> nice name map
269 1 my %nice_name_of = (
270 camp => 'campus',
271 colg => 'college',
272 dept => 'department',
273 );
274  
275 # This will hold the options
276 1 my %categories;
277  
278 # Cycle through all the select elements on the page
279 1 SELECT: foreach my $select ($document->getElementsByTagName('select')) {
280 3 ▶ if (!$select->hasAttribute('id')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($select->hasAttribute("id")) 0 3 3
281 # Go to the next select element, as this is not important
282 0 next SELECT;
283 }
284  
285 # Get the element's ID
286 3 my $id = $select->getAttribute('id');
287  
288 3 ▶ if (exists $nice_name_of{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $nice_name_of{$id}) 3 0 3
289 # Get the select as key value pair
290 3 my %menu = _select_node_to_hash($select);
291  
292 # Delete the "Any" entry
293 3 delete $menu{Any};
294  
295 # Save this to the categories under the nice name
296 3 $categories{$nice_name_of{$id}} = \%menu;
297 }
298 }
299  
300 # Return a hash reference to the categories
301 1 return \%categories;
302 }
303  
304 ###########################################################################
305 # PRIVATE BUILDERS
306 1 ▶sub _build_advanced_search_parameters {
Subroutine _build_advanced_search_parameters: called CC=1 SCAR=0.0
307 # This will get the advanced categories and save them in the attribute
308 1 return shift->_get_advanced_categories;
309 }
310 4 ▶sub _build_sajax {
Subroutine _build_sajax: called CC=1 SCAR=0.0
311 4 my ($self) = @_;
312  
313 # This will return a SAJAX object with default options
314 4 return Net::SAJAX->new(
315 url => $self->directory_url->clone,
316 );
317 }
318  
319 ###########################################################################
320 # PRIVATE FUNCTIONS
321 44 ▶sub _clean_node_text {
Subroutine _clean_node_text: called CC=2 SCAR=6.9
322 44 my ($node) = @_;
323  
324 # Make a copy of the node so modifications don't affect the original node.
325 44 $node = $node->cloneNode(1);
326  
327 # Find all the line breaks
328 44 foreach my $br ($node->getElementsByTagName('br')) {
329 # Replace the line breaks with a text node with a new line
330 5 $br->replaceNode($node->ownerDocument->createTextNode('{NEWLINE}'));
331 }
332  
333 # Get the text of the node
334 44 my $text = $node->textContent;
335  
336 # Transform all the horizontal space into ASCII spaces
337 44 $text =~ s{\s+}{ }gmsx;
338  
339 # Truncate leading and trailing horizontal space
340 44 $text =~ s{^\s+|\s+$}{}gmsx;
341  
342 # Change the new-lines back
343 44 $text =~ s{{NEWLINE}}{\n}gmsx; # Because perl < 5.10 cannot do \v and \h
344  
345 # Return the text
346 44 return $text;
347 }
348 8 ▶sub _clean_node_text_as_perl_name {
Subroutine _clean_node_text_as_perl_name: called CC=1 SCAR=0.0
349 8 my ($node) = @_;
350  
351 # Get the cleaned text as lowercase
352 8 my $text = lc _clean_node_text($node);
353  
354 # Change all space into underscores
355 8 $text =~ s{\p{IsSpace}+}{_}gmsx;
356  
357 # Return the text
358 8 return $text;
359 }
360 4 ▶sub _inclusion_bitmask {
Subroutine _inclusion_bitmask: called CC=4 SCAR=14.6
361 4 my (%args) = @_;
362  
363 # Create a default bitmask where nothing is selected
364 4 my $bitmask = 0;
365  
366 4 ▶ if ($args{include_faculty}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"include_faculty"}) 4 0 4
367 # OR in the faculty bit
368 4 $bitmask |= $FACULTY_BIT;
369 }
370  
371 4 ▶ if ($args{include_staff}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"include_staff"}) 4 0 4
372 # OR in the staff bit
373 4 $bitmask |= $STAFF_BIT;
374 }
375  
376 4 ▶ if ($args{include_students}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"include_students"}) 0 4 4
377 # OR in the students bit
378 0 $bitmask |= $STUDENTS_BIT;
379 }
380  
381 # Return the bitmask
382 4 return $bitmask;
383 }
384 4 ▶sub _parse_search_results_table {
Subroutine _parse_search_results_table: called CC=7 SCAR=19.5
385 4 my ($search_results_html) = @_;
386  
387 # Create a new HTML parser
388 4 my $parser = HTML::HTML5::Parser->new;
389  
390 # Parse the HTML into a document
391 4 my $document = $parser->parse_string($search_results_html);
392  
393 # Get the first heading level 3 element
394 4 my $heading = $document->getElementsByTagName('h3')->get_node(1);
395  
396 4 ▶ if (defined $heading) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $heading) 3 1 4
397 # Determine if the response thinks there are too many results
398 3 ▶ if ($heading->textContent eq 'Too many results') {
Branch: 2 branches
BranchTrueFalseTotal
if ($heading->textContent eq 'Too many results') { } 1 2 3
elsif ($heading->textContent eq '0 matches found') { } 1 1 2
399 # Get the first paragraph element in the content
400 1 my $paragraph = $document->getElementsByTagName('p')->get_node(1);
401  
402 1 ▶ if (defined $paragraph && $paragraph->textContent =~ m{(\d+) \s+ matches}msx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $paragraph and $paragraph->textContent =~ /(\d+) \s+ matches/msx) 1 0 1
Condition33%1/3
defined $paragraph and $paragraph->textContent =~ /(\d+) \s+ matches/msx
!ll&&!rl&&r
001
MC/DC0%0/2
defined $paragraph and $paragraph->textContent =~ /(\d+) \s+ matches/msx
defined $paragraph $paragraph->textContent =~ /(\d+) \s+ matches/msx
Truth table33%1/3
A and B
A= defined $paragraphB= $paragraph->textContent =~ /(\d+) \s+ matches/msx
ABresult
0X0
100
111
403 # Store the max results from the regular expression
404 1 my $max_results = $1;
405  
406 # Throw a TooManyResults exception
407 1 WWW::USF::Directory::Exception->throw(
408 class => 'TooManyResults',
409 message => 'The search returned too many results',
410 max_results => $max_results,
411 );
412 }
413 }
414 # Determine if the response had no results
415 elsif ($heading->textContent eq '0 matches found') {
416 # Return nothing
417 1 return;
418 }
419 }
420  
421 # Get the first table in the response
422 2 my $search_results_table = $document->getElementsByTagName('table')->shift;
423  
424 2 ▶ if (!defined $search_results_table) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $search_results_table) 1 1 2
425 # Don't know how to handle the response, so throw exception
426 1 WWW::USF::Directory::Exception->throw(
427 class => 'UnknownResponse',
428 message => 'The response from the server did not contain a results table',
429 ajax_response => $search_results_html,
430 );
431 }
432  
433 # Get all the table rows
434 1 my $table_rows = $search_results_table->getChildrenByTagName('tbody')->shift
435 ->getChildrenByTagName('tr');
436  
437 # Get an array of table headers
438 1 my @table_header = map { _clean_node_text_as_perl_name($_) }
439 $table_rows->shift->getChildrenByTagName('td');
440  
441 # Get the table's content as array of entries
442 1 my @results = map { _table_row_to_entry($_, \@table_header) }
443 $table_rows->get_nodelist;
444  
445 1 return @results;
446 }
447 3 ▶sub _select_node_to_hash {
Subroutine _select_node_to_hash: called CC=1 SCAR=0.0
448 3 my ($select_node) = @_;
449  
450 12 return map { ($_->getAttribute('value'), _clean_node_text($_)) }
451 3 grep { $_->hasAttribute('value') }
452 $select_node->getChildrenByTagName('option');
453 }
454 3 ▶sub _table_row_to_entry {
Subroutine _table_row_to_entry: called CC=11 SCAR=24.5
455 3 my ($tr_node, $table_header) = @_;
456  
457 # Get the row's text content as an array
458 3 my @row_content = map { _clean_node_text($_) }
459 $tr_node->getChildrenByTagName('td');
460  
461 # Make a hash with the headers as the keys
462 3 my %row = List::MoreUtils::mesh @{$table_header}, @row_content;
463  
464 # Delete all keys with blank content
465 3 delete @row{grep { $row{$_} =~ m{\A \p{IsSpace}* \z}msx } keys %row};
466  
467 3 ▶ if (exists $row{given_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $row{"given_name"}) 3 0 3
468 # Split on vertical whitespace
469 3 my @given_names = split m{[\r\n]+}msx, $row{given_name};
470  
471 # The first two given names are as follows
472 3 my ($first_name, $middle_name) = @given_names;
473  
474 3 ▶ if (defined $first_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first_name) 3 0 3
475 # Set the first name
476 3 $row{first_name} = $first_name;
477 }
478  
479 3 ▶ if (defined $middle_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $middle_name) 3 0 3
480 # Set the middle name
481 3 $row{middle_name} = $middle_name;
482 }
483 }
484  
485 3 ▶ if (exists $row{affiliation}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $row{"affiliation"}) 3 0 3
486 # There could be zero or more affiliations seperated by vertical space
487 3 my @affiliations = split m{\s*[\r\n]+\s*}msx, delete $row{affiliation};
488  
489 # Change the affiliation to objects
490 3 foreach my $affiliation (@affiliations) {
491 3 $affiliation = WWW::USF::Directory::Entry::Affiliation->new($affiliation);
492 }
493  
494 # Store the affiliations
495 3 $row{affiliations} = \@affiliations;
496 }
497  
498 # Remove vertical whitespace from all non-reference values
499 3 foreach my $value (values %row) {
500 26 ▶ if (ref $value eq q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "") 23 3 26
501 # A string, so remove vertical whitespace
502 23 $value =~ s{\s*[\r\n]+\s*}{ }gmsx;
503 }
504 }
505  
506 3 ▶ if (exists $row{campus_phone}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $row{"campus_phone"}) 3 0 3
507 # Remove all non-letters and non-numbers
508 3 $row{campus_phone} =~ s{[^a-z0-9]+}{}gimsx;
509  
510 # Remove the U.S. country code if present
511 3 $row{campus_phone} =~ s{\A \+ 1}{}msx;
512  
513 # Reformat the phone number
514 3 $row{campus_phone} =~ s{\A (\d{3}) (\d{3}) (\d{4}) \z}{+1 $1 $2 $3}msx;
515 }
516  
517 3 ▶ if (exists $row{email}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $row{"email"}) 3 0 3
518 # USF is not too bright at preventing unwanted text from coming through
519 3 ▶ if (List::MoreUtils::any { $_ eq $row{email} } qw[null undefined]) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (&List::MoreUtils::any(sub { $_ eq $row{"email"}; } , "null", "undefined")) 0 3 3
520 # This is an invalid address
521 0 delete $row{email};
522 }
523 }
524  
525 # Make a new entry for the result
526 3 my $entry = WWW::USF::Directory::Entry->new(%row);
527  
528 # Return the entry
529 3 return $entry;
530 }
531  
532 ###########################################################################
533 # MAKE MOOSE OBJECT IMMUTABLE
534 2 __PACKAGE__->meta->make_immutable;
535  
536 2 1;
537  
538 __END__
↑ Summary Directory/Entry/Affiliation.pm »