summary refs log tree commit diff
path: root/lib/Net/HTTP/Console/Role
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-06-08 15:22:36 +0200
committerfranck cuny <franck@lumberjaph.net>2010-06-08 15:22:36 +0200
commit78fcbf54a4305e1ee6c3a105f91af40c7ea0a62a (patch)
treec4eb6cec29a92076c2f6fc47ca318cbe59f5e0ba /lib/Net/HTTP/Console/Role
parentupdate regex; modify http methods; .. (diff)
downloadnet-http-console-78fcbf54a4305e1ee6c3a105f91af40c7ea0a62a.tar.gz
switch to MX::Declare; rename some roles and dispatcher
Diffstat (limited to 'lib/Net/HTTP/Console/Role')
-rw-r--r--lib/Net/HTTP/Console/Role/API.pm35
-rw-r--r--lib/Net/HTTP/Console/Role/APILib.pm26
-rw-r--r--lib/Net/HTTP/Console/Role/HTTP.pm41
-rw-r--r--lib/Net/HTTP/Console/Role/HTTP/Response.pm44
-rw-r--r--lib/Net/HTTP/Console/Role/Headers.pm31
-rw-r--r--lib/Net/HTTP/Console/Role/Plugins.pm38
-rw-r--r--lib/Net/HTTP/Console/Role/Prompt.pm20
7 files changed, 154 insertions, 81 deletions
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;