summary refs log tree commit diff
path: root/lib/Net/HTTP/Spore/Role.pm
blob: fae079d056e3d1773701d9ce1fb63425d560d930 (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
package Net::HTTP::Spore::Role;

use MooseX::Role::Parameterized;
use Net::HTTP::Spore;

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

role {
    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;

=head1 NAME

Net::HTTP::Spore::Role

=head1 DESCRIPTION

=head1 SYNOPSIS

  package my::app;
  use Moose;
  with Net::HTTP::Spore::Role => {name => 'twitter', config => 'twitter_config'};

  ...

  my $app = my::app->new(twitter_config => $config->{spore}->{twitter_config});