Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- get '/ph/category_list*way' => sub {
- my $self = shift;
- #construct rest end point URL for all categories - creating from default vars
- my $way = $self->param('way');
- my $limit = $self->stash('limit');
- my $api_host = $self->stash('api_host');
- my $api_key = $self->stash('api_key');
- my $order = 'order_by=name';
- my $begin = 1;
- my $start;
- if (defined ($self->param('start'))) {
- $start = $self->param('start');
- } else {
- $start = 0;
- }
- # used in the pagnation
- my $rest_base = "/ph/category_list/$way?api_key=$api_key&$order&limit=$limit";
- # used for the rest request
- my $url = "$api_host/$way?api_key=$api_key&$order&start=$start";
- #fine get stuff from DOM
- my $result = $self->ua->get($url)->res->dom->result;
- #$start = $result->start->text;
- my $total= $result->total->text;
- use POSIX qw(ceil);
- my $modulus = ceil($total/$limit);
- warn $self->dumper($modulus);
- # Delayed rendering - Disable automatic rendering, for long polling
- #$self->render_later;
- my @array;
- for (my $i = 0; $i < $modulus; $i ++) {
- if ($total < $limit) {
- push (@array, "$begin.\"-\".$total." );
- return;
- }
- push (@array, "$begin.\"-\".$limit." );
- $begin = $begin + $limit;
- $limit = $limit + $limit;
- }
- #$self->stash(start => $start);
- # Using Mojo UserAgent to make a GET request to the DOM and search the tree up to 'category' - put that into an array @categories
- # Then Render that data ready to be put into the category_list.html.ep HTML template
- $self->ua->get(
- "$url",
- sub {
- my ( $ua, $tx ) = @_;
- my $categories =
- $tx->res->dom->find('result > categories > category');
- $self->render( 'category_list',
- categories => $categories,
- start => $start, limit => $limit, rest_base => $rest_base,
- begin => $begin, modulus => $modulus, array => \@array);
- }
- );
- };
- @@ pagination.html.ep
- % for my $out (@$array) {
- <%= $out %>
- % }
- @@ category_list.html.ep
- <div class="center_div">
- <h1>Powerhouse API Category List</h1><table border=1 class="center_table"><tr><td><b>Item Category Name</b></td><td><b>Number of Items</b></td></tr>
- %= include 'pagination'
- %#Create a table from the categories array
- % for my $c (@$categories) {
- <tr><td><a href="/ph/category<%= $c->items_uri->text %>"><%= $c->name->text %></a></td><td><%= $c->num_items->text %></td></tr>
- % }
- </table>
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement