summary refs log tree commit diff
path: root/lib/Net/HTTP/Console/Dispatcher
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Net/HTTP/Console/Dispatcher')
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm25
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/HTTP.pm73
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm78
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/Headers.pm49
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/Help.pm83
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/Load.pm17
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/LoadLib.pm18
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/Method.pm26
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/Set.pm45
9 files changed, 200 insertions, 214 deletions
diff --git a/lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm b/lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm
deleted file mode 100644
index 12a66e4..0000000
--- a/lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm
+++ /dev/null
@@ -1,25 +0,0 @@
-package Net::HTTP::Console::Dispatcher::ExecuteMethod;
-
-use Moose;
-with qw/Net::HTTP::Console::Dispatcher/;
-
-sub dispatch {
-    my ($self, $input) = @_;
-    (my $method, my $args) = $input =~ /^(\w+)\s(.*)$/;
-    my ($content, $response) =
-      $self->application->api_object->$method(%{JSON::decode_json($args)});
-    $self->application->_set_and_show($content, $response);
-    1;
-}
-
-sub pattern {
-    my ($self, $input) = @_;
-    (my $method) = $input =~ /^(\w+)/;
-
-    # XXX find_api_method_by_name ?
-    $self->application->api_object->meta->find_method_by_name($method)
-      ? return $input
-      : return 0;
-}
-
-1;
diff --git a/lib/Net/HTTP/Console/Dispatcher/HTTP.pm b/lib/Net/HTTP/Console/Dispatcher/HTTP.pm
new file mode 100644
index 0000000..789c04c
--- /dev/null
+++ b/lib/Net/HTTP/Console/Dispatcher/HTTP.pm
@@ -0,0 +1,73 @@
+package Net::HTTP::Console::Dispatcher::HTTP;
+
+use MooseX::Declare;
+
+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;
+    }
+
+    method dispatch($input) {
+        $self->_clean_http_lib;
+
+        my ($method, $path, $body);
+        if (($method, $path) = $input =~ /^(GET|DELETE)\s(.*)$/) {
+            $self->_do_request($method, $path);
+        }
+        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
+        }
+        return 1;
+    }
+
+
+    method _do_request($http_method, $path) {
+        $self->application->new_anonymous_method($http_method, $path);
+        try {
+            my ($content, $result) = $self->application->api_object->anonymous;
+            $self->application->_set_and_show($content, $result);
+        }catch{
+            warn $_;
+        };
+    }
+
+    method _clean_http_lib {
+        if ($self->application->api_lib eq "Net::HTTP::Console::Dummy") {
+            map { $self->application->api_lib->meta->remove_net_api_method($_) }
+              $self->application->api_lib->meta->get_all_net_api_methods();
+        }
+    }
+
+    method _do_request_with_body($http_method, $path, $body) {
+        $self->application->new_anonymous_method($http_method, $path);
+
+        # XXX clean handlers
+        $self->application->api_object->api_useragent->add_handler(
+            request_prepare => sub {
+                my $request = shift;
+                $request->header('Content-Type' => 'application/json');
+                $request->content('{"foof":"bar"}');
+            }
+        );
+        try {
+            my ($content, $result) = $self->application->api_object->anonymous;
+            $self->application->_set_and_show($content, $result);
+        }catch{
+            warn $_;
+            use YAML::Syck;
+            warn Dump $_->http_error;
+        };
+    }
+}
+
+1;
diff --git a/lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm b/lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm
deleted file mode 100644
index ec8892f..0000000
--- a/lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm
+++ /dev/null
@@ -1,78 +0,0 @@
-package Net::HTTP::Console::Dispatcher::HTTPRequest;
-
-use Moose;
-use Try::Tiny;
-
-with qw/ Net::HTTP::Console::Dispatcher /;
-
-sub _clean_http_lib {
-    my $self = shift;
-    if ($self->application->lib eq "Net::HTTP::Console::Dummy") {
-        map { $self->application->lib->meta->remove_net_api_method($_) }
-          $self->application->lib->meta->get_all_api_methods();
-    }
-}
-
-sub dispatch {
-    my ($self, $input) = @_;
-
-    $self->_clean_http_lib;
-
-    if ($input =~ /^(GET|DELETE)\s(.*)$/) {
-        $self->_do_request($1, $2);
-    }
-    elsif ($input =~ /^(POST|PUT)\s(.*)(?:\s(.*))$/) {
-        $self->_do_request_with_body($1, $2, $3);
-    }
-    elsif ($input =~ /^show\s(headers|content)$/) {
-        my $method = "_show_last_$1";
-        $self->application->$method;
-    }
-    else {
-
-        # XXX unsupporter method
-    }
-    return 1;
-}
-
-sub pattern {
-    my ($self, $input) = @_;
-    $input =~ /^(?:GET|POST|PUT|DELETE|HEAD|show)\s/ ? return $input : return 0;
-}
-
-sub _do_request {
-    my ($self, $http_method, $path) = @_;
-    $self->application->new_anonymous_method($http_method, $path);
-    try {
-        my ($content, $result) = $self->application->api_object->anonymous;
-        $self->application->_set_and_show($content, $result);
-    }catch{
-        warn $_;
-    };
-}
-
-sub _do_request_with_body {
-    my ($self, $http_method, $path, $body) = @_;
-    $self->application->new_anonymous_method($http_method, $path);
-
-    # XXX clean handlers
-    $self->application->api_object->api_useragent->add_handler(
-        request_prepare => sub {
-            my $request = shift;
-            $request->header('Content-Type' => 'application/json');
-            $request->content('{"foof":"bar"}');
-        }
-    );
-    try {
-        my ($content, $result) = $self->application->api_object->anonymous;
-        $self->application->_set_and_show($content, $result);
-    }catch{
-        warn $_;
-        use YAML::Syck;
-        warn Dump $_->http_error;
-    };
-}
-
-no Moose;
-
-1;
diff --git a/lib/Net/HTTP/Console/Dispatcher/Headers.pm b/lib/Net/HTTP/Console/Dispatcher/Headers.pm
deleted file mode 100644
index 4c29098..0000000
--- a/lib/Net/HTTP/Console/Dispatcher/Headers.pm
+++ /dev/null
@@ -1,49 +0,0 @@
-package Net::HTTP::Console::Dispatcher::Headers;
-
-use Moose;
-with qw/Net::HTTP::Console::Dispatcher/;
-
-sub dispatch {
-    my ($self, $input) = @_;
-
-    (my $command, my $header, my $value) =
-      $input =~ /^([\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();
-    }
-}
-
-sub pattern {
-    my ($self, $input) = @_;
-    $input =~ /(un)?set_header|show_defined_headers/
-      ? return $input
-      : return 0;
-}
-
-sub _unset_header {
-    my ($self, $header) = @_;
-    $self->application->delete_header($header);
-    print "header $header unset\n";
-}
-
-sub _set_header {
-    my ($self, $header, $value) = @_;
-    $self->application->set_header($header, $value);
-    print "header $header set to $value\n";
-}
-
-sub _show_defined_headers{
-    my $self = shift;
-    foreach ($self->application->all_headers) {
-        print $_->[0].": ".$_->[1]."\n";
-    }
-}
-
-1;
diff --git a/lib/Net/HTTP/Console/Dispatcher/Help.pm b/lib/Net/HTTP/Console/Dispatcher/Help.pm
index 7055b2b..a7a774b 100644
--- a/lib/Net/HTTP/Console/Dispatcher/Help.pm
+++ b/lib/Net/HTTP/Console/Dispatcher/Help.pm
@@ -1,66 +1,61 @@
 package Net::HTTP::Console::Dispatcher::Help;
 
-use Moose;
-with qw/Net::HTTP::Console::Dispatcher/;
+use MooseX::Declare;
 
-sub dispatch {
-    my ($self, $input) = @_;
+class Net::HTTP::Console::Dispatcher::Help with Net::HTTP::Console::Dispatcher {
 
-    (my $cmd, my $cmd_name) = $input =~ /^help\s(\w+)?\s?(\w+)?/;
+    method dispatch($input) {
+        (my $cmd, my $cmd_name) = $input =~ /^help\s(\w+)?\s?(\w+)?/;
 
-    if ($cmd) {
-        if ($cmd eq 'command' && $cmd_name) {
-            $self->_get_help_for_command($cmd_name);
+        if ($cmd) {
+            if ($cmd eq 'command' && $cmd_name) {
+                $self->_get_help_for_command($cmd_name);
+            }
+            elsif ($cmd eq 'command') {
+                $self->_list_commands();
+            }
         }
-        elsif ($cmd eq 'command') {
-            $self->_list_commands();
+        else {
+            $self->_display_help();
         }
+        1;
     }
-    else {
-        $self->_display_help();
-    }
-    1;
-}
 
-sub pattern {
-    my ($self, $input) = @_;
-    $input =~ /^help/ ? return $input : return 0;
-}
+    method pattern($input) {
+        $input =~ /^help/ ? return $input : return 0;
+    }
 
-sub _display_help {
-    print <<EOF
+    method _display_help {
+        print <<EOF
 help command    -  help about a command
 help request    -  help on how to write request
 EOF
-}
-
-sub _list_commands {
-    my $self = shift;
-    my @methods =
-      $self->application->api_object->meta->get_all_net_api_methods();
-
-    if (!@methods) {
-        print "no method available\n";
-        return;
     }
 
-    print "available commands:\n";
-    map { print "- " . $_ . "\n" } @methods;
-}
+      method _list_commands {
+          my @methods =
+            $self->application->api_object->meta->get_all_net_api_methods();
 
-sub _get_help_for_command {
-    my ($self, $cmd_name) = @_;
+          if (!@methods) {
+              print "no method available\n";
+              return;
+          }
 
-    my $method =
-      $self->application->api_object->meta->find_net_api_method_by_name(
-        $cmd_name);
+          print "available commands:\n";
+          map { print "- " . $_ . "\n" } @methods;
+      }
 
-    if (!$method) {
-        print "unknown method " . $cmd_name . "\n";
-        return;
-    }
+      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 $method->documentation;
+      }
 }
 
 1;
diff --git a/lib/Net/HTTP/Console/Dispatcher/Load.pm b/lib/Net/HTTP/Console/Dispatcher/Load.pm
new file mode 100644
index 0000000..3986b79
--- /dev/null
+++ b/lib/Net/HTTP/Console/Dispatcher/Load.pm
@@ -0,0 +1,17 @@
+package Net::HTTP::Console::Dispatcher::Load;
+
+use MooseX::Declare;
+
+class Net::HTTP::Console::Dispatcher::Load with Net::HTTP::Console::Dispatcher {
+
+    method dispatch($input) {
+        $self->application->load_api_lib($input);
+    }
+
+    method pattern($input) {
+        $input =~ /load\s(.*)$/ ? $1 : 0;
+    }
+
+}
+
+1;
diff --git a/lib/Net/HTTP/Console/Dispatcher/LoadLib.pm b/lib/Net/HTTP/Console/Dispatcher/LoadLib.pm
deleted file mode 100644
index d5e97cc..0000000
--- a/lib/Net/HTTP/Console/Dispatcher/LoadLib.pm
+++ /dev/null
@@ -1,18 +0,0 @@
-package Net::HTTP::Console::Dispatcher::LoadLib;
-
-use Moose;
-use namespace::autoclean;
-
-with qw/Net::HTTP::Console::Dispatcher/;
-
-sub dispatch {
-    my ($self, $input) = @_;
-    $self->application->load_api_lib($input);
-}
-
-sub pattern {
-    my ($self, $input) = @_;
-    $input =~ /load\s(.*)$/ ? $1 : 0;
-}
-
-1;
diff --git a/lib/Net/HTTP/Console/Dispatcher/Method.pm b/lib/Net/HTTP/Console/Dispatcher/Method.pm
new file mode 100644
index 0000000..0c031d3
--- /dev/null
+++ b/lib/Net/HTTP/Console/Dispatcher/Method.pm
@@ -0,0 +1,26 @@
+package Net::HTTP::Console::Dispatcher::Method;
+
+use MooseX::Declare;
+
+class Net::HTTP::Console::Dispatcher::Method with Net::HTTP::Console::Dispatcher {
+
+    method dispatch($input) {
+        (my $method, my $args) = $input =~ /^(\w+)\s(.*)$/;
+        my ($content, $response) =
+          $self->application->api_object->$method(%{JSON::decode_json($args)});
+        $self->application->_set_and_show($content, $response);
+        1;
+    }
+
+    method pattern($input) {
+        (my $method) = $input =~ /^(\w+)/;
+
+        # XXX find_api_method_by_name ?
+        $self->application->api_object->meta->find_method_by_name($method)
+          ? return $input
+            : return 0;
+    }
+
+}
+
+1;
diff --git a/lib/Net/HTTP/Console/Dispatcher/Set.pm b/lib/Net/HTTP/Console/Dispatcher/Set.pm
new file mode 100644
index 0000000..9f23447
--- /dev/null
+++ b/lib/Net/HTTP/Console/Dispatcher/Set.pm
@@ -0,0 +1,45 @@
+package Net::HTTP::Console::Dispatcher::Set;
+
+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(.*))?$/;
+
+        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();
+        }
+    }
+
+    method pattern($input) {
+        $input =~ /(un)?set_header|show_defined_headers/
+          ? return $input
+          : return 0;
+    }
+
+    method _set_header($header, $value) {
+        $self->application->set_header($header, $value);
+        print "header $header set to $value\n";
+    }
+
+    method _unset_header($header) {
+        $self->application->delete_header($header);
+        print "header $header unset\n";
+    }
+
+    method _show_defined_headers {
+        foreach ($self->application->all_headers) {
+            print $_->[0].": ".$_->[1]."\n";
+        }
+    }
+}
+
+1;