File Coverage

blib/lib/Mojo/RabbitMQ/Client/Method.pm
Criterion Covered Total %
statement 3 15 20.0
branch 0 2 0.0
condition n/a
subroutine 1 5 20.0
pod 2 2 100.0
total 6 24 25.0


line stmt bran cond sub pod time code
1             package Mojo::RabbitMQ::Client::Method;
2 5     5   36 use Mojo::Base 'Mojo::EventEmitter';
  5         15  
  5         31  
3              
4             has is_sent => 0;
5             has client => undef;
6             has channel => undef;
7             has name => undef;
8             has arguments => sub { {} };
9             has expect => undef;
10              
11             sub setup {
12 0     0 1   my $self = shift;
13 0           $self->name(shift);
14 0           $self->arguments(shift);
15 0           $self->expect(shift);
16              
17 0           return $self;
18             }
19              
20             sub deliver {
21 0     0 1   my $self = shift;
22              
23 0 0         return 0 unless $self->channel->_assert_open();
24              
25             $self->client->_write_expect(
26             $self->name => $self->arguments,
27 0     0     $self->expect => sub { $self->emit('success', @_); },
28 0     0     sub { $self->emit('error', @_); }, $self->channel->id,
  0            
29             );
30 0           $self->is_sent(1);
31              
32 0           return 1;
33             }
34              
35             1;
36              
37             =encoding utf8
38              
39             =head1 NAME
40              
41             Mojo::RabbitMQ::Client::Method - it's a generic class for all AMQP method calls
42              
43             =head1 SYNOPSIS
44              
45             use Mojo::RabbitMQ::Client::Method;
46              
47             my $method = Mojo::RabbitMQ::Client::Method->new(
48             client => $client,
49             channel => $channel
50             )->setup(
51             'Basic::Consume' => {
52             ...
53             },
54             ['Basic::ConsumeOk', ...]
55             );
56              
57             # Watch for errors
58             $method->on(error => sub { warn "Error in reception: " . $_[1] });
59              
60             # Send this frame to AMQP
61             $method->deliver;
62              
63             =head1 DESCRIPTION
64              
65             L is general class for every AMQP method call.
66              
67             =head1 EVENTS
68              
69             L inherits all events from L and can emit the
70             following new ones.
71              
72             =head2 success
73              
74             $method->on(success => sub {
75             my ($method, $frame) = @_;
76             ...
77             });
78              
79             Emitted when one of expected replies is received.
80              
81             =head2 message
82              
83             Can be emitted by consumption & get methods.
84              
85             =head2 empty
86              
87             Can be emitted by get method, when no messages are available on queue.
88              
89             =head1 ATTRIBUTES
90              
91             L has following attributes.
92              
93             =head2 is_sent
94              
95             $method->is_sent ? "Method was sent" : "Method is still pending delivery";
96              
97             =head2 client
98              
99             my $client = $method->client;
100             $method->client($client);
101              
102             =head2 name
103              
104             my $name = $method->name;
105             $method->name('Basic::Get');
106              
107             =head2 arguments
108              
109             my $arguments = $method->arguments;
110             $method->arguments({no_ack => 1, ticket => 0, queue => 'amq.queue'});
111              
112             =head2 expect
113              
114             my $expectations = $method->expect;
115             $method->expect([qw(Basic::GetOk Basic::GetEmpty)]);
116              
117             =head1 METHODS
118              
119             L inherits all methods from L and implements
120             the following new ones.
121              
122             =head2 setup
123              
124             $method = $method->setup($name, $arguments, $expectations);
125              
126             Sets AMQP method name, its arguments and expected replies.
127              
128             =head2 deliver
129              
130             my $status = $method->deliver();
131              
132             This delivers AMQP method call to server. Returns C<> when channel is not open, C<> otherwise.
133             On successful delivery->reply cycle emits C<> event.
134             C<> is emitted when none of expected replies are received.
135              
136             =head1 SEE ALSO
137              
138             L, L
139              
140             =head1 COPYRIGHT AND LICENSE
141              
142             Copyright (C) 2015-2017, Sebastian Podjasek and others
143              
144             Based on L - Copyright (C) 2010 Masahito Ikuta, maintained by C<< bobtfish@bobtfish.net >>
145              
146             This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
147              
148             =cut