Changeset 27 in bookmarks for trunk/BookmarkApp.pm


Ignore:
Timestamp:
05/23/13 00:54:34 (11 years ago)
Author:
peter
Message:
  • protect the specific field request with an eval; if it fails because the method is not found, respond with a 404
  • use the JSON convert_blessed in the single bookmark view
  • use the accessors instead of the direct hash calls for ctime and mtime
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/BookmarkApp.pm

    r26 r27  
    236236        if ($field) { 
    237237            # respond with just the requested field as plain text 
     238            my $value = eval { $bookmark->$field }; 
     239            if ($@) { 
     240                if ($@ =~ /Can't locate object method/) { 
     241                    $self->header_props( 
     242                        -type    => 'text/plain', 
     243                        -charset => 'UTF-8', 
     244                        -status  => 404, 
     245                    ); 
     246                    return qq{"$field" is not a valid bookmark data field}; 
     247                } else { 
     248                    die $@; 
     249                } 
     250            } 
    238251            $self->header_props( 
    239252                -type    => 'text/plain', 
    240253                -charset => 'UTF-8', 
    241254            ); 
    242             my $value = $bookmark->{$field}; 
    243255            return ref $value eq 'ARRAY' ? join(',', @{ $value }) : $value; 
    244256        } else { 
     
    248260                    -charset => 'UTF-8', 
    249261                ); 
    250                 return decode_utf8(encode_json($bookmark)); 
     262                return decode_utf8(JSON->new->utf8->convert_blessed->encode($bookmark)); 
    251263            } else { 
    252264                # display the bookmark form for this bookmark 
    253265                $bookmark->{exists} = 1; 
    254                 $bookmark->{created} = "Created " . localtime($bookmark->{ctime}); 
    255                 $bookmark->{created} .= '; Updated ' . localtime($bookmark->{mtime}) unless $bookmark->{ctime} == $bookmark->{mtime}; 
     266                $bookmark->{created} = "Created " . localtime($bookmark->ctime); 
     267                $bookmark->{created} .= '; Updated ' . localtime($bookmark->mtime) unless $bookmark->ctime == $bookmark->mtime; 
    256268                $self->header_props( 
    257269                    -type    => 'text/html', 
Note: See TracChangeset for help on using the changeset viewer.