[Jifty-commit] r1707 -
jifty/trunk/plugins/REST/lib/Jifty/Plugin/REST
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Sun Jul 30 03:45:35 EDT 2006
Author: audreyt
Date: Sun Jul 30 03:45:34 2006
New Revision: 1707
Modified:
jifty/trunk/plugins/REST/lib/Jifty/Plugin/REST/Dispatcher.pm
Log:
* J::P::REST::Dispatcher - all GET model URLs work, with 404s.
Modified: jifty/trunk/plugins/REST/lib/Jifty/Plugin/REST/Dispatcher.pm
==============================================================================
--- jifty/trunk/plugins/REST/lib/Jifty/Plugin/REST/Dispatcher.pm (original)
+++ jifty/trunk/plugins/REST/lib/Jifty/Plugin/REST/Dispatcher.pm Sun Jul 30 03:45:34 2006
@@ -23,10 +23,11 @@
dispatch $1;
};
-on GET '/=/model/*/*/*' => \&show_item;
-on GET '/=/model/*/*' => \&list_model_items;
-on GET '/=/model/*' => \&list_model_columns;
-on GET '/=/model' => \&list_models;
+on GET '/=/model/*/*/*/*' => \&show_item_field;
+on GET '/=/model/*/*/*' => \&show_item;
+on GET '/=/model/*/*' => \&list_model_items;
+on GET '/=/model/*' => \&list_model_columns;
+on GET '/=/model' => \&list_models;
on PUT '/=/model/*/*/*' => \&replace_item;
on DELETE '/=/model/*/*/*' => \&delete_item;
@@ -35,44 +36,80 @@
on GET '/=/action' => \&list_actions;
on POST '/=/action/*' => \&run_action;
-sub list { outs(\@_) }
+sub list {
+ my $prefix = shift;
+ outs($prefix, \@_)
+}
sub outs {
no warnings 'utf8';
+ my $prefix = shift;
my $accept = ($ENV{HTTP_ACCEPT} || '');
+ my $apache = Jifty->handler->apache;
+ my $url = Jifty->web->url(path => join '/', '=', map {
+ Jifty::Web->escape_uri($_)
+ } @$prefix);
+
if ($accept =~ /ya?ml/i) {
- Jifty->handler->apache->header_out('Content-Type' => 'text/yaml; charset=UTF-8');
+ $apache->header_out('Content-Type' => 'text/yaml; charset=UTF-8');
+ $apache->send_http_header;
print Jifty::YAML::Dump(@_);
}
elsif ($accept =~ /json/i) {
- Jifty->handler->apache->header_out('Content-Type' => 'text/json; charset=UTF-8');
+ $apache->header_out('Content-Type' => 'text/json; charset=UTF-8');
+ $apache->send_http_header;
print Jifty::JSON::objToJson( @_, { singlequote => 1 } );
}
elsif ($accept =~ /j(?:ava)?s|ecmascript/i) {
- Jifty->handler->apache->header_out('Content-Type' => 'application/javascript; charset=UTF-8');
+ $apache->header_out('Content-Type' => 'application/javascript; charset=UTF-8');
+ $apache->send_http_header;
print 'var $_ = ', Jifty::JSON::objToJson( @_, { singlequote => 1 } );
}
elsif ($accept =~ /perl/i) {
- Jifty->handler->apache->header_out('Content-Type' => 'application/perl; charset=UTF-8');
+ $apache->header_out('Content-Type' => 'application/perl; charset=UTF-8');
+ $apache->send_http_header;
print Data::Dumper::Dumper(@_);
}
elsif (ref($_[0]) eq 'ARRAY') {
print start_html(-encoding => 'UTF-8', -declare_xml => 1, -title => 'models'),
ul(map {
- li(a({-href => Jifty::Web->escape_uri($_).'/'}, Jifty::Web->escape($_)))
+ li(a({-href => "$url/".Jifty::Web->escape_uri($_)}, Jifty::Web->escape($_)))
} @{$_[0]}),
- end_html()
+ end_html();
}
- else {
+ elsif (ref($_[0]) eq 'HASH') {
print start_html(-encoding => 'UTF-8', -declare_xml => 1, -title => 'models'),
dl(map {
- dt(a({-href => Jifty::Web->escape_uri($_).'/'}, Jifty::Web->escape($_))),
- dd(Jifty::Web->escape($_[0]->{$_})),
+ dt(a({-href => "$url/".Jifty::Web->escape_uri($_)}, Jifty::Web->escape($_))),
+ dd(html_dump($_[0]->{$_})),
} sort keys %{$_[0]}),
- end_html()
+ end_html();
+ }
+ else {
+ print start_html(-encoding => 'UTF-8', -declare_xml => 1, -title => 'models'),
+ Jifty::Web->escape($_[0]),
+ end_html();
+ }
+
+ last_rule;
+}
+
+sub html_dump {
+ if (ref($_[0]) eq 'ARRAY') {
+ ul(map {
+ li(html_dump($_))
+ } @{$_[0]});
+ }
+ elsif (ref($_[0]) eq 'HASH') {
+ dl(map {
+ dt(Jifty::Web->escape($_)),
+ dd(html_dump($_[0]->{$_})),
+ } sort keys %{$_[0]}),
+ }
+ else {
+ Jifty::Web->escape($_[0]);
}
- abort(200);
}
sub model {
@@ -85,35 +122,53 @@
return $cls if $cls =~ /$model$/i;
}
- Jifty->handler->apache->header_out(Status => '404');
- last_rule;
+ abort(404);
}
sub list_models {
- list(Jifty->class_loader->models);
+ list(['model'], Jifty->class_loader->models);
}
sub list_model_columns {
- my ($model) = $1;
- outs({ map { $_->name => '' } model($model)->new->columns });
+ my ($model) = model($1);
+ outs(['model', $model], { map { $_->name => { %$_ } } $model->new->columns });
}
sub list_model_items {
# Normalize model name - fun!
- my ($model, $column) = ($1, $2);
- my $col = model($model)->new->collection_class->new;
+ my ($model, $column) = (model($1), $2);
+ my $col = $model->new->collection_class->new;
$col->unlimit;
$col->columns($column);
$col->order_by(column => $column);
- list(map { $_->__value($column) } @{ $col->items_array_ref || [] });
+ list(
+ ['model', $model, $column],
+ map { $_->__value($column) } @{ $col->items_array_ref || [] }
+ );
+}
+
+sub show_item_field {
+ my ($model, $column, $key, $field) = (model($1), $2, $3, $4);
+ my $rec = $model->new;
+ $rec->load_by_cols( $column => $key );
+ $rec->id or abort(404);
+ exists $rec->{values}{$field} or abort(404);
+ outs(
+ ['model', $model, $column, $key, $field],
+ $rec->{values}{$field}
+ );
}
sub show_item {
- my ($model, $column, $key) = ($1, $2, $3);
- my $rec = model($model)->new;
+ my ($model, $column, $key) = (model($1), $2, $3);
+ my $rec = $model->new;
$rec->load_by_cols( $column => $key );
- outs($rec->{values});
+ $rec->id or abort(404);
+ outs(
+ ['model', $model, $column, $key],
+ $rec->{values}
+ );
}
sub replace_item {
More information about the Jifty-commit
mailing list