Add RESTful Content-Type bits to the root controller, add a simple test for summary...
Tomas Doran [Mon, 11 Jan 2010 00:07:32 +0000 (00:07 +0000)]
lib/Gitalist/Controller/Root.pm
t/json_view.t [new file with mode: 0644]

index 396296c..be29129 100644 (file)
@@ -93,7 +93,7 @@ A summary of what's happening in the repo.
 
 sub summary : Chained('base') Args(0) {
   my ( $self, $c ) = @_;
-  my $repository = $c->stash->{Repository};
+  my $repository = $c->stash->{data} = $c->stash->{Repository};
   $c->detach('error_404') unless $repository;
   my $commit = $self->_get_object($c);
   my @heads  = @{$repository->heads};
@@ -658,12 +658,15 @@ sub base : Chained('/root') PathPart('') CaptureArgs(0) {
   );
 }
 
-sub end : ActionClass('RenderView') {
+sub end : ActionClass('Serialize') {
     my ($self, $c) = @_;
     # Give repository views the current HEAD.
     if ($c->stash->{Repository}) {
         $c->stash->{HEAD} = $c->stash->{Repository}->head_hash;
     }
+    if ($c->stash->{data} && blessed $c->stash->{data}) {
+        $c->stash->{rest} = $c->stash->{data}->pack;
+    }
 }
 
 sub error_404 : Action {
@@ -672,6 +675,14 @@ sub error_404 : Action {
     $c->response->body('Page not found');
 }
 
+__PACKAGE__->config(
+    default => 'text/html',
+    map => {
+        'text/html'        => [qw/ View Default /],
+        'application/json' => [qw/ JSON /],
+    }
+);
+
 __PACKAGE__->meta->make_immutable;
 
 __END__
diff --git a/t/json_view.t b/t/json_view.t
new file mode 100644 (file)
index 0000000..026479d
--- /dev/null
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More;
+use HTTP::Request::Common;
+use FindBin qw/$Bin/;
+use JSON::Any;
+
+BEGIN {
+    $ENV{GITALIST_CONFIG} = $Bin;
+    $ENV{GITALIST_REPO_DIR} = '';
+    use_ok 'Catalyst::Test', 'Gitalist';
+}
+
+my $j = JSON::Any->new;
+
+my $res = request(GET 'http://localhost/summary?p=repo1', 'Content-Type' => 'application/json');
+is $res->code, 200;
+my $data = $j->decode($res->content);
+is ref($data), 'HASH';
+is_deeply $data, {
+          'owner' => 'Tomas Doran',
+          'is_bare' => 1,
+          '__CLASS__' => 'Gitalist::Git::Repository',
+          'last_change' => '2009-11-12T19:00:34Z',
+          'references' => {
+                            '0710a7c8ee11c73e8098d08f9384c2a839c65e4e' => [
+                                                                            'heads/branch1'
+                                                                          ],
+                            '36c6c6708b8360d7023e8a1649c45bcf9b3bd818' => [
+                                                                            'heads/master'
+                                                                          ]
+                          },
+          'name' => 'repo1',
+          'description' => 'some test repository'
+        };
+
+done_testing;
+
+