File Coverage

blib/lib/AnyEvent/RabbitMQ/PubSub/Publisher.pm
Criterion Covered Total %
statement 6 17 35.2
branch n/a
condition 0 5 0.0
subroutine 2 7 28.5
pod 0 2 0.0
total 8 31 25.8


line stmt bran cond sub pod time code
1             package AnyEvent::RabbitMQ::PubSub::Publisher;
2 1     1   57762 use Moose;
  1         316180  
  1         7  
3              
4 1     1   5161 use AnyEvent;
  1         1  
  1         320  
5              
6             has channel => (
7             is => 'ro', isa => 'AnyEvent::RabbitMQ::Channel', required => 1
8             );
9             has exchange => (
10             is => 'ro', isa => 'HashRef', required => 1
11             );
12             has routing_key => (
13             is => 'ro', isa => 'Str', default => '#'
14             );
15             has default_header => (
16             is => 'ro', isa => 'Maybe[HashRef]'
17             );
18              
19             sub init {
20 0     0 0   my ($self) = @_;
21              
22 0           my $cv = AnyEvent->condvar;
23              
24             $self->channel->declare_exchange(
25 0           %{ $self->exchange },
26 0     0     on_success => sub { $cv->send() },
27 0     0     on_failure => sub { $cv->croak(@_) },
28 0           );
29              
30 0           $cv->recv();
31             return
32 0           }
33              
34             sub publish {
35 0     0 0   my ($self, %options) = @_;
36              
37             $self->channel->publish(
38             exchange => $self->exchange->{exchange},
39             routing_key => $self->routing_key,
40 0     0     on_inactive => sub { die 'Failed to publish: channel inactive' },
41             %options,
42             header => $options{header} // $self->default_header,
43 0   0       body => $options{body} // '',
      0        
44             );
45             }
46              
47             1