Changeset 7 in bookmarks for trunk


Ignore:
Timestamp:
03/23/12 23:50:49 (12 years ago)
Author:
peter
Message:
  • added a base_uri property to the Bookmarks class; if present, it adds a bookmark_uri key to bookmarks returned by get_bookmark() and get_resources()
  • an individual bookmark can be returned as JSON by passing a "?format=json" parameter to the individual bookmark GET request
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/BookmarkApp.pm

    r6 r7  
    1111use Bookmarks; 
    1212 
     13use URI; 
     14my $base_uri = URI->new; 
     15$base_uri->scheme('http'); 
     16$base_uri->host($ENV{SERVER_NAME}); 
     17$base_uri->port($ENV{SERVER_PORT}); 
     18$base_uri->path($ENV{SCRIPT_NAME} . '/'); 
     19 
    1320my $dbname = 'new.db'; 
    1421my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", "", "", { RaiseError => 1 }); 
    1522my $bookmarks = Bookmarks->new({ 
    1623    dbh => $dbh, 
     24    base_uri => $base_uri, 
    1725}); 
    1826 
     
    7280        return decode_utf8( 
    7381            encode_json({ 
    74                 resources => \@resources, 
     82                bookmarks => \@resources, 
    7583            }) 
    7684        ); 
     
    102110    my $id = $self->param('id'); 
    103111    my $field = $self->param('field'); 
     112    my $format = $self->query->param('format') || 'html'; 
    104113 
    105114    my $bookmark = $bookmarks->get_bookmark({ id => $id }); 
     
    114123            return ref $value eq 'ARRAY' ? join(',', @{ $value }) : $value; 
    115124        } else { 
    116             # display the bookmark form for this bookmark 
    117             $bookmark->{exists} = 1; 
    118             $bookmark->{created} = "Created " . localtime($bookmark->{ctime}); 
    119             $bookmark->{created} .= '; Updated ' . localtime($bookmark->{mtime}) unless $bookmark->{ctime} == $bookmark->{mtime}; 
    120             $self->header_props( 
    121                 -type    => 'text/html', 
    122                 -charset => 'UTF-8', 
    123             ); 
    124             return $self->tt_process( 
    125                 'bookmark.tt', 
    126                 $bookmark, 
    127             ); 
     125            if ($format eq 'json') { 
     126                $self->header_props( 
     127                    -type    => 'application/json', 
     128                    -charset => 'UTF-8', 
     129                ); 
     130                return decode_utf8(encode_json($bookmark)); 
     131            } else { 
     132                # display the bookmark form for this bookmark 
     133                $bookmark->{exists} = 1; 
     134                $bookmark->{created} = "Created " . localtime($bookmark->{ctime}); 
     135                $bookmark->{created} .= '; Updated ' . localtime($bookmark->{mtime}) unless $bookmark->{ctime} == $bookmark->{mtime}; 
     136                $self->header_props( 
     137                    -type    => 'text/html', 
     138                    -charset => 'UTF-8', 
     139                ); 
     140                return $self->tt_process( 
     141                    'bookmark.tt', 
     142                    $bookmark, 
     143                ); 
     144            } 
    128145        } 
    129146    } else { 
  • trunk/Bookmarks.pm

    r2 r7  
    44use Bookmark; 
    55 
    6 has dbh => ( is => 'ro'); 
     6has dbh      => ( is => 'ro' ); 
     7has base_uri => ( is => 'ro' ); 
    78 
    89sub get_bookmark { 
     
    2425        $sth_tag->execute($bookmark->{uri}); 
    2526        $bookmark->{tags} = [ map { $$_[0] } @{ $sth_tag->fetchall_arrayref } ]; 
     27        if ($self->base_uri) { 
     28            $bookmark->{bookmark_uri} = $self->base_uri . $bookmark->{id}; 
     29        } 
    2630    } 
    2731    return $bookmark; 
     
    4650        $sth_tag->execute($resource->{uri}); 
    4751        $resource->{tags} = [ map { $$_[0] } @{ $sth_tag->fetchall_arrayref } ]; 
     52        if ($self->base_uri) { 
     53            $resource->{bookmark_uri} = $self->base_uri . $resource->{id}; 
     54        } 
    4855        push @resources, $resource; 
    4956    } 
Note: See TracChangeset for help on using the changeset viewer.