summary refs log tree commit diff
path: root/lib/Net/HTTP/Console.pm
blob: 486db4c0648ebb17e78f6545dfb8256404b2732b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package Net::HTTP::Console;

use Moose;
use Try::Tiny;
use Method::Signatures::Simple;
use namespace::autoclean;

with qw/
  MooseX::Getopt
  Net::HTTP::Console::Role::APILib
  Net::HTTP::Console::Role::HTTP
  Net::HTTP::Console::Role::Headers
  /;

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;
    },
);

method dispatch($input) {
    my $result;
    try {
        foreach ($self->all_plugins) {
            $result = $_->dispatch($input);
            last if $result;
        }
    }
    catch {
        print "[ERROR] : " . $_;
    };
}

method new_anonymous_method($http_method, $path) {
    $self->api_object->meta->add_net_api_method(
        'anonymous',
        method => $http_method,
        path   => $path,
    );
}

no Moose;

1;