diff options
author | franck cuny <franck@lumberjaph.net> | 2010-06-08 15:22:36 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-06-08 15:22:36 +0200 |
commit | 78fcbf54a4305e1ee6c3a105f91af40c7ea0a62a (patch) | |
tree | c4eb6cec29a92076c2f6fc47ca318cbe59f5e0ba | |
parent | update regex; modify http methods; .. (diff) | |
download | net-http-console-78fcbf54a4305e1ee6c3a105f91af40c7ea0a62a.tar.gz |
switch to MX::Declare; rename some roles and dispatcher
-rw-r--r-- | lib/Net/HTTP/Console.pm | 92 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher.pm | 27 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm | 25 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/HTTP.pm | 73 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm | 78 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/Headers.pm | 49 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/Help.pm | 83 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/Load.pm | 17 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/LoadLib.pm | 18 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/Method.pm | 26 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/Set.pm | 45 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Role/API.pm | 35 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Role/APILib.pm | 26 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Role/HTTP.pm | 41 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Role/HTTP/Response.pm | 44 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Role/Headers.pm | 31 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Role/Plugins.pm | 38 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Role/Prompt.pm | 20 |
18 files changed, 399 insertions, 369 deletions
diff --git a/lib/Net/HTTP/Console.pm b/lib/Net/HTTP/Console.pm index 486db4c..2036b47 100644 --- a/lib/Net/HTTP/Console.pm +++ b/lib/Net/HTTP/Console.pm @@ -1,70 +1,40 @@ -package Net::HTTP::Console; +package Net::HTTP::Console; # For PAUSE +our $VERSION = 0.01; -use Moose; -use Try::Tiny; -use Method::Signatures::Simple; -use namespace::autoclean; +use MooseX::Declare; -with qw/ - MooseX::Getopt - Net::HTTP::Console::Role::APILib - Net::HTTP::Console::Role::HTTP - Net::HTTP::Console::Role::Headers - /; +class Net::HTTP::Console { + use Try::Tiny; -has url => (isa => 'Str', is => 'rw', predicate => 'has_url'); -has format => (isa => 'Str', is => 'rw', predicate => 'has_format'); -has format_mode => (isa => 'Str', is => 'rw', predicate => 'has_format_mode'); -has prompt => ( - isa => 'Str', - is => 'rw', - lazy => 1, - default => sub { - my $self = shift; - my $url = $self->api_object->api_base_url; - return ($url || '[no url defined!]') . '> '; - } -); -has plugins => ( - traits => ['Array'], - is => 'rw', - isa => 'ArrayRef[Object]', - lazy => 1, - handles => {all_plugins => 'elements', add_plugin => 'push'}, - default => sub { - my $self = shift; - my @p; - for (qw/LoadLib HTTPRequest Help ExecuteMethod Headers/) { - my $p = "Net::HTTP::Console::Dispatcher::" . $_; - Class::MOP::load_class($p); - my $o = $p->new(application => $self); - push @p, $o; - } - \@p; - }, -); + with 'MooseX::Getopt'; + with 'Net::HTTP::Console::Role::Headers'; + with 'Net::HTTP::Console::Role::Prompt'; + with 'Net::HTTP::Console::Role::Plugins'; + with 'Net::HTTP::Console::Role::API'; + with 'Net::HTTP::Console::Role::HTTP::Response'; + + has url => (isa => 'Str', is => 'rw', predicate => 'has_url'); + has format => (isa => 'Str', is => 'rw', predicate => 'has_format'); + has format_mode => (isa => 'Str', is => 'rw', predicate => 'has_format_mode'); -method dispatch($input) { - my $result; - try { - foreach ($self->all_plugins) { - $result = $_->dispatch($input); - last if $result; - } + method dispatch ($input) { + my $result; + try { + foreach ($self->all_plugins) { + last if ($result = $_->dispatch($input)); + } + }catch{ + print "[ERROR]: ".$_; + }; } - catch { - print "[ERROR] : " . $_; - }; -} -method new_anonymous_method($http_method, $path) { - $self->api_object->meta->add_net_api_method( - 'anonymous', - method => $http_method, - path => $path, - ); + method new_anonymous_method ($http_method, $path) { + $self->api_object->meta->add_net_api_method( + 'anonymous', + method => $http_method, + path => $path, + ); + } } -no Moose; - 1; diff --git a/lib/Net/HTTP/Console/Dispatcher.pm b/lib/Net/HTTP/Console/Dispatcher.pm index 052e493..057f2d5 100644 --- a/lib/Net/HTTP/Console/Dispatcher.pm +++ b/lib/Net/HTTP/Console/Dispatcher.pm @@ -1,21 +1,22 @@ package Net::HTTP::Console::Dispatcher; -use Moose::Role; +use MooseX::Declare; -has application => (is => 'rw', isa => 'Net::HTTP::Console'); +role Net::HTTP::Console::Dispatcher { -requires qw/dispatch pattern/; + has application => (is => 'rw', isa => 'Net::HTTP::Console'); -around dispatch => sub { - my $orig = shift; - my $self = shift; - my $in = shift; + requires qw/dispatch pattern/; - if (my $r = $self->pattern($in)) { - return $self->$orig($r); - }else{ - return undef; - } -}; + around dispatch ($input) { + if (my $r = $self->pattern($input)) { + return $self->$orig($r); + } + else { + return undef; + } + }; + +} 1; 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; diff --git a/lib/Net/HTTP/Console/Role/API.pm b/lib/Net/HTTP/Console/Role/API.pm new file mode 100644 index 0000000..f9abc30 --- /dev/null +++ b/lib/Net/HTTP/Console/Role/API.pm @@ -0,0 +1,35 @@ +package Net::HTTP::Console::Role::API; + +use MooseX::Declare; + +role Net::HTTP::Console::Role::API { + + has api_lib => ( + isa => 'Str', + is => 'rw', + default => 'Net::HTTP::Console::Dummy' + ); + + has api_object => ( + isa => 'Object', + is => 'rw', + lazy => 1, + default => sub { + my $self = shift; + $self->load_api_lib($self->api_lib); + }, + ); + + 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; + } + +} + +1; diff --git a/lib/Net/HTTP/Console/Role/APILib.pm b/lib/Net/HTTP/Console/Role/APILib.pm deleted file mode 100644 index 5f5aff1..0000000 --- a/lib/Net/HTTP/Console/Role/APILib.pm +++ /dev/null @@ -1,26 +0,0 @@ -package Net::HTTP::Console::Role::APILib; - -use Moose::Role; - -has lib => (isa => 'Str', is => 'rw', default => 'Net::HTTP::Console::Dummy'); -has api_object => ( - isa => 'Object', - is => 'rw', - lazy => 1, - default => sub { - my $self = shift; - $self->load_api_lib($self->lib); - }, -); - -sub load_api_lib { - my ($self, $lib) = @_; - Class::MOP::load_class($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; -} - -1; diff --git a/lib/Net/HTTP/Console/Role/HTTP.pm b/lib/Net/HTTP/Console/Role/HTTP.pm deleted file mode 100644 index 292de40..0000000 --- a/lib/Net/HTTP/Console/Role/HTTP.pm +++ /dev/null @@ -1,41 +0,0 @@ -package Net::HTTP::Console::Role::HTTP; - -use Moose::Role; - -has _last_http_response => ( - is => 'rw', - isa => 'Object', - clearer => '_clear_last_http_response', -); -has _last_http_content => ( - is => 'rw', - isa => 'Str', - clearer => '_clear_last_http_content', -); -has _json => ( - is => 'rw', - isa => 'Object', - lazy => 1, - default => sub { JSON->new; }, -); - -sub _show_last_content { - my $self = shift; - print $self->_last_http_content; -} - -sub _show_last_headers { - my $self = shift; - foreach my $k (keys %{$self->_last_http_response->headers}) { - print "$k: ".$self->_last_http_response->header($k)."\n"; - } -} - -sub _set_and_show { - my ($self, $content, $response) = @_; - $self->_last_http_content($self->_json->pretty->encode($content)); - $self->_last_http_response($response); - $self->_show_last_content; -} - -1; diff --git a/lib/Net/HTTP/Console/Role/HTTP/Response.pm b/lib/Net/HTTP/Console/Role/HTTP/Response.pm new file mode 100644 index 0000000..606c7de --- /dev/null +++ b/lib/Net/HTTP/Console/Role/HTTP/Response.pm @@ -0,0 +1,44 @@ +package Net::HTTP::Console::Role::HTTP::Response; + +use MooseX::Declare; + +role Net::HTTP::Console::Role::HTTP::Response { + + has _last_http_response => ( + is => 'rw', + isa => 'Object', + clearer => '_clear_last_http_response', + ); + + has _last_http_content => ( + is => 'rw', + isa => 'Str', + clearer => '_clear_last_http_content', + ); + + has _json => ( + is => 'rw', + isa => 'Object', + lazy => 1, + default => sub { JSON->new; }, + ); + + method _show_last_content { + print $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"; + } + } + + method _set_and_show($content, $response) { + $self->_last_http_content($self->_json->pretty->encode($content)); + $self->_last_http_response($response); + $self->_show_last_content; + } + +} + +1; diff --git a/lib/Net/HTTP/Console/Role/Headers.pm b/lib/Net/HTTP/Console/Role/Headers.pm index d694d85..4d79a3b 100644 --- a/lib/Net/HTTP/Console/Role/Headers.pm +++ b/lib/Net/HTTP/Console/Role/Headers.pm @@ -1,19 +1,22 @@ package Net::HTTP::Console::Role::Headers; -use Moose::Role; +use MooseX::Declare; -has custom_headers => ( - traits => ['Hash'], - is => 'ro', - isa => 'HashRef[Str]', - default => sub { {} }, - handles => { - set_header => 'set', - get_header => 'get', - has_no_headers => 'is_empty', - delete_header => 'delete', - all_headers => 'kv', - }, -); +role Net::HTTP::Console::Role::Headers { + + has custom_headers => ( + traits => ['Hash'], + is => 'ro', + isa => 'HashRef[Str]', + default => sub { {} }, + handles => { + set_header => 'set', + get_header => 'get', + has_no_headers => 'is_empty', + delete_header => 'delete', + all_headers => 'kv', + }, + ); +} 1; diff --git a/lib/Net/HTTP/Console/Role/Plugins.pm b/lib/Net/HTTP/Console/Role/Plugins.pm new file mode 100644 index 0000000..a3136f2 --- /dev/null +++ b/lib/Net/HTTP/Console/Role/Plugins.pm @@ -0,0 +1,38 @@ +package Net::HTTP::Console::Role::Plugins; + +use MooseX::Declare; + +role Net::HTTP::Console::Role::Plugins { + + has dispatchers => ( + is => 'rw', + isa => 'ArrayRef[Str]', + traits => ['Array'], + lazy => 1, + auto_deref => 1, + default => sub { + [qw/Load HTTP Help Method Set/], + } + ); + + has plugins => ( + traits => ['Array'], + is => 'rw', + isa => 'ArrayRef[Object]', + lazy => 1, + handles => {all_plugins => 'elements', add_plugin => 'push'}, + default => sub { + my $self = shift; + my @p; + for ($self->dispatchers) { + my $p = "Net::HTTP::Console::Dispatcher::" . $_; + Class::MOP::load_class($p); + my $o = $p->new(application => $self); + push @p, $o; + } + \@p; + }, + ); +} + +1; diff --git a/lib/Net/HTTP/Console/Role/Prompt.pm b/lib/Net/HTTP/Console/Role/Prompt.pm new file mode 100644 index 0000000..b6a9599 --- /dev/null +++ b/lib/Net/HTTP/Console/Role/Prompt.pm @@ -0,0 +1,20 @@ +package Net::HTTP::Console::Role::Prompt; + +use MooseX::Declare; + +role Net::HTTP::Console::Role::Prompt { + + has prompt => ( + isa => 'Str', + is => 'rw', + lazy => 1, + default => sub { + my $self = shift; + my $url = $self->api_object->api_base_url; + return ($url || '[no url defined!]') . '> '; + } + ); + +} + +1; |