diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/Net/HTTP/Console.pm | 1 | ||||
| -rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/HTTP.pm | 8 | ||||
| -rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/Help.pm | 49 | ||||
| -rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/Set.pm | 14 | ||||
| -rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/View.pm | 12 | ||||
| -rw-r--r-- | lib/Net/HTTP/Console/Role/API.pm | 30 | ||||
| -rw-r--r-- | lib/Net/HTTP/Console/Role/HTTP/Response.pm | 4 | ||||
| -rw-r--r-- | lib/Net/HTTP/Console/Role/Message.pm | 18 | ||||
| -rw-r--r-- | lib/Net/HTTP/Console/Role/Plugins.pm | 2 |
9 files changed, 72 insertions, 66 deletions
diff --git a/lib/Net/HTTP/Console.pm b/lib/Net/HTTP/Console.pm index eef3ba8..a1bc1dd 100644 --- a/lib/Net/HTTP/Console.pm +++ b/lib/Net/HTTP/Console.pm @@ -9,6 +9,7 @@ class Net::HTTP::Console { with 'Net::HTTP::Console::Role::Headers'; with 'Net::HTTP::Console::Role::Prompt'; with 'Net::HTTP::Console::Role::Plugins'; + with 'Net::HTTP::Console::Role::Message'; with 'Net::HTTP::Console::Role::API'; with 'Net::HTTP::Console::Role::HTTP::Response'; diff --git a/lib/Net/HTTP/Console/Dispatcher/HTTP.pm b/lib/Net/HTTP/Console/Dispatcher/HTTP.pm index c0e24ae..88f8814 100644 --- a/lib/Net/HTTP/Console/Dispatcher/HTTP.pm +++ b/lib/Net/HTTP/Console/Dispatcher/HTTP.pm @@ -21,7 +21,7 @@ class Net::HTTP::Console::Dispatcher::HTTP with Net::HTTP::Console::Dispatcher { $self->_do_request_with_body($method, $path, $body); } else { - # XXX unsupporter method + $self->application->logger('error', 'unsupported HTTP method'); } return 1; } @@ -32,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{ - # XXX error + $self->application->error('error', $_); }; } @@ -58,9 +58,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 $_; - use YAML::Syck; - warn Dump $_->http_error; + $self->application->error('error', $_); }; } } diff --git a/lib/Net/HTTP/Console/Dispatcher/Help.pm b/lib/Net/HTTP/Console/Dispatcher/Help.pm index 6218676..4493799 100644 --- a/lib/Net/HTTP/Console/Dispatcher/Help.pm +++ b/lib/Net/HTTP/Console/Dispatcher/Help.pm @@ -4,6 +4,10 @@ use MooseX::Declare; class Net::HTTP::Console::Dispatcher::Help with Net::HTTP::Console::Dispatcher { + method pattern($input) { + $input =~ /^help/ ? return $input : return 0; + } + method dispatch($input) { (my $cmd, my $cmd_name) = $input =~ /^help\s(\w+)?\s?(\w+)?/; @@ -29,10 +33,6 @@ class Net::HTTP::Console::Dispatcher::Help with Net::HTTP::Console::Dispatcher { 1; } - method pattern($input) { - $input =~ /^help/ ? return $input : return 0; - } - method _display_help { print <<EOF help command - help about a command @@ -43,31 +43,6 @@ 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(); - - 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; - } - method _help_about_view { print <<EOF view headers - show the headers of the last request @@ -91,7 +66,21 @@ EOF method _help_about_load { print <<EOF load libname - load a MooseX::Net::API library -EOF + + 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; } } diff --git a/lib/Net/HTTP/Console/Dispatcher/Set.pm b/lib/Net/HTTP/Console/Dispatcher/Set.pm index f97474b..c72b09b 100644 --- a/lib/Net/HTTP/Console/Dispatcher/Set.pm +++ b/lib/Net/HTTP/Console/Dispatcher/Set.pm @@ -13,10 +13,6 @@ class Net::HTTP::Console::Dispatcher::Set with Net::HTTP::Console::Dispatcher { }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) { @@ -25,19 +21,13 @@ class Net::HTTP::Console::Dispatcher::Set with Net::HTTP::Console::Dispatcher { method _set_header($header, $value) { $self->application->set_header($header, $value); - print "header $header set to $value\n"; + $self->application->message("header $header set to $value"); } method _unset_header($header) { $self->application->delete_header($header); - print "header $header unset\n"; + $self->application->message("header $header unset"); } - - # 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 index 9d623d7..bfc50f0 100644 --- a/lib/Net/HTTP/Console/Dispatcher/View.pm +++ b/lib/Net/HTTP/Console/Dispatcher/View.pm @@ -12,12 +12,14 @@ class Net::HTTP::Console::Dispatcher::View with Net::HTTP::Console::Dispatcher { (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') { + $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"; + $self->application->message( $_->[0] . ': ' . $_->[1]); } } } diff --git a/lib/Net/HTTP/Console/Role/API.pm b/lib/Net/HTTP/Console/Role/API.pm index c6ea1f2..cd3ba75 100644 --- a/lib/Net/HTTP/Console/Role/API.pm +++ b/lib/Net/HTTP/Console/Role/API.pm @@ -18,22 +18,30 @@ role Net::HTTP::Console::Role::API { lazy => 1, default => sub { my $self = shift; - $self->load_api_lib($self->api_lib); + $self->_load_api_lib($self->api_lib); }, ); - method load_api_lib($lib) { + method _load_api_lib($lib) { + my $api; 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 - } + $api = $lib->new(); + $api->api_base_url($self->url) if $self->has_url; + $api->api_format($self->format) if $self->has_format; + $api->api_format_mode($self->format_mode) + if $self->has_format_mode; + }catch { + $self->logger('error', "failed to load $lib: $_"); + }; + return $api if $api; + } + + method load_api_lib($lib) { + my $object = $self->_load_api_lib($lib); + $self->api_object($object); + $self->message("successfully loaded $lib"); } method new_anonymous_method ($http_method, $path) { @@ -44,7 +52,7 @@ role Net::HTTP::Console::Role::API { path => $path, ); }catch { - # XXX ERROR + $self->logger('error', "failed to add anonymous method: ".$_); } } } diff --git a/lib/Net/HTTP/Console/Role/HTTP/Response.pm b/lib/Net/HTTP/Console/Role/HTTP/Response.pm index 606c7de..38e35b2 100644 --- a/lib/Net/HTTP/Console/Role/HTTP/Response.pm +++ b/lib/Net/HTTP/Console/Role/HTTP/Response.pm @@ -24,12 +24,12 @@ role Net::HTTP::Console::Role::HTTP::Response { ); method _show_last_content { - print $self->_last_http_content; + $self->message($self->_last_http_content); } method _show_last_headers { foreach my $k (keys %{$self->_last_http_response->headers}) { - print "$k: ".$self->_last_http_response->header($k)."\n"; + $self->message("$k: ".$self->_last_http_response->header($k)); } } diff --git a/lib/Net/HTTP/Console/Role/Message.pm b/lib/Net/HTTP/Console/Role/Message.pm new file mode 100644 index 0000000..1fcac63 --- /dev/null +++ b/lib/Net/HTTP/Console/Role/Message.pm @@ -0,0 +1,18 @@ +package Net::HTTP::Console::Role::Message; + +use MooseX::Declare; + +role Net::HTTP::Console::Role::Message { + + # XXX colors ? + method logger($level, $message) { + print "[".uc($level)."] ".$message."\n"; + } + + method message($message) { + print $message."\n"; + } + +} + +1; diff --git a/lib/Net/HTTP/Console/Role/Plugins.pm b/lib/Net/HTTP/Console/Role/Plugins.pm index 2bddccd..07e1eeb 100644 --- a/lib/Net/HTTP/Console/Role/Plugins.pm +++ b/lib/Net/HTTP/Console/Role/Plugins.pm @@ -43,7 +43,7 @@ role Net::HTTP::Console::Role::Plugins { last if ($result = $_->dispatch($input)); } }catch{ - print "[ERROR]: ".$_; + $self->logger('error', $_); }; } } |
