blib/lib/EPFL/Sciper/List.pm

filter: Statementstmt 100% 61 / 61 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 6 / 6 totaltotal 100% 82 / 82 SCAR 17.9
CC
6
Cov
100%
retrieveSciper
5.0
toTsv
2.0
BEGIN
1.0
CRAP 6.0
↑ Summary
1 package EPFL::Sciper::List;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 EPFL::Sciper::List - Retrieve a list of all public active sciper from EPFL.
14  
15 =head1 VERSION
16  
17 Version 1.01
18  
19 =cut
20  
21 3 our $VERSION = '1.01';
22  
23 =head1 SYNOPSIS
24  
25 Retrieve sciper from EPFL
26  
27 use EPFL::Sciper::List qw/retrieveSciper toJson toTsv/;
28  
29 my @listPersons = retrieveSciper();
30 print toJson(@listPersons);
31 print toTsv(@listPersons);
32  
33 Via the command line epfl-sciper-list
34  
35 =head1 DESCRIPTION
36  
37 A simple module to retrieve a list of all public active sciper from EPFL.
38  
39 =cut
40  
41 3 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 our @EXPORT_OK =
43 qw/p_createUserAgent p_getUrl p_buildUrl retrieveSciper toJson toTsv/;
44  
45 3 Readonly::Scalar my $TIMEOUT => 1200;
46  
47 3 Readonly::Scalar my $MAXREDIRECT => 10;
48  
49 3 Readonly::Scalar my $AUTOCOMPLETE_URL =>
50 'https://search.epfl.ch/json/autocompletename.action?maxRows=99999999&term=';
51  
52 =head1 SUBROUTINES/METHODS
53  
54 =head2 retrieveSciper( )
55  
56 Return a list of persons from EPFL with information like:
57  
58 (sciper => 999999, firstname => 'Taylor', name => 'Swift');
59  
60 =cut
61  
62 sub retrieveSciper {
63 2 ▶ my @listPersons = ();
Subroutine retrieveSciper: called CC=5 SCAR=16.1
Pod documented
64 2 my @alphabet = ( 'a' .. 'z' );
65  
66 2 my $ua = p_createUserAgent();
67 2 foreach my $letter (@alphabet) {
68 52 my $response = p_getUrl( $ua, p_buildUrl($letter) );
69  
70 52 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 26 26 52
71 26 my $struct = from_json( $response->decoded_content );
72 26 push @listPersons, @{ $struct->{result} };
73 }
74 }
75  
76 2 my %hash = ();
77 2 foreach my $per (@listPersons) {
78 65 $hash{ $per->{sciper} } = $per;
79 }
80  
81 2 @listPersons = ();
82 2 foreach my $sciper ( sort { $a <=> $b } keys %hash ) {
83 62 push @listPersons, $hash{$sciper};
84 }
85  
86 2 return @listPersons;
87 }
88  
89 =head2 toJson
90  
91 Return sciper list in JSON
92  
93 =cut
94  
95 sub toJson {
96 1 ▶ my @list = @_;
Subroutine toJson: called CC=1 SCAR=0.0
Pod documented
97  
98 1 my $json = JSON->new->allow_nonref;
99 1 return $json->pretty->encode( \@list );
100 }
101  
102 =head2 toTsv
103  
104 Return sciper list in TSV
105  
106 =cut
107  
108 sub toTsv {
109 1 ▶ my @list = @_;
Subroutine toTsv: called CC=2 SCAR=6.9
Pod documented
110  
111 1 my $output = q{};
112 1 foreach my $per (@list) {
113 $output .=
114 62 $per->{sciper} . "\t" . $per->{firstname} . "\t" . $per->{name} . "\n";
115 }
116 1 return $output;
117 }
118  
119 =head1 PRIVATE SUBROUTINES/METHODS
120  
121 =head2 p_createUserAgent
122  
123 Return a LWP::UserAgent.
124 LWP::UserAgent objects can be used to dispatch web requests.
125  
126 =cut
127  
128 sub p_createUserAgent {
129 3 ▶ my $ua = LWP::UserAgent->new;
Subroutine p_createUserAgent: called CC=1 SCAR=0.0
Pod documented
130  
131 3 $ua->timeout($TIMEOUT);
132 3 $ua->agent('DevRunBot');
133 3 $ua->env_proxy;
134 3 $ua->max_redirect($MAXREDIRECT);
135  
136 3 return $ua;
137 }
138  
139 =head2 p_getUrl
140  
141 Dispatch a GET request on the given $url
142 The return value is a response object. See HTTP::Response for a description
143 of the interface it provides.
144  
145 =cut
146  
147 sub p_getUrl {
148 54 ▶ my ( $ua, $url ) = @_;
Subroutine p_getUrl: called CC=1 SCAR=0.0
Pod documented
149  
150 54 return $ua->get($url);
151 }
152  
153 =head2 p_buildUrl
154  
155 Return the autocomplete url to retrieve sciper.
156  
157 =cut
158  
159 sub p_buildUrl {
160 1 ▶ my $letter = shift;
Subroutine p_buildUrl: called CC=1 SCAR=0.0
Pod documented
161  
162 1 return $AUTOCOMPLETE_URL . $letter;
163 }
164  
165 =head1 AUTHOR
166  
167 William Belle, C<< <william.belle at gmail.com> >>
168  
169 =head1 BUGS AND LIMITATIONS
170  
171 Please report any bugs or feature requests here L<https://github.com/epfl-devrun/epfl-sciper-list/issues>.
172 I will be notified, and then you'll automatically be notified of progress on
173 your bug as I make changes.
174  
175 =head1 SUPPORT
176  
177 You can find documentation for this module with the perldoc command.
178  
179 perldoc EPFL::Sciper::List
180  
181 You can also look for information at:
182  
183 =over 4
184  
185 =item * AnnoCPAN: Annotated CPAN documentation
186  
187 L<http://annocpan.org/dist/EPFL-Sciper-List>
188  
189 =item * CPAN Ratings
190  
191 L<http://cpanratings.perl.org/d/EPFL-Sciper-List>
192  
193 =item * Search CPAN
194  
195 L<http://search.cpan.org/dist/EPFL-Sciper-List/>
196  
197 =back
198  
199 =head1 LICENSE AND COPYRIGHT
200  
201 Copyright ECOLE POLYTECHNIQUE FEDERALE DE LAUSANNE,
202 Switzerland, VPSI, 2017-2018.
203  
204 Licensed under the Apache License, Version 2.0 (the "License");
205 you may not use this file except in compliance with the License.
206 You may obtain a copy of the License at
207  
208 L<http://www.apache.org/licenses/LICENSE-2.0>
209  
210 Unless required by applicable law or agreed to in writing, software
211 distributed under the License is distributed on an "AS IS" BASIS,
212 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
213 See the License for the specific language governing permissions and
214 limitations under the License.
215  
216 =cut
217  
218 3 1; # End of EPFL::Sciper::List
↑ Summary