[Jifty-commit] r2042 - in jifty/trunk: lib/Jifty/Plugin/REST
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Fri Oct 20 11:12:31 EDT 2006
Author: nelhage
Date: Fri Oct 20 11:12:30 2006
New Revision: 2042
Modified:
jifty/trunk/lib/Jifty/Plugin/REST/Dispatcher.pm
jifty/trunk/t/TestApp-Plugin-REST/t/02-basic-use.t
Log:
You can now run actions and get back arbitrary data formats
Modified: jifty/trunk/lib/Jifty/Plugin/REST/Dispatcher.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/REST/Dispatcher.pm (original)
+++ jifty/trunk/lib/Jifty/Plugin/REST/Dispatcher.pm Fri Oct 20 11:12:30 2006
@@ -41,9 +41,15 @@
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);
+ my @prefix;
+ my $url;
+
+ if($prefix) {
+ @prefix = map {s/::/./g; $_} @$prefix;
+ my $url = Jifty->web->url(path => join '/', '=', map {
+ Jifty::Web->escape_uri($_)
+ } @prefix);
+ }
if ($accept =~ /ya?ml/i) {
$apache->header_out('Content-Type' => 'text/x-yaml; charset=UTF-8');
@@ -68,11 +74,9 @@
elsif ($accept =~ qr|^(text/)?xml$|i) {
$apache->header_out('Content-Type' => 'text/xml; charset=UTF-8');
$apache->send_http_header;
- print render_as_xml(@_);
-
+ print render_as_xml(@_);
}
else {
-
$apache->header_out('Content-Type' => 'text/html; charset=UTF-8');
$apache->send_http_header;
print render_as_html($prefix, $url, @_);
@@ -105,14 +109,18 @@
if (ref($content) eq 'ARRAY') {
return start_html(-encoding => 'UTF-8', -declare_xml => 1, -title => 'models'),
ul(map {
- li(a({-href => "$url/".Jifty::Web->escape_uri($_)}, Jifty::Web->escape($_)))
+ li($prefix ?
+ a({-href => "$url/".Jifty::Web->escape_uri($_)}, Jifty::Web->escape($_))
+ : Jifty::Web->escape($_) )
} @{$content}),
end_html();
}
elsif (ref($content) eq 'HASH') {
return start_html(-encoding => 'UTF-8', -declare_xml => 1, -title => 'models'),
dl(map {
- dt(a({-href => "$url/".Jifty::Web->escape_uri($_)}, Jifty::Web->escape($_))),
+ dt($prefix ?
+ a({-href => "$url/".Jifty::Web->escape_uri($_)}, Jifty::Web->escape($_))
+ : Jifty::Web->escape($_)),
dd(html_dump($content->{$_})),
} sort keys %{$content}),
end_html();
@@ -162,7 +170,7 @@
}
sub list_models {
- list(['model'], Jifty->class_loader->models);
+ list(['model'], map {s/::/./g; $_ } Jifty->class_loader->models);
}
sub list_model_columns {
@@ -209,7 +217,7 @@
}
sub list_actions {
- list(['action'], Jifty->api->actions);
+ list(['action'], map {s/::/./g; $_} Jifty->api->actions);
}
sub list_action_params {
@@ -251,19 +259,35 @@
local $@;
eval { $action->run };
- if ($@ or $action->result->failure) {
+ if ($@) {
abort(500);
}
my $rec = $action->{record};
- if ($rec and $rec->isa('Jifty::Record') and $rec->id) {
+ if ($action->result->success && $rec and $rec->isa('Jifty::Record') and $rec->id) {
my $url = Jifty->web->url(path => join '/', '=', map {
Jifty::Web->escape_uri($_)
} 'model', ref($rec), 'id', $rec->id);
Jifty->handler->apache->header_out('Location' => $url);
}
- #outs(undef, [$action->result->message, Jifty->web->response->messages]);
- print start_html(-encoding => 'UTF-8', -declare_xml => 1, -title => 'models'), ul(map { li(html_dump($_)) } $action->result->message, Jifty->web->response->messages), end_html();
+
+ my $result = $action->result;
+
+ my $out = {};
+ $out->{success} = $result->success;
+ $out->{message} = $result->message;
+ $out->{error} = $result->error;
+ $out->{field_errors} = {$result->field_errors};
+ for (keys %{$out->{field_errors}}) {
+ delete $out->{field_errors}->{$_} unless $out->{field_errors}->{$_};
+ }
+ $out->{field_warnings} = {$result->field_warnings};
+ for (keys %{$out->{field_warnings}}) {
+ delete $out->{field_warnings}->{$_} unless $out->{field_warnings}->{$_};
+ }
+ $out->{content} = $result->content;
+
+ outs(undef, $out);
last_rule;
}
Modified: jifty/trunk/t/TestApp-Plugin-REST/t/02-basic-use.t
==============================================================================
--- jifty/trunk/t/TestApp-Plugin-REST/t/02-basic-use.t (original)
+++ jifty/trunk/t/TestApp-Plugin-REST/t/02-basic-use.t Fri Oct 20 11:12:30 2006
@@ -35,7 +35,7 @@
$mech->get_ok("$URL/=/model.yml", "Got model list");
my $list = Jifty::YAML::Load($mech->content);
is(scalar @$list, 1, "Got one model");
-is($list->[0],'TestApp::Plugin::REST::Model::User');
+is($list->[0],'TestApp.Plugin.REST.Model.User');
# on GET '/=/model/*' => \&list_model_keys;
$mech->get_ok('/=/model/User');
@@ -83,13 +83,13 @@
# on GET '/=/action' => \&list_actions;
-my @actions = qw(TestApp::Plugin::REST::Action::CreateUser
- TestApp::Plugin::REST::Action::UpdateUser
- TestApp::Plugin::REST::Action::DeleteUser
- TestApp::Plugin::REST::Action::SearchUser
- TestApp::Plugin::REST::Action::DoSomething
- Jifty::Action::Autocomplete
- Jifty::Action::Redirect);
+my @actions = qw(TestApp.Plugin.REST.Action.CreateUser
+ TestApp.Plugin.REST.Action.UpdateUser
+ TestApp.Plugin.REST.Action.DeleteUser
+ TestApp.Plugin.REST.Action.SearchUser
+ TestApp.Plugin.REST.Action.DoSomething
+ Jifty.Action.Autocomplete
+ Jifty.Action.Redirect);
$mech->get_ok('/=/action/');
is($mech->status, 200);
@@ -145,55 +145,38 @@
$mech->content_contains('Something happened!');
-TODO: {
- $mech->post( $URL . '/=/action/DoSomething', { email => 'bad at email.com' } );
+$mech->post( $URL . '/=/action/DoSomething', { email => 'bad at email.com' } );
- local $TODO = "Waiting for actions to return validation errors";
- $mech->content_contains('Bad looking email');
- $mech->content_lacks('Something happened!');
+$mech->content_contains('Bad looking email');
+$mech->content_lacks('Something happened!');
- $mech->post( $URL . '/=/action/DoSomething', { email => 'warn at email.com' } );
+$mech->post( $URL . '/=/action/DoSomething', { email => 'warn at email.com' } );
- $mech->content_contains('Warning for email');
- $mech->content_contains('Something happened!');
-}
+$mech->content_contains('Warning for email');
+$mech->content_contains('Something happened!');
# Test YAML posts
-TODO: {
- local $TODO = "Waiting for YAML posts to work right";
-
- yaml_post( $URL . '/=/action/DoSomething.yml', { email => 'good at email.com' } );
+$mech->post ( $URL . '/=/action/DoSomething.yml', { email => 'good at email.com' } );
- eval {
- %content = %{get_content()};
- };
+eval {
+ %content = %{get_content()};
+};
- ok($content{success});
- is($content{message}, 'Something happened');
+ok($content{success});
+is($content{message}, 'Something happened!');
- yaml_post( $URL . '/=/action/DoSomething', { email => 'bad at email.com' } );
+$mech->post ( $URL . '/=/action/DoSomething.yaml', { email => 'bad at email.com' } );
- eval {
- %content = %{get_content()};
- };
-
- ok(!$content{success});
- is($content{error}, 'Bad looking email');
+eval {
+ %content = %{get_content()};
};
+ok(!$content{success}, "Action that doesn't validate fails");
+is($content{field_errors}{email}, 'Bad looking email');
-sub get_content { return Jifty::YAML::Load($mech->content)}
-sub yaml_post {
- my $url = shift;
- my $data = shift;
- my $request = HTTP::Request->new('POST', $url);
- $request->header('Content-Type', 'text/yaml');
- $request->content(Jifty::YAML::Dump($data));
- $mech->request($request);
-
-}
+sub get_content { return Jifty::YAML::Load($mech->content)}
1;
More information about the Jifty-commit
mailing list