Changeset 37 in bookmarks


Ignore:
Timestamp:
05/27/13 14:46:49 (12 years ago)
Author:
peter
Message:
  • moved the creation of the $bookmarks and $base_uri objects into the BookmarkApp::setup() method; they are now parameters of the app
  • added a _bookmarks accessor as a shortcut to get the bookmarks object
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/BookmarkApp.pm

    r36 r37  
    1212 
    1313use URI; 
    14 my $base_uri = URI->new; 
    15 $base_uri->scheme('http'); 
    16 $base_uri->host($ENV{HTTP_X_FORWARDED_HOST} || $ENV{SERVER_NAME}); 
    17 $base_uri->port($ENV{SERVER_PORT}); 
    18 $base_uri->path($ENV{SCRIPT_NAME} . '/'); 
    1914 
    2015my $dbname = 'fk.db'; 
    21 my $bookmarks = Bookmarks->new({ 
    22     dbname   => $dbname, 
    23     base_uri => $base_uri, 
    24 }); 
    2516 
    2617sub setup { 
     
    3425        edit 
    3526    }]); 
    36 } 
     27    my $base_uri = URI->new; 
     28    $base_uri->scheme('http'); 
     29    $base_uri->host($ENV{HTTP_X_FORWARDED_HOST} || $ENV{SERVER_NAME}); 
     30    $base_uri->port($ENV{SERVER_PORT}); 
     31    $base_uri->path($ENV{SCRIPT_NAME} . '/'); 
     32 
     33    my $bookmarks = Bookmarks->new({ 
     34        dbname   => $dbname, 
     35        base_uri => $base_uri, 
     36    }); 
     37 
     38    $self->param( 
     39        base_uri  => $base_uri, 
     40        bookmarks => $bookmarks, 
     41    ); 
     42} 
     43 
     44sub _bookmarks { $_[0]->param('bookmarks') } 
    3745 
    3846sub list { 
     
    4351    # see if a bookmark for that URI already exists 
    4452    if (defined(my $uri = $q->param('uri'))) { 
    45         my $bookmark = $bookmarks->get_bookmark({ uri => $uri }); 
     53        my $bookmark = $self->_bookmarks->get_bookmark({ uri => $uri }); 
    4654        if ($bookmark) { 
    4755            # redirect to the view of the existing bookmark 
     
    7381    my $limit = $q->param('limit'); 
    7482    my $offset = $q->param('offset'); 
    75     my @resources = $bookmarks->get_bookmarks({ 
     83    my @resources = $self->_bookmarks->get_bookmarks({ 
    7684        tag    => \@tags, 
    7785        limit  => $limit, 
    7886        offset => $offset, 
    7987    }); 
    80     my @all_tags = $bookmarks->get_tags({ selected => $tags[0] }); 
    81     my @cotags = $bookmarks->get_cotags({ tag => \@tags }); 
     88    my @all_tags = $self->_bookmarks->get_tags({ selected => $tags[0] }); 
     89    my @cotags = $self->_bookmarks->get_cotags({ tag => \@tags }); 
    8290     
    8391    my $title = 'Bookmarks' . (@tags ? " tagged as " . join(' & ', @tags) : ''); 
     
    185193        ); 
    186194        for my $link (@links) { 
    187             $link->{href} = URI->new_abs($link->{path} || '', $base_uri); 
     195            $link->{href} = URI->new_abs($link->{path} || '', $self->param('base_uri')); 
    188196            $link->{href}->query_form($link->{query}); 
    189197        } 
     
    222230    $feed->title($title); 
    223231 
    224     my $feed_uri = URI->new_abs('feed', $base_uri); 
     232    my $feed_uri = URI->new_abs('feed', $self->param('base_uri')); 
    225233    $feed_uri->query_form(tag => \@tags); 
    226234    $feed->id($feed_uri->canonical); 
     
    235243    $html_link->type('text/html'); 
    236244    $html_link->rel('alternate'); 
    237     my $html_uri = $base_uri->clone; 
     245    my $html_uri = $self->param('base_uri')->clone; 
    238246    $html_uri->query_form(tag => \@tags); 
    239247    $html_link->href($html_uri->canonical); 
     
    241249 
    242250    # construct a feed from the most recent 12 bookmarks 
    243     for my $bookmark ($bookmarks->get_bookmarks({ tag => \@tags, limit => 12 })) { 
     251    for my $bookmark ($self->_bookmarks->get_bookmarks({ tag => \@tags, limit => 12 })) { 
    244252        my $entry = XML::Atom::Entry->new; 
    245253        $entry->id($bookmark->bookmark_uri->canonical); 
     
    268276    my $format = $self->query->param('format') || 'html'; 
    269277 
    270     my $bookmark = $bookmarks->get_bookmark({ id => $id }); 
     278    my $bookmark = $self->_bookmarks->get_bookmark({ id => $id }); 
    271279    if ($bookmark) { 
    272280        if ($format eq 'json') { 
     
    305313    my $field = $self->param('field'); 
    306314 
    307     my $bookmark = $bookmarks->get_bookmark({ id => $id }); 
     315    my $bookmark = $self->_bookmarks->get_bookmark({ id => $id }); 
    308316    if ($bookmark) { 
    309317        # respond with just the requested field as plain text 
     
    345353    my $title = $q->param('title'); 
    346354    my @tags = split ' ', $q->param('tags'); 
    347     $bookmarks->add({ 
     355    $self->_bookmarks->add({ 
    348356        uri   => $uri, 
    349357        title => $title, 
Note: See TracChangeset for help on using the changeset viewer.