Created
June 2, 2012 23:25
-
-
Save anonymous/2860450 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use Mojolicious::Lite; | |
app->defaults( | |
layout => 'default', | |
api_host => 'http://api.powerhousemuseum.com', | |
api_key => '8fbb61ea34d6f5d', | |
); | |
# start root position | |
get '/' => sub { | |
my $self = shift; | |
my $way = 'api/v1/category/xml/'; | |
my $order = 'order_by=name'; | |
my $url = Mojo::URL->new($self->stash('api_host').'/'.$way) | |
->query(api_key => $self->stash('api_key')); | |
$self->ua->get("$url&$order", sub { | |
my ($ua, $tx) = @_; | |
my $categories = $tx->res->dom->find('result categories category'); | |
$self->render('index', categories => $categories) | |
}); | |
}; | |
# once clicked on the $way link go here: | |
get '/*way' => sub { | |
my $self = shift; | |
my $url = Mojo::URL->new($self->stash('api_host').'/'.$self->param('way')) | |
->query(api_key => $self->stash('api_key')); | |
$self->ua->get($url, sub { | |
my ($ua, $tx) = @_; | |
my $items = $tx->res->dom->find('result items item'); | |
$self->render('items', items => $items) | |
}); | |
}; | |
#start the webapp: | |
app->start('daemon'); | |
#Start template files: | |
__DATA__ | |
@@ layouts/default.html.ep | |
<!doctype html> | |
<html> | |
<body> | |
<%= content %> | |
</body> | |
</html> | |
@@ index.html.ep | |
<center> | |
<table border=1> | |
<tr> | |
<td> | |
<b>Item Category Name</b> | |
</td> | |
<td> | |
<b>Number of Items</b> | |
</td> | |
</tr> | |
% for my $c (@$categories) { | |
<tr> | |
<td> | |
<a href="<%= $c->items_uri->text %>"><%= $c->name->text %></a> | |
</td> | |
<td> | |
<%= $c->num_items->text %> | |
</td> | |
</tr> | |
% } | |
</table> | |
</center> | |
@@ items.html.ep | |
<h1>Items</h1> | |
<ol> | |
% for my $i (@$items) { | |
<h2><a href="<%= $i->item_uri->text %>"><%= $i->title->text %></a></h2> | |
% if (my $url = $i->at('thumbnail url')) { | |
<img src="<%= $url->text %>" height="160" width="160"> | |
%} else { | |
<img src="http://i.imgur.com/NUo6Z.gif" height="160" width="160"> | |
% } | |
<p><%= $i->description->text %></p> | |
% } | |
</ol> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment