blib/script/TestNOSOpen.pl untested

filter: Statementstmt 0.0% 0 / 201 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 228 CC 5 SCAR 23.3
CC
5
Cov
0%
main
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 10.2
1 #!/usr/bin/env perl -w # -*- cperl; cperl-indent-level: 4 -*-
2 use strict;
3 use warnings;
4  
5 use utf8;
6 use 5.014000;
7  
8 our $VERSION = 'v1.0.3';
9  
10 use CGI qw/:all/;
11 use HTTP::Server::Brick;
12  
13 use lib q{../lib};
14 use Getopt::Long;
15 use Pod::Usage;
16 use Pod::Usage::CommandLine;
17 use WWW::NOS::Open;
18  
19 use Readonly;
20 Readonly::Scalar my $CONNECTOR_PORT => 8081;
21 Readonly::Scalar my $API_KEY => $ENV{'NOSOPEN_API_KEY'} || q{TEST};
22 Readonly::Scalar my $TITLE => q{NOS Open test page};
23 Readonly::Scalar my $EMPTY => q{};
24 Readonly::Scalar my $ROOT => q{/};
25 Readonly::Scalar my $SLASH => q{/};
26  
27 Readonly::Array my @GETOPT_CONFIG =>
28 qw(no_ignore_case bundling auto_version auto_help);
29 Readonly::Array my @GETOPTIONS => ( q{port|p=s}, q{verbose|v+}, );
30 Readonly::Hash my %OPTS_DEFAULT => ( 'port' => $CONNECTOR_PORT, );
31  
32 Getopt::Long::Configure(@GETOPT_CONFIG);
33 my %opts = %OPTS_DEFAULT;
34 Getopt::Long::GetOptions( \%opts, @GETOPTIONS ) or Pod::Usage::pod2usage(2);
35  
36 my $server = HTTP::Server::Brick->new( 'port' => $opts{'port'} );
37 my $nos = WWW::NOS::Open->new($API_KEY);
38 my $nos_version =
39 $nos->get_version->get_version . $SLASH . $nos->get_version->get_build;
40  
41 $server->mount(
42 $ROOT => {
43 'handler' => \&main,
44 'wildcard' => 1,
45 },
46 );
47  
48 my $css = <<'EOC';
49 body {
50 color: #000;
51 background-color: #fff;
52 font-family: Arial, sans-serif;
53 font-size: 10px;
54 width: 640px;
55 }
56  
57 h1 {
58 font-size: 16px;
59 }
60  
61 h2 {
62 font-size: 14px;
63 }
64  
65 h3 {
66 font-size: 12px;
67 }
68 EOC
69  
70 sub main {
71 my ( $req, $res ) = @_;
72 my $uri = $req->uri;
73 my $q = CGI->new($uri);
74  
75 $res->add_content_utf8(
76 $q->start_html(
77 '-title' => $q->escapeHTML($TITLE),
78 '-style' => { '-code' => $css },
79 '-encoding' => q{utf-8},
80 ),
81 );
82 $res->add_content_utf8( $q->p( $q->escapeHTML($nos_version) ) );
83  
84 $res->add_content_utf8( $q->h1( $q->escapeHTML(q{Latest news articles}) ) );
85 my @latest_articles = $nos->get_latest_articles(q{nieuws});
86 while ( my $article = shift @latest_articles ) {
87 $res->add_content_utf8(
88 $q->h2(
89 { '-id' => q{id} . $q->escapeHTML( $article->get_id ) },
90 $q->escapeHTML( $article->get_title ),
91 ),
92 );
93 $res->add_content_utf8(
94 $q->p( $q->escapeHTML( $article->get_description ) ) );
95 $res->add_content_utf8(
96 $q->p( $q->escapeHTML( $article->get_published ) ) );
97 $res->add_content_utf8(
98 $q->p( $q->escapeHTML( $article->get_last_update ) ) );
99 $res->add_content_utf8(
100 $q->a(
101 { '-href' => $q->escapeHTML( $article->get_link ) },
102 $q->img(
103 { '-src' => $q->escapeHTML( $article->get_thumbnail_xs ) },
104 ),
105 ),
106 );
107 $res->add_content_utf8(
108 $q->a(
109 { '-href' => $q->escapeHTML( $article->get_link ) },
110 $q->img(
111 { '-src' => $q->escapeHTML( $article->get_thumbnail_s ) },
112 ),
113 ),
114 );
115 $res->add_content_utf8(
116 $q->a(
117 { '-href' => $q->escapeHTML( $article->get_link ) },
118 $q->img(
119 { '-src' => $q->escapeHTML( $article->get_thumbnail_m ) },
120 ),
121 ),
122 );
123 if ( my @keywords = @{ $article->get_keywords } ) {
124 $res->add_content_utf8(
125 $q->ul( map { $q->li( $q->escapeHTML($_) ) } @keywords ) );
126 }
127 }
128  
129 $res->add_content_utf8( $q->h1( $q->escapeHTML(q{Latest news videos}) ) );
130 my @latest_videos = $nos->get_latest_videos( $q->escapeHTML(q{nieuws}) );
131 while ( my $video = shift @latest_videos ) {
132 $res->add_content_utf8(
133 $q->h2(
134 { '-id' => q{id} . $q->escapeHTML( $video->get_id ) },
135 $q->escapeHTML( $video->get_title ),
136 ),
137 );
138 $res->add_content_utf8(
139 $q->p( $q->escapeHTML( $video->get_description ) ) );
140 $res->add_content_utf8( $q->p( $video->get_embedcode ) );
141 $res->add_content_utf8(
142 $q->p( $q->escapeHTML( $video->get_published ) ) );
143 $res->add_content_utf8(
144 $q->p( $q->escapeHTML( $video->get_last_update ) ) );
145 $res->add_content_utf8(
146 $q->a(
147 { '-href' => $q->escapeHTML( $video->get_link ) },
148 $q->img(
149 { '-src' => $q->escapeHTML( $video->get_thumbnail_xs ) },
150 ),
151 ),
152 );
153 $res->add_content_utf8(
154 $q->a(
155 { '-href' => $q->escapeHTML( $video->get_link ) },
156 $q->img(
157 { '-src' => $q->escapeHTML( $video->get_thumbnail_s ) },
158 ),
159 ),
160 );
161 $res->add_content_utf8(
162 $q->a(
163 { '-href' => $q->escapeHTML( $video->get_link ) },
164 $q->img(
165 { '-src' => $q->escapeHTML( $video->get_thumbnail_m ) },
166 ),
167 ),
168 );
169  
170 if ( my @keywords = @{ $video->get_keywords } ) {
171 $res->add_content_utf8(
172 $q->ul( map { $q->li( $q->escapeHTML($_) ) } @keywords ) );
173 }
174 }
175  
176 $res->add_content_utf8(
177 $q->h1( $q->escapeHTML(q{Latest news audio fragments}) ) );
178 my @latest_audio_fragments =
179 $nos->get_latest_audio_fragments( $q->escapeHTML(q{nieuws}) );
180 while ( my $audio_fragment = shift @latest_audio_fragments ) {
181 $res->add_content_utf8(
182 $q->h2(
183 { '-id' => q{id} . $q->escapeHTML( $audio_fragment->get_id ) },
184 $q->escapeHTML( $audio_fragment->get_title ),
185 ),
186 );
187 $res->add_content_utf8(
188 $q->p( $q->escapeHTML( $audio_fragment->get_description ) ) );
189 $res->add_content_utf8( $q->p( $audio_fragment->get_embedcode ) );
190 $res->add_content_utf8(
191 $q->p( $q->escapeHTML( $audio_fragment->get_published ) ) );
192 $res->add_content_utf8(
193 $q->p( $q->escapeHTML( $audio_fragment->get_last_update ) ) );
194 $res->add_content_utf8(
195 $q->a(
196 { '-href' => $q->escapeHTML( $audio_fragment->get_link ) },
197 $audio_fragment->get_thumbnail_s
198 ? $q->img(
199 {
200 '-src' =>
201 $q->escapeHTML( $audio_fragment->get_thumbnail_s ),
202 },
203 )
204 : $q->escapeHTML( $audio_fragment->get_title ),
205 ),
206 );
207 if ( my @keywords = @{ $audio_fragment->get_keywords } ) {
208 $res->add_content_utf8(
209 $q->ul( map { $q->li( $q->escapeHTML($_) ) } @keywords ) );
210 }
211 }
212  
213 my $result = $nos->search(q{cricket});
214 my @results = @{ $result->get_documents };
215 my @related = @{ $result->get_related };
216 while ( my $result_item = shift @results ) {
217 $res->add_content_utf8(
218 $q->h3( $q->escapeHTML( $result_item->get_title ) ) );
219 $res->add_content_utf8(
220 $q->p( $q->escapeHTML( $result_item->get_description ) ) );
221 }
222 while ( my $relation = shift @related ) {
223 $res->add_content_utf8( $q->span( $q->escapeHTML($relation) ) );
224 }
225  
226 my @tv_days = $nos->get_tv_broadcasts( q{2011-01-01}, q{2011-01-03} );
227 while ( my $tv_day = shift @tv_days ) {
228 $res->add_content_utf8( $q->h1( $q->escapeHTML( $tv_day->get_type ) ) );
229 $res->add_content_utf8( $q->h2( $q->escapeHTML( $tv_day->get_date ) ) );
230 my @broadcasts = @{ $tv_day->get_broadcasts };
231 while ( my $broadcast = shift @broadcasts ) {
232 $res->add_content_utf8(
233 $q->h3( $q->escapeHTML( $broadcast->get_title ) ) );
234 $res->add_content_utf8(
235 $q->p( $q->escapeHTML( $broadcast->get_starttime ) ) );
236 $res->add_content_utf8(
237 $q->p( $q->escapeHTML( $broadcast->get_endtime ) ) );
238 $res->add_content_utf8(
239 $q->img(
240 {
241 '-src' =>
242 $q->escapeHTML( $broadcast->get_channel_icon ),
243 '-alt' =>
244 $q->escapeHTML( $broadcast->get_channel_name ),
245 },
246 ),
247 );
248 $res->add_content_utf8(
249 $q->p( $q->escapeHTML( $broadcast->get_channel_code ) ) );
250 $res->add_content_utf8(
251 $q->p( $q->escapeHTML( $broadcast->get_channel_name ) ) );
252 $res->add_content_utf8(
253 $q->h3( $q->escapeHTML( $broadcast->get_title ) ) );
254 $res->add_content_utf8(
255 $q->p( $q->escapeHTML( $broadcast->get_id ) ) );
256 $res->add_content_utf8(
257 $q->p( $q->escapeHTML( $broadcast->get_genre ) ) );
258 $res->add_content_utf8(
259 $q->p( $q->escapeHTML( $broadcast->get_description ) ) );
260 }
261 }
262 my @radio_days = $nos->get_radio_broadcasts( q{2011-01-01}, q{2011-01-03} );
263 while ( my $radio_day = shift @radio_days ) {
264 $res->add_content_utf8(
265 $q->h1( $q->escapeHTML( $radio_day->get_type ) ) );
266 $res->add_content_utf8(
267 $q->h2( $q->escapeHTML( $radio_day->get_date ) ) );
268 my @broadcasts = @{ $radio_day->get_broadcasts };
269 while ( my $broadcast = shift @broadcasts ) {
270 $res->add_content_utf8(
271 $q->h3( $q->escapeHTML( $broadcast->get_title ) ) );
272 $res->add_content_utf8(
273 $q->p( $q->escapeHTML( $broadcast->get_starttime ) ) );
274 $res->add_content_utf8(
275 $q->p( $q->escapeHTML( $broadcast->get_endtime ) ) );
276 $res->add_content_utf8(
277 $q->img(
278 {
279 '-src' =>
280 $q->escapeHTML( $broadcast->get_channel_icon ),
281 '-alt' =>
282 $q->escapeHTML( $broadcast->get_channel_name ),
283 },
284 ),
285 );
286 $res->add_content_utf8(
287 $q->p( $q->escapeHTML( $broadcast->get_channel_code ) ) );
288 $res->add_content_utf8(
289 $q->p( $q->escapeHTML( $broadcast->get_channel_name ) ) );
290 $res->add_content_utf8(
291 $q->h3( $q->escapeHTML( $broadcast->get_title ) ) );
292 $res->add_content_utf8(
293 $q->p( $q->escapeHTML( $broadcast->get_id ) ) );
294 $res->add_content_utf8(
295 $q->p( $q->escapeHTML( $broadcast->get_genre ) ) );
296 $res->add_content_utf8(
297 $q->p( $q->escapeHTML( $broadcast->get_description ) ) );
298 }
299 }
300  
301 $res->add_content_utf8( $q->end_html );
302  
303 $res->header( 'Content-Type', 'text/html; charset=utf-8' );
304 return 1;
305 }
306  
307 $server->start;
308  
309 __END__
310  
311 =encoding utf8
312  
313 =for stopwords DateTime URI TestNOSOpen.pl manpage apikey Readonly Ipenburg MERCHANTABILITY
314  
315 =head1 NAME
316  
317 TestNOSOpen.pl - an HTTP server that shows content through the NOS Open API
318  
319 =head1 USAGE
320  
321 B<./TestNOSOpen.pl> [B<--apikey=APIKEY>] [B<--port=PORT>]
322  
323 =head1 DESCRIPTION
324  
325 This script starts an HTTP service which presents an HTML page that interacts
326 with the Open NOS service giving examples of the available API calls. For more
327 information see: L<http://open.nos.nl/|http://open.nos.nl/>
328  
329 =head1 REQUIRED ARGUMENTS
330  
331 None.
332  
333 =head1 OPTIONS
334  
335 =over 4
336  
337 =item B< -?, -h, --help>
338  
339 Show help
340  
341 =item B< -m, --man>
342  
343 Show manpage
344  
345 =item B< -v, --verbose>
346  
347 Be more verbose
348  
349 =item B<--version>
350  
351 Show version and license
352  
353 =item B<--apikey>
354  
355 API key to use Open NOS with
356  
357 =item B<--port>
358  
359 Port number to listen on, defaults to port 8081
360  
361 =back
362  
363 =head1 DIAGNOSTICS
364  
365 =head1 EXIT STATUS
366  
367 =head1 CONFIGURATION
368  
369 The account belonging to the API key must be configured to allow access to the
370 API from the IP range you are accessing the service from.
371  
372 =head1 DEPENDENCIES
373  
374 Perl 5.14.0, CGI, Getopt::Long, HTTP::Server::Brick, Pod::Usage,
375 Pod::Usage::CommandLine, Readonly, WWW::NOS::Open
376  
377 =head1 INCOMPATIBILITIES
378  
379 Version 2 of the API is not used.
380  
381 =head1 BUGS AND LIMITATIONS
382  
383 Only version 1 of the API is used.
384  
385 Please report any bugs or feature requests at
386 L<RT for rt.cpan.org|https://rt.cpan.org/Dist/Display.html?Queue=WWW-NOS-Open>.
387  
388 =head1 CONFIGURATION AND ENVIRONMENT
389  
390 Using the defaults it starts the HTTP service on port 8081.
391  
392 =head1 AUTHOR
393  
394 Roland van Ipenburg, E<lt>ipenburg@xs4all.nlE<gt>
395  
396 =head1 LICENSE AND COPYRIGHT
397  
398 Copyright 2012 by Roland van Ipenburg
399  
400 This program is free software; you can redistribute it and/or modify
401 it under the same terms as Perl itself, either Perl version 5.14.0 or,
402 at your option, any later version of Perl 5 you may have available.
403  
404 =head1 DISCLAIMER OF WARRANTY
405  
406 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
407 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
408 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
409 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
410 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
411 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
412 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
413 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
414 NECESSARY SERVICING, REPAIR, OR CORRECTION.
415  
416 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
417 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
418 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE
419 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
420 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
421 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
422 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
423 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
424 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
425 SUCH DAMAGES.
426  
427 =cut