Skip to content

Commit

Permalink
multiple clients
Browse files Browse the repository at this point in the history
Signed-off-by: franck cuny <[email protected]>
  • Loading branch information
fcuny committed Jul 26, 2011
1 parent 64b9434 commit 36f54fd
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions lib/Net/HTTP/Spore/Role.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,42 @@ package Net::HTTP::Spore::Role;
use MooseX::Role::Parameterized;
use Net::HTTP::Spore;

parameter name => (isa => 'Str', required => 1);
parameter config => (isa => 'Str', required => 1);
parameter spore_clients => (isa => 'ArrayRef[HashRef]', required => 1);

role {
my $p = shift;
my $name = $p->name;
my $config = $p->config;

has $name => (
is => 'rw',
isa => 'Object',
lazy => 1,
default => sub {
my $self = shift;
my $client_config = $self->$config;
my $client = Net::HTTP::Spore->new_from_spec(
$client_config->{spec},
%{ $client_config->{options} },
);
foreach my $mw ( @{ $client_config->{middlewares} } ) {
$client->enable($mw);
}
$client;
},
);

has $config => (
is => 'rw',
isa => 'HashRef',
lazy => 1,
default => sub { {} },
);
my $p = shift;
my $clients = $p->spore_clients;

foreach my $client (@$clients) {
my $name = $client->{name};
my $config = $client->{config};

has $name => (
is => 'rw',
isa => 'Object',
lazy => 1,
default => sub {
my $self = shift;
my $client_config = $self->$config;
my $client = Net::HTTP::Spore->new_from_spec(
$client_config->{spec},
%{ $client_config->{options} },
);
foreach my $mw ( @{ $client_config->{middlewares} } ) {
my %options = %{$mw->{options} || {}};
$client->enable( $mw->{name}, %options);
}
$client;
},
);

has $config => (
is => 'rw',
isa => 'HashRef',
lazy => 1,
default => sub { {} },
);
}
};

1;
Expand Down

0 comments on commit 36f54fd

Please sign in to comment.