File Coverage

blib/lib/EPFL/Sciper/List.pm
Criterion Covered Total %
statement 55 55 100.0
branch 2 2 100.0
condition n/a
subroutine 13 13 100.0
pod 6 6 100.0
total 76 76 100.0


line stmt bran cond sub pod time code
1             package EPFL::Sciper::List;
2              
3 3     3   117359 use 5.006;
  3         10  
4 3     3   13 use strict;
  3         5  
  3         56  
5 3     3   12 use warnings;
  3         11  
  3         64  
6              
7 3     3   1134 use JSON;
  3         22220  
  3         18  
8 3     3   1616 use Readonly;
  3         8253  
  3         133  
9 3     3   1188 use LWP::UserAgent;
  3         99075  
  3         141  
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 0.02
18              
19             =cut
20              
21             our $VERSION = '0.02';
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 program epfl-sciper-list.pl
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     3   25 use base 'Exporter';
  3         6  
  3         1398  
42             our @EXPORT_OK =
43             qw/p_createUserAgent p_getUrl p_buildUrl retrieveSciper toJson toTsv/;
44              
45             Readonly::Scalar my $TIMEOUT => 1200;
46              
47             Readonly::Scalar my $MAXREDIRECT => 10;
48              
49             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     2 1 8975 my @listPersons = ();
64 2         13 my @alphabet = ( 'a' .. 'z' );
65              
66 2         6 my $ua = p_createUserAgent();
67 2         6 foreach my $letter (@alphabet) {
68 52         355 my $response = p_getUrl( $ua, p_buildUrl($letter) );
69              
70 52 100       80165 if ( $response->is_success ) {
71 26         196 my $struct = from_json( $response->decoded_content );
72 26         3183 push @listPersons, @{ $struct->{result} };
  26         204  
73             }
74             }
75              
76 2         13 my %hash = ();
77 2         5 foreach my $per (@listPersons) {
78 65         106 $hash{ $per->{sciper} } = $per;
79             }
80              
81 2         7 @listPersons = ();
82 2         14 foreach my $sciper ( sort { $a <=> $b } keys %hash ) {
  293         293  
83 62         71 push @listPersons, $hash{$sciper};
84             }
85              
86 2         90 return @listPersons;
87             }
88              
89             =head2 toJson
90              
91             Return sciper list in JSON
92              
93             =cut
94              
95             sub toJson {
96 1     1 1 2833 my @list = @_;
97              
98 1         12 my $json = JSON->new->allow_nonref;
99 1         66 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     1 1 38001 my @list = @_;
110              
111 1         3 my $output = q{};
112 1         2 foreach my $per (@list) {
113             $output .=
114 62         124 $per->{sciper} . "\t" . $per->{firstname} . "\t" . $per->{name} . "\n";
115             }
116 1         10 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     3 1 7891 my $ua = LWP::UserAgent->new;
130              
131 3         4519 $ua->timeout($TIMEOUT);
132 3         50 $ua->agent('DevRunBot');
133 3         182 $ua->env_proxy;
134 3         19670 $ua->max_redirect($MAXREDIRECT);
135              
136 3         35 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     54 1 25741 my ( $ua, $url ) = @_;
149              
150 54         147 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     1 1 65 my $letter = shift;
161              
162 1         5 return $AUTOCOMPLETE_URL . $letter;
163             }
164              
165             =head1 AUTHOR
166              
167             William Belle, C<< >>
168              
169             =head1 BUGS AND LIMITATIONS
170              
171             Please report any bugs or feature requests here L.
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
188              
189             =item * CPAN Ratings
190              
191             L
192              
193             =item * Search CPAN
194              
195             L
196              
197             =back
198              
199             =head1 LICENSE AND COPYRIGHT
200              
201             Copyright ECOLE POLYTECHNIQUE FEDERALE DE LAUSANNE, Switzerland, VPSI, 2017.
202              
203             Licensed under the Apache License, Version 2.0 (the "License");
204             you may not use this file except in compliance with the License.
205             You may obtain a copy of the License at
206              
207             L
208              
209             Unless required by applicable law or agreed to in writing, software
210             distributed under the License is distributed on an "AS IS" BASIS,
211             WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
212             See the License for the specific language governing permissions and
213             limitations under the License.
214              
215             =cut
216              
217             1; # End of EPFL::Sciper::List