summaryrefslogtreecommitdiff
path: root/lib/Net/HTTP/Console.pm
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-06-06 16:52:32 +0200
committerfranck cuny <franck@lumberjaph.net>2010-06-06 16:52:32 +0200
commit6754c25c01c202ff788ffba6265d98b4b75f4db4 (patch)
tree8ec3b04963a14462a51479555381cdec361f5f43 /lib/Net/HTTP/Console.pm
downloadnet-http-console-6754c25c01c202ff788ffba6265d98b4b75f4db4.tar.gz
initial commit
Diffstat (limited to '')
-rw-r--r--lib/Net/HTTP/Console.pm95
1 files changed, 95 insertions, 0 deletions
diff --git a/lib/Net/HTTP/Console.pm b/lib/Net/HTTP/Console.pm
new file mode 100644
index 0000000..42d30f6
--- /dev/null
+++ b/lib/Net/HTTP/Console.pm
@@ -0,0 +1,95 @@
+package Net::HTTP::Console;
+
+use Moose;
+use Try::Tiny;
+use Method::Signatures::Simple;
+use namespace::autoclean;
+
+with qw/MooseX::Getopt/;
+
+has url => (isa => 'Str', is => 'rw');
+has format => (isa => 'Str', is => 'rw', default => 'json');
+has format_mode => (isa => 'Str', is => 'rw', default => 'content-type');
+has lib => (
+ isa => 'Str',
+ is => 'rw',
+ default => sub {
+ Class::MOP::load_class('Net::HTTP::Console::Dummy');
+ 'Net::HTTP::Console::Dummy';
+ },
+ handles => qr/.*/,
+);
+has prompt => (
+ isa => 'Str',
+ is => 'rw',
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ # FIXME
+# my $url = $self->lib->api_base_url ? $self->lib->api_base_url : $self->url;
+# my $url= "http";
+ return $self->url . '> ';
+ }
+);
+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/) {
+ my $p = "Net::HTTP::Console::Dispatcher::" . $_;
+ Class::MOP::load_class($p);
+ my $o = $p->new(application => $self);
+ push @p, $o;
+ }
+ \@p;
+ },
+);
+
+sub BUILDARGS {
+ my ($class, %args) = @_;
+ if ($args{lib}) {
+ Class::MOP::load_class($args{lib});
+ }
+ return {%args};
+}
+
+method dispatch($input) {
+ my @plugins = $self->all_plugins();
+ my $result;
+ foreach (@plugins) {
+ $result = $_->dispatch($input);
+ last if $result;
+ }
+ # if (!$result) {
+ # try {
+
+ # }
+ # catch {
+ # warn $_;
+ # print "no command found!\n" unless $result;
+ # };
+#}
+}
+
+method new_lib($http_method, $path) {
+ my $lib = $self->lib->new(
+ api_base_url => $self->url,
+ api_format => $self->format,
+ api_format_mode => $self->format_mode,
+ );
+ $lib->meta->add_net_api_method(
+ 'anonymous',
+ method => $http_method,
+ path => $path,
+ );
+ return $lib;
+}
+
+no Moose;
+
+1;