Advertisement
Guest User

Untitled

a guest
Jun 6th, 2012
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. get '/ph/category_list*way' => sub {
  2. my $self = shift;
  3. #construct rest end point URL for all categories - creating from default vars
  4. my $way = $self->param('way');
  5. my $limit = $self->stash('limit');
  6. my $api_host = $self->stash('api_host');
  7. my $api_key = $self->stash('api_key');
  8. my $order = 'order_by=name';
  9. my $begin = 1;
  10. my $start;
  11.  
  12. if (defined ($self->param('start'))) {
  13. $start = $self->param('start');
  14. } else {
  15. $start = 0;
  16. }
  17.  
  18. # used in the pagnation
  19. my $rest_base = "/ph/category_list/$way?api_key=$api_key&$order&limit=$limit";
  20. # used for the rest request
  21. my $url = "$api_host/$way?api_key=$api_key&$order&start=$start";
  22. #fine get stuff from DOM
  23. my $result = $self->ua->get($url)->res->dom->result;
  24. #$start = $result->start->text;
  25. my $total= $result->total->text;
  26.  
  27. use POSIX qw(ceil);
  28. my $modulus = ceil($total/$limit);
  29. warn $self->dumper($modulus);
  30.  
  31. # Delayed rendering - Disable automatic rendering, for long polling
  32. #$self->render_later;
  33.  
  34.  
  35. my @array;
  36. for (my $i = 0; $i < $modulus; $i ++) {
  37. if ($total < $limit) {
  38. push (@array, "$begin.\"-\".$total." );
  39. return;
  40. }
  41. push (@array, "$begin.\"-\".$limit." );
  42. $begin = $begin + $limit;
  43. $limit = $limit + $limit;
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50. #$self->stash(start => $start);
  51.  
  52. # Using Mojo UserAgent to make a GET request to the DOM and search the tree up to 'category' - put that into an array @categories
  53. # Then Render that data ready to be put into the category_list.html.ep HTML template
  54. $self->ua->get(
  55. "$url",
  56. sub {
  57. my ( $ua, $tx ) = @_;
  58. my $categories =
  59. $tx->res->dom->find('result > categories > category');
  60. $self->render( 'category_list',
  61. categories => $categories,
  62. start => $start, limit => $limit, rest_base => $rest_base,
  63. begin => $begin, modulus => $modulus, array => \@array);
  64. }
  65. );
  66.  
  67. };
  68.  
  69.  
  70. @@ pagination.html.ep
  71.  
  72. % for my $out (@$array) {
  73. <%= $out %>
  74. % }
  75.  
  76. @@ category_list.html.ep
  77. <div class="center_div">
  78. <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>
  79.  
  80.  
  81. %= include 'pagination'
  82.  
  83.  
  84.  
  85. %#Create a table from the categories array
  86.  
  87. % for my $c (@$categories) {
  88. <tr><td><a href="/ph/category<%= $c->items_uri->text %>"><%= $c->name->text %></a></td><td><%= $c->num_items->text %></td></tr>
  89. % }
  90. </table>
  91. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement