SHOW:
|
|
- or go back to the newest paste.
1 | #!/usr/bin/perl | |
2 | use Mojolicious::Lite; | |
3 | get '/' => sub { | |
4 | my $self = shift; | |
5 | ||
6 | my $url = "http://api.powerhousemuseum.com/api/v1/item/970/xml/?api_key=8fbb61ea34d6f5d"; | |
7 | my @items_detail = | |
8 | $self->ua->max_redirects(5)->get("$url")->res->dom->find('result > item') | |
9 | ->map( | |
10 | sub { | |
11 | { | |
12 | - | names => $_->at('names item') ? $_->names->item->text : undef, |
12 | + | names => do { my $u = $_->at('names item'); $u ? $u->text : '' }, |
13 | - | title=> $_->at('title') ? $_->title->text : undef, |
13 | + | title => do { my $u = $_->at('title'); $u ? $u->text : '' }, |
14 | - | summary=> $_->at('summary') ? $_->summary->text : undef, |
14 | + | summary => do { my $u = $_->at('summary'); $u ? $u->text : '' }, |
15 | - | description=> $_->at('description') ? $_->description->text : undef, |
15 | + | description => do { my $u = $_->at('description'); $u ? $u->text : '' }, |
16 | - | importance=> $_->at('importance') ? $_->importance->text : undef, |
16 | + | importance => do { my $u = $_->at('importance'); $u ? $u->text : '' }, |
17 | - | registration_number=> $_->at('registration_number') ? $_->registration_number->text : undef, |
17 | + | registration_number => do { my $u = $_->at('registration_number'); $u ? $u->text : '' }, |
18 | - | thumbnail => $_->at('thumbnail url')? $_->thumbnail->url->text: 'http://i.imgur.com/NUo6Z.gif', |
18 | + | thumbnail => do { my $u = $_->at('thumbnail url'); $u ? $u->text : 'http://i.imgur.com/NUo6Z.gif' }, |
19 | - | multimedia_uri=> $_->at('multimedia_uri') ? $_->multimedia_uri->text : undef, |
19 | + | multimedia_uri => do { my $u = $_->at('multimedia_uri'); $u ? $u->text : '' }, |
20 | }; | |
21 | } | |
22 | )->each; | |
23 | ||
24 | $self->render( 'item', items_detail => \@items_detail ); | |
25 | ||
26 | ||
27 | }; | |
28 | #start the webapp: | |
29 | app->start('daemon'); | |
30 | ||
31 | #Start template files: | |
32 | __DATA__ | |
33 | @@ item.html.ep | |
34 | <h1>Item detail</h1> | |
35 | <dl> | |
36 | % for my $d (@$items_detail) { | |
37 | % if ($d->{title} !~ /None/) { | |
38 | <dt><b>Title</b><dt> | |
39 | <dd><%= $d->{title} %><dd> | |
40 | %} | |
41 | % if ($d->{names} !~ /None/) { | |
42 | <dt><b> Name </b></dt> | |
43 | <dd><%= $d->{names} %></dd> | |
44 | %} | |
45 | % if ($d->{summary} !~ /None/) { | |
46 | <dt><b> Summary </b></dt> | |
47 | <dd><%= $d->{summary} %></dd> | |
48 | %} | |
49 | % if ($d->{description} !~ /None/) { | |
50 | <dt><b> Description </b></dt> | |
51 | <dd><%= $d->{description} %></dd> | |
52 | %} | |
53 | % if ($d->{importance} !~ /None/) { | |
54 | <dt><b> Importance </b></dt> | |
55 | <dd><%= $d->{importance} %></dd> | |
56 | %} | |
57 | % if ($d->{registration_number} !~ /None/) { | |
58 | <dt><b> Registration Number </b></dt> | |
59 | <dd><%= $d->{registration_number} %></dd> | |
60 | %} | |
61 | <dt><b> Thumbnail </b></dt> | |
62 | <dd><img src="<%= $d->{thumbnail} %>" height="160" width="160" /></dd> | |
63 | % if ($d->{multimedia_uri} !~ /None/) { | |
64 | <dt><b> Display Multimedia From Remote REST resource </b></dt> | |
65 | <dd><%= $d->{multimedia_uri}%></dd> | |
66 | %} | |
67 | % } | |
68 | </dl> |