Changeset 7 in bookmarks for trunk/BookmarkApp.pm


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
File:
1 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 { 
Note: See TracChangeset for help on using the changeset viewer.