[Bps-public-commit] rt-extension-rest2 branch, master, updated. 0599547f03fb61ab020429b254235ee1d7cb0a89

Shawn Moore shawn at bestpractical.com
Thu Dec 15 16:39:08 EST 2016


The branch, master has been updated
       via  0599547f03fb61ab020429b254235ee1d7cb0a89 (commit)
       via  63c9826692a3509d4d68e429e92dd56161b8d175 (commit)
      from  14a7444bac90d7aa00897d52b65f66f22da912f4 (commit)

Summary of changes:
 lib/RT/Extension/REST2/Dispatcher.pm    | 18 +++++++++---------
 lib/RT/Extension/REST2/Resource/Root.pm | 23 +++++++++++++++++++++++
 t/lib/RT/Extension/REST2/Test.pm.in     | 28 +++++++++++++++-------------
 t/root.t                                |  4 ++--
 t/tickets.t                             |  2 +-
 5 files changed, 50 insertions(+), 25 deletions(-)
 create mode 100644 lib/RT/Extension/REST2/Resource/Root.pm

- Log -----------------------------------------------------------------
commit 63c9826692a3509d4d68e429e92dd56161b8d175
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Thu Dec 15 20:46:35 2016 +0000

    Replace get_hypermedia with url_for_hypermedia
    
    That way it can be reused with get, post, put_json, etc etc

diff --git a/t/lib/RT/Extension/REST2/Test.pm.in b/t/lib/RT/Extension/REST2/Test.pm.in
index 030143b..8ba0598 100644
--- a/t/lib/RT/Extension/REST2/Test.pm.in
+++ b/t/lib/RT/Extension/REST2/Test.pm.in
@@ -54,6 +54,21 @@ sub mech { RT::Extension::REST2::Test::Mechanize->new }
         return $class->SUPER::new(%args);
     }
 
+    sub hypermedia_ref {
+        my ($self, $ref) = @_;
+
+        my $json = $self->json_response;
+        my @matches = grep { $_->{ref} eq $ref } @{ $json->{_hyperlinks} };
+        Test::More::is(@matches, 1, "got one match for hypermedia with ref '$ref'") or return;
+        return $matches[0];
+
+    }
+
+    sub url_for_hypermedia {
+        my ($self, $ref) = @_;
+        return $self->hypermedia_ref($ref)->{_url};
+    }
+
     sub post_json {
         my ($self, $url, $payload, %headers) = @_;
         $self->post(
@@ -86,19 +101,6 @@ sub mech { RT::Extension::REST2::Test::Mechanize->new }
 
         return $json->decode($res->content);
     }
-
-    sub get_hypermedia {
-        my $self = shift;
-        my $ref = shift;
-        my %args = @_;
-
-        my $json = $self->json_response;
-        my @matches = grep { $_->{ref} eq $ref } @{ $json->{_hyperlinks} };
-        Test::More::is(@matches, 1, "got one match for hypermedia with ref '$ref'") or return;
-
-        my $url = $matches[0]{_url};
-        return $self->get($url, %args);
-    }
 }
 
 1;
diff --git a/t/tickets.t b/t/tickets.t
index 2cb3d4a..7c9047d 100644
--- a/t/tickets.t
+++ b/t/tickets.t
@@ -186,7 +186,7 @@ my ($ticket_url, $ticket_id);
     );
     is($res->code, 200);
 
-    $res = $mech->get_hypermedia('history',
+    $res = $mech->get($mech->url_for_hypermedia('history'),
         'Authorization' => $auth,
     );
     is($res->code, 200);

commit 0599547f03fb61ab020429b254235ee1d7cb0a89
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Thu Dec 15 21:34:32 2016 +0000

    Factor / handler into its own resource

diff --git a/lib/RT/Extension/REST2/Dispatcher.pm b/lib/RT/Extension/REST2/Dispatcher.pm
index 9c5eaad..c51ace4 100644
--- a/lib/RT/Extension/REST2/Dispatcher.pm
+++ b/lib/RT/Extension/REST2/Dispatcher.pm
@@ -4,11 +4,10 @@ use strict;
 use warnings;
 use Web::Simple;
 use Web::Machine;
-use RT::Extension::REST2::PodViewer 'podview_as_html';
 use Web::Dispatch::HTTPMethods;
 
 sub dispatch_request {
-    my ($self) = @_;
+    my ($self, $env) = @_;
     sub (/**) {
         my ($resource_name) = ucfirst(lc $_[1]) =~ /([^\/]+)\/?/;
         my $resource = "RT::Extension::REST2::Resource::${resource_name}";
@@ -22,13 +21,14 @@ sub dispatch_request {
         }
     },
     sub () {
-        my $main = [
-            200,
-            ['Content-Type' => 'text/html; charset=utf-8'],
-            [ podview_as_html('RT::Extension::REST2') ]
-        ];
-        sub (~) { GET { $main } },
-        sub (/) { GET { $main } },
+        my $resource = "RT::Extension::REST2::Resource::Root";
+        $resource->require;
+        my $root = Web::Machine->new(
+            resource => $resource,
+        )->to_app;
+
+        sub (~) { GET { $root->($env) } },
+        sub (/) { GET { $root->($env) } },
     }
 }
 
diff --git a/lib/RT/Extension/REST2/Resource/Root.pm b/lib/RT/Extension/REST2/Resource/Root.pm
new file mode 100644
index 0000000..163d7c6
--- /dev/null
+++ b/lib/RT/Extension/REST2/Resource/Root.pm
@@ -0,0 +1,23 @@
+package RT::Extension::REST2::Resource::Root;
+use strict;
+use warnings;
+
+use Moose;
+use namespace::autoclean;
+use RT::Extension::REST2::PodViewer 'podview_as_html';
+
+extends 'RT::Extension::REST2::Resource';
+
+sub content_types_provided { [{ 'text/html' => 'to_html' }] }
+sub charsets_provided      { [ 'utf-8' ] }
+sub default_charset        {   'utf-8'   }
+sub allowed_methods        { ['GET', 'HEAD', 'OPTIONS'] }
+
+sub to_html {
+    return podview_as_html('RT::Extension::REST2') 
+}
+
+__PACKAGE__->meta->make_immutable;
+
+1;
+
diff --git a/t/root.t b/t/root.t
index ebb43fe..3f66799 100644
--- a/t/root.t
+++ b/t/root.t
@@ -22,7 +22,7 @@ my $auth = RT::Extension::REST2::Test->authorization_header;
     for my $path ($rest_base_path, "$rest_base_path/") {
         my $res = $mech->get($path, 'Authorization' => $auth);
         is($res->code, 200);
-        is($res->header('content-type'), 'text/html; charset=utf-8');
+        is($res->header('content-type'), 'text/html; charset="utf-8"');
 
         # this is a temp solution as for main doc
         # TODO: write an end user aimed documentation
@@ -33,7 +33,7 @@ my $auth = RT::Extension::REST2::Test->authorization_header;
 
         $res = $mech->head($path, 'Authorization' => $auth);
         is($res->code, 200);
-        is($res->header('content-type'), 'text/html; charset=utf-8');
+        is($res->header('content-type'), 'text/html; charset="utf-8"');
     }
 }
 

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list