Changeset 112 in bookmarks for trunk


Ignore:
Timestamp:
02/17/16 22:05:50 (8 years ago)
Author:
peter
Message:
  • Added the sidebar resource.
  • Moved the list resources to list.
  • Respond with a 406 if the required modules are not available for XBEL, Atom, or CSV.
Location:
trunk/lib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Bookmarks/Controller.pm

    r108 r112  
    7171} 
    7272 
     73sub _get_search_from_request { 
     74    my $self = shift; 
     75 
     76    my @tags   = grep { $_ ne '' } $self->request->param('tag'); 
     77    my $query  = $self->request->param('q'); 
     78    my $limit  = $self->request->param('limit'); 
     79    my $offset = $self->request->param('offset'); 
     80    my $page   = $self->request->param('page'); 
     81 
     82    return $self->bookmarks->search({ 
     83        query  => $query, 
     84        tags   => \@tags, 
     85        limit  => $limit, 
     86        offset => $offset, 
     87        page   => $page, 
     88    }); 
     89} 
     90 
    7391sub list { 
    7492    my $self = shift; 
     
    7997    my $format = $self->request->param('format') || 'html'; 
    8098 
    81     my @tags = grep { $_ ne '' } $self->request->param('tag'); 
    82     my $query = $self->request->param('q'); 
    83     my $limit = $self->request->param('limit'); 
    84     my $offset = $self->request->param('offset'); 
    85     my $page = $self->request->param('page'); 
    86  
    8799    my $list = Bookmarks::List->new({ 
    88100        bookmarks => $self->bookmarks, 
    89         search    => $self->bookmarks->search({ 
    90             query  => $query, 
    91             tags   => \@tags, 
    92             limit  => $limit, 
    93             offset => $offset, 
    94             page   => $page, 
    95         }), 
     101        search    => $self->_get_search_from_request, 
    96102    }); 
    97103 
     
    101107    } 
    102108    return $list->$as_format; 
     109} 
     110 
     111sub sidebar { 
     112    my $self = shift; 
     113 
     114    my $list = Bookmarks::List->new({ 
     115        bookmarks => $self->bookmarks, 
     116        search    => $self->_get_search_from_request, 
     117    }); 
     118 
     119    require Template; 
     120    require File::Basename; 
     121    my $template = Template->new({ INCLUDE_PATH => File::Basename::dirname($INC{'Bookmarks/List.pm'}) }); 
     122 
     123    my @all_tags = $list->bookmarks->get_tags({ selected => @{ $list->tags }[0] }); 
     124    my @cotags = $list->bookmarks->get_cotags({ search => $list->search }); 
     125 
     126    $template->process( 
     127        'list.tt', 
     128        { 
     129            base_url     => $list->bookmarks->base_uri, 
     130            title        => $list->title, 
     131            query        => $list->query, 
     132            selected_tag => @{ $list->tags }[0], 
     133            search_tags  => $list->tags, 
     134            links        => [ $list->_get_list_links('text/html', { q => $list->query, tag => $list->tags }) ], 
     135            all_tags     => \@all_tags, 
     136            cotags       => \@cotags, 
     137            resources    => $list->results, 
     138            pages        => $list->search->pages, 
     139        }, 
     140        \my $output, 
     141    ); 
     142    return [200, ['Content-Type' => 'text/html; charset=UTF-8'], [$output]]; 
    103143} 
    104144 
  • trunk/lib/Bookmarks/List.pm

    r106 r112  
    3232            text => 'JSON', 
    3333            type => 'application/json', 
     34            path => 'list', 
    3435            query => { 
    3536                %$query, 
     
    4041            text => 'XBEL', 
    4142            type => 'application/xml', 
     43            path => 'list', 
    4244            query => { 
    4345                %$query, 
     
    5658            text => 'CSV', 
    5759            type => 'text/csv', 
     60            path => 'list', 
    5861            query => { 
    5962                %$query, 
     
    6467            text => 'URI List', 
    6568            type => 'text/uri-list', 
     69            path => 'list', 
    6670            query => { 
    6771                %$query, 
     
    7276            text => 'HTML', 
    7377            type => 'text/html', 
     78            path => 'list', 
    7479            query => { 
    7580                %$query, 
     
    105110sub as_xbel { 
    106111    my $self = shift; 
    107     require XML::XBEL; 
    108     #TODO: conditional support; if XML::XBEL is not present, return a 5xx response 
     112 
     113    eval { require XML::XBEL; } or 
     114        return [406, ['Content-Type' => 'text/plain'], ['This server does not support XBEL lists']]; 
    109115 
    110116    my $xbel = XML::XBEL->new; 
     
    147153sub as_csv { 
    148154    my $self = shift; 
    149     require Text::CSV::Encoded; 
     155 
     156    eval { require Text::CSV::Encoded } or 
     157        return [406, ['Content-Type' => 'text/plain'], ['This server does not support CSV lists']]; 
     158 
    150159    my $csv = Text::CSV::Encoded->new({ encoding_out => 'utf8' }); 
    151160    my $text = qq{id,uri,title,tags,ctime,mtime\n}; 
     
    182191    my $template = Template->new({ INCLUDE_PATH => File::Basename::dirname($INC{'Bookmarks/List.pm'}) }); 
    183192 
    184     my @all_tags = $self->bookmarks->get_tags({ selected => @{ $self->tags }[0] }); 
    185     my @cotags = $self->bookmarks->get_cotags({ search => $self->search }); 
    186  
    187193    $template->process( 
    188         'list.tt', 
     194        'html_list.tt', 
    189195        { 
    190196            base_url     => $self->bookmarks->base_uri, 
    191197            title        => $self->title, 
    192198            query        => $self->query, 
    193             selected_tag => @{ $self->tags }[0], 
    194             search_tags  => $self->tags, 
    195199            links        => [ $self->_get_list_links('text/html', { q => $self->query, tag => $self->tags }) ], 
    196             all_tags     => \@all_tags, 
    197             cotags       => \@cotags, 
    198200            resources    => $self->results, 
    199201            pages        => $self->search->pages, 
     
    207209    my $self = shift; 
    208210 
    209     require XML::Atom; 
     211    eval { require XML::Atom } or 
     212        return [406, ['Content-Type' => 'text/plain'], ['This server does not support Atom lists']]; 
     213 
    210214    $XML::Atom::DefaultVersion = "1.0"; 
    211215 
  • trunk/lib/BookmarksApp.pm

    r107 r112  
    6161                } 
    6262 
    63                 # otherwise return a list 
    64                 return $self->_controller->list; 
     63                # otherwise return the sidebar 
     64                return $self->_controller->sidebar; 
    6565            }; 
    6666            POST { 
Note: See TracChangeset for help on using the changeset viewer.