Browse files

add dispatcher for viewing content; update help

  • Loading branch information...
1 parent e71a441 commit 2bb154b2b3bbe819c98a01d9d43f2183e6389401 franck cuny committed Jun 8, 2010
View
8 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
};
}
View
75 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;
View
34 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;
View
28 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;
View
34 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
+ }
}
}
View
4 lib/Net/HTTP/Console/Role/Plugins.pm
@@ -5,15 +5,15 @@ use MooseX::Declare;
role Net::HTTP::Console::Role::Plugins {
use Try::Tiny;
-
+
has dispatchers => (
is => 'rw',
isa => 'ArrayRef[Str]',
traits => ['Array'],
lazy => 1,
auto_deref => 1,
default => sub {
- [qw/Load HTTP Help Method Set/],
+ [qw/Load HTTP Help Method Set View/],
}
);

0 comments on commit 2bb154b

Please sign in to comment.