summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-06-08 17:30:55 +0200
committerfranck cuny <franck@lumberjaph.net>2010-06-08 17:33:00 +0200
commit2bb154b2b3bbe819c98a01d9d43f2183e6389401 (patch)
tree1a304667863e5feb12c543dfbb72a579e88901c9
parentmove code to appropriate role (diff)
downloadnet-http-console-2bb154b2b3bbe819c98a01d9d43f2183e6389401.tar.gz
add dispatcher for viewing content; update help
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/HTTP.pm8
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/Help.pm75
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/Set.pm34
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/View.pm28
-rw-r--r--lib/Net/HTTP/Console/Role/API.pm34
-rw-r--r--lib/Net/HTTP/Console/Role/Plugins.pm4
6 files changed, 126 insertions, 57 deletions
diff --git a/lib/Net/HTTP/Console/Dispatcher/HTTP.pm b/lib/Net/HTTP/Console/Dispatcher/HTTP.pm
index 5dc5719..c0e24ae 100644
--- a/lib/Net/HTTP/Console/Dispatcher/HTTP.pm
+++ b/lib/Net/HTTP/Console/Dispatcher/HTTP.pm
@@ -7,7 +7,7 @@ class Net::HTTP::Console::Dispatcher::HTTP with Net::HTTP::Console::Dispatcher {
     use Try::Tiny;
 
     method pattern($input) {
-        $input =~ /^(?:GET|POST|PUT|DELETE|HEAD|show)\s/ ? return $input : return 0;
+        $input =~ /^(?:GET|POST|PUT|DELETE|HEAD)\s/ ? return $input : return 0;
     }
 
     method dispatch($input) {
@@ -20,10 +20,6 @@ class Net::HTTP::Console::Dispatcher::HTTP with Net::HTTP::Console::Dispatcher {
         elsif (($method, $path, $body) = $input =~ /^(POST|PUT)\s(.*)(?:\s(.*))$/) {
             $self->_do_request_with_body($method, $path, $body);
         }
-        elsif ($input =~ /^show\s(headers|content)$/) {
-            my $method = "_show_last_$1";
-            $self->application->$method;
-        }
         else {
             # XXX unsupporter method
         }
@@ -36,7 +32,7 @@ class Net::HTTP::Console::Dispatcher::HTTP with Net::HTTP::Console::Dispatcher {
             my ($content, $result) = $self->application->api_object->anonymous;
             $self->application->_set_and_show($content, $result);
         }catch{
-            warn $_;
+            # XXX error
         };
     }
 
diff --git a/lib/Net/HTTP/Console/Dispatcher/Help.pm b/lib/Net/HTTP/Console/Dispatcher/Help.pm
index a7a774b..6218676 100644
--- a/lib/Net/HTTP/Console/Dispatcher/Help.pm
+++ b/lib/Net/HTTP/Console/Dispatcher/Help.pm
@@ -13,6 +13,14 @@ class Net::HTTP::Console::Dispatcher::Help with Net::HTTP::Console::Dispatcher {
             }
             elsif ($cmd eq 'command') {
                 $self->_list_commands();
+            }elsif($cmd eq 'view') {
+                $self->_help_about_view();
+            }elsif($cmd eq 'set') {
+                $self->_help_about_set();
+            }elsif($cmd eq 'request') {
+                $self->_help_about_request();
+            }elsif($cmd eq 'load') {
+                $self->_help_about_load();
             }
         }
         else {
@@ -29,33 +37,62 @@ class Net::HTTP::Console::Dispatcher::Help with Net::HTTP::Console::Dispatcher {
         print <<EOF
 help command    -  help about a command
 help request    -  help on how to write request
+help set        -  help on how to set values
+help view       -  help on how to view values
+help load       -  help on how to load a lib
 EOF
     }
 
-      method _list_commands {
-          my @methods =
-            $self->application->api_object->meta->get_all_net_api_methods();
+    method _list_commands {
+        my @methods =
+          $self->application->api_object->meta->get_all_net_api_methods();
 
-          if (!@methods) {
-              print "no method available\n";
-              return;
-          }
+        if (!@methods) {
+            print "no method available\n";
+            return;
+        }
+
+        print "available commands:\n";
+        map { print "- " . $_ . "\n" } @methods;
+    }
+
+    method _get_help_for_command($cmd_name) {
+        my $method =
+          $self->application->api_object->meta->find_net_api_method_by_name($cmd_name);
+
+        if (!$method) {
+            print "unknown method " . $cmd_name . "\n";
+            return;
+        }
+
+        print $method->documentation;
+    }
 
-          print "available commands:\n";
-          map { print "- " . $_ . "\n" } @methods;
-      }
+    method _help_about_view {
+        print <<EOF
+view headers         -  show the headers of the last request
+view content         -  show the last content
+view defined_headers -  show the defined headers
+EOF
+    }
 
-      method _get_help_for_command($cmd_name) {
-          my $method =
-            $self->application->api_object->meta->find_net_api_method_by_name($cmd_name);
+    method _help_about_set {
+        print <<EOF
+set header key value   -  set the value for a header (global)
+EOF
+    }
 
-          if (!$method) {
-              print "unknown method " . $cmd_name . "\n";
-              return;
-          }
+    method _help_about_request {
+        print <<EOF
+HTTP Method path    -  make a HTTP request on a path
+EOF
+    }
 
-          print $method->documentation;
-      }
+    method _help_about_load {
+        print <<EOF
+load libname    -  load a MooseX::Net::API library
+EOF
+    }
 }
 
 1;
diff --git a/lib/Net/HTTP/Console/Dispatcher/Set.pm b/lib/Net/HTTP/Console/Dispatcher/Set.pm
index 9f23447..f97474b 100644
--- a/lib/Net/HTTP/Console/Dispatcher/Set.pm
+++ b/lib/Net/HTTP/Console/Dispatcher/Set.pm
@@ -5,24 +5,22 @@ use MooseX::Declare;
 class Net::HTTP::Console::Dispatcher::Set with Net::HTTP::Console::Dispatcher {
 
     method dispatch($input) {
-        (my $command, my $header, my $value) =
-          $input =~ /^([\w_]+)(?:\s([\w-]+))?(?:\s(.*))?$/;
+        (my $command, my $type, my $key, my $value) =
+          $input =~ /^([\w_]+)(?:\s([\w_]+))(?:\s([\w_]+))(?:\s(.*))?$/;
 
-        if ($command eq 'unset_header') {
-            $self->_unset_header($header);
-        }
-        elsif ($command eq 'set_header') {
-            $self->_set_header($header, $value);
-        }
-        elsif ($command eq 'show_defined_headers') {
-            $self->_show_defined_headers();
+        if ($command eq 'set') {
+            $self->_set_header($key, $value) if $type eq 'header';
+        }elsif($command eq 'unset') {
+            $self->_unset_header($key) if $type eq 'header';
         }
+
+        # elsif ($command eq 'show_defined_headers') {
+        #     $self->_show_defined_headers();
+        # }
     }
 
     method pattern($input) {
-        $input =~ /(un)?set_header|show_defined_headers/
-          ? return $input
-          : return 0;
+        $input =~ /^(un)?set/ ? return $input : return 0;
     }
 
     method _set_header($header, $value) {
@@ -35,11 +33,11 @@ class Net::HTTP::Console::Dispatcher::Set with Net::HTTP::Console::Dispatcher {
         print "header $header unset\n";
     }
 
-    method _show_defined_headers {
-        foreach ($self->application->all_headers) {
-            print $_->[0].": ".$_->[1]."\n";
-        }
-    }
+    # method _show_defined_headers {
+    #     foreach ($self->application->all_headers) {
+    #         print $_->[0].": ".$_->[1]."\n";
+    #     }
+    # }
 }
 
 1;
diff --git a/lib/Net/HTTP/Console/Dispatcher/View.pm b/lib/Net/HTTP/Console/Dispatcher/View.pm
new file mode 100644
index 0000000..059fe53
--- /dev/null
+++ b/lib/Net/HTTP/Console/Dispatcher/View.pm
@@ -0,0 +1,28 @@
+package Net::HTTP::Console::Dispatcher::View;
+
+use MooseX::Declare;
+
+class Net::HTTP::Console::Dispatcher::View with Net::HTTP::Console::Dispatcher {
+
+    method pattern ($input) {
+        $input =~ /^show/ ? return $input : return 0;
+    }
+
+    method dispatch ($input) {
+        (my $key) = $input =~ /^show ([\w]+)/;
+
+        if ($key eq 'headers') {
+            $self->application->_show_last_headers;
+        }
+        elsif ($key eq 'content') {
+            $self->application->_show_last_content;
+        }
+        elsif ($key eq 'defined_headers') {
+            foreach ($self->application->all_headers) {
+                print $_->[0] . ': ' . $_->[1] . "\n";
+            }
+        }
+    }
+}
+
+1;
diff --git a/lib/Net/HTTP/Console/Role/API.pm b/lib/Net/HTTP/Console/Role/API.pm
index 0b99796..c6ea1f2 100644
--- a/lib/Net/HTTP/Console/Role/API.pm
+++ b/lib/Net/HTTP/Console/Role/API.pm
@@ -4,6 +4,8 @@ use MooseX::Declare;
 
 role Net::HTTP::Console::Role::API {
 
+    use Try::Tiny;
+
     has api_lib => (
         isa     => 'Str',
         is      => 'rw',
@@ -21,21 +23,29 @@ role Net::HTTP::Console::Role::API {
     );
 
     method load_api_lib($lib) {
-        Class::MOP::load_class($lib);
-        $self->api_lib($lib);
-        my $o = $lib->new();
-        $o->api_base_url($self->url)            if $self->has_url;
-        $o->api_format($self->format)           if $self->has_format;
-        $o->api_format_mode($self->format_mode) if $self->has_format_mode;
-        $o;
+        try {
+            Class::MOP::load_class($lib);
+            $self->api_lib($lib);
+            my $o = $lib->new();
+            $o->api_base_url($self->url)            if $self->has_url;
+            $o->api_format($self->format)           if $self->has_format;
+            $o->api_format_mode($self->format_mode) if $self->has_format_mode;
+            $o;
+        }catch{
+            # XXX ERROR
+        }
     }
 
     method new_anonymous_method ($http_method, $path) {
-        $self->api_object->meta->add_net_api_method(
-            'anonymous',
-            method => $http_method,
-            path   => $path,
-        );
+        try {
+            $self->api_object->meta->add_net_api_method(
+                'anonymous',
+                method => $http_method,
+                path   => $path,
+            );
+        }catch {
+            # XXX ERROR
+        }
     }
 }
 
diff --git a/lib/Net/HTTP/Console/Role/Plugins.pm b/lib/Net/HTTP/Console/Role/Plugins.pm
index d7bf7b5..2bddccd 100644
--- a/lib/Net/HTTP/Console/Role/Plugins.pm
+++ b/lib/Net/HTTP/Console/Role/Plugins.pm
@@ -5,7 +5,7 @@ use MooseX::Declare;
 role Net::HTTP::Console::Role::Plugins {
 
     use Try::Tiny;
-    
+
     has dispatchers => (
         is         => 'rw',
         isa        => 'ArrayRef[Str]',
@@ -13,7 +13,7 @@ role Net::HTTP::Console::Role::Plugins {
         lazy       => 1,
         auto_deref => 1,
         default    => sub {
-            [qw/Load HTTP Help Method Set/],
+            [qw/Load HTTP Help Method Set View/],
         }
     );