File Coverage

blib/lib/Net/Stomp/MooseHelpers/CanSubscribe.pm
Criterion Covered Total %
statement 30 33 90.9
branch 1 2 50.0
condition 2 2 100.0
subroutine 8 11 72.7
pod 2 2 100.0
total 43 50 86.0


line stmt bran cond sub pod time code
1             package Net::Stomp::MooseHelpers::CanSubscribe;
2             $Net::Stomp::MooseHelpers::CanSubscribe::VERSION = '2.9';
3             {
4             $Net::Stomp::MooseHelpers::CanSubscribe::DIST = 'Net-Stomp-MooseHelpers';
5             }
6 1     1   1937 use Moose::Role;
  1         2  
  1         7  
7 1     1   3906 use Net::Stomp::MooseHelpers::Exceptions;
  1         2  
  1         26  
8 1         7 use Net::Stomp::MooseHelpers::Types qw(SubscriptionConfigList
9             Headers
10 1     1   4 );
  1         1  
11 1     1   2548 use Try::Tiny;
  1         2  
  1         63  
12 1     1   6 use namespace::autoclean;
  1         1  
  1         9  
13              
14             # ABSTRACT: role for classes that subscribe via Net::Stomp
15              
16              
17             has subscribe_headers => (
18             is => 'ro',
19             isa => Headers,
20             lazy => 1,
21             builder => '_default_subscribe_headers',
22             );
23 0     0   0 sub _default_subscribe_headers { { } }
24              
25              
26             has subscriptions => (
27             is => 'ro',
28             isa => SubscriptionConfigList,
29             coerce => 1,
30             lazy => 1,
31             builder => '_default_subscriptions',
32             );
33 0     0   0 sub _default_subscriptions { [] }
34              
35             requires 'connection','current_server';
36              
37              
38             sub subscribe {
39 1     1 1 21 my ($self) = @_;
40              
41 1         30 my %headers = (
42 1 50       3 %{$self->subscribe_headers},
43 1         1 %{$self->current_server->{subscribe_headers} || {}},
44             );
45              
46 1         13 my $sub_id = 0;
47              
48             try {
49 1     1   30 for my $sub (@{$self->subscriptions}) {
  1         33  
50 2         3 my $destination = $sub->{destination};
51 2   100     7 my $more_headers = $sub->{headers} || {};
52              
53 2         9 $self->subscribe_single(
54             $sub,
55             {
56             destination => $destination,
57             %headers,
58             %$more_headers,
59             id => $sub_id,
60             ack => 'client',
61             }
62             );
63              
64 2         5 ++$sub_id;
65             }
66             } catch {
67 0     0   0 Net::Stomp::MooseHelpers::Exceptions::Stomp->throw({
68             stomp_error => $_
69             });
70 1         7 };
71             }
72              
73              
74             sub subscribe_single {
75 2     2 1 3 my ($self,$subscription,$headers) = @_;
76              
77 2         58 $self->connection->subscribe($headers);
78              
79 2         12 return;
80             }
81              
82             1;
83              
84             __END__
85              
86             =pod
87              
88             =encoding UTF-8
89              
90             =head1 NAME
91              
92             Net::Stomp::MooseHelpers::CanSubscribe - role for classes that subscribe via Net::Stomp
93              
94             =head1 VERSION
95              
96             version 2.9
97              
98             =head1 SYNOPSIS
99              
100             package MyThing;
101             use Moose;
102             with 'Net::Stomp::MooseHelpers::CanConnect';
103             with 'Net::Stomp::MooseHelpers::CanSubscribe';
104             use Try::Tiny;
105              
106             sub foo {
107             my ($self) = @_;
108             $self->connect();
109             $self->subscribe();
110             do_something( $self->connection->receive_frame() );
111             }
112              
113             =head1 DESCRIPTION
114              
115             This role provides your class with a flexible way to define
116             subscriptions to a STOMP server, and to actually subscribe.
117              
118             B<NOTE>: as shown in the synopsis, you need 2 separate calls the
119             C<with>, otherwise the roles won't apply. The reason is that this role
120             requires a C<connection> attribute, that is provided by
121             L<Net::Stomp::MooseHelpers::CanConnect>, but the role dependency
122             resolution does not notice that.
123              
124             =head1 ATTRIBUTES
125              
126             =head2 C<subscribe_headers>
127              
128             Global setting for subscription headers (passed to
129             L<Net::Stomp/subscribe>). Can be overridden by the
130             C<subscribe_headers> slot in each element of L</servers> and by the
131             C<headers> slot in each element fof L</subscriptions>. Defaults to
132             the empty hashref.
133              
134             =head2 C<subscriptions>
135              
136             A
137             L<SubscriptionConfigList|Net::Stomp::MooseHelpers::Types/SubscriptionConfigList>,
138             that is, an arrayref of hashrefs, each of which describes a
139             subscription. Defaults to the empty arrayref. You should set this
140             value to something useful, otherwise your connection will not receive
141             any message.
142              
143             =head1 METHODS
144              
145             =head2 C<subscribe>
146              
147             Call L</subscribe_single> method for each element of
148             L</subscriptions>, passing the generic L</subscribe_headers>, the
149             per-server subscribe headers (from
150             L<current_server|Net::Stomp::MooseHelpers::CanConnect/current_server>,
151             slot C<subscribe_headers>) and the per-subscription subscribe headers
152             (from L</subscriptions>, slot C<headers>).
153              
154             Throws a L<Net::Stomp::MooseHelpers::Exceptions::Stomp> if anything
155             goes wrong.
156              
157             =head2 C<subscribe_single>
158              
159             $self->subscribe_single($subscription,$headers);
160              
161             Call the C<subscribe> method on L</connection>, passing the
162             C<$headers>.
163              
164             You can override or modify this method in your class if you need to
165             perform more work on each subscription.
166              
167             =head1 AUTHOR
168              
169             Gianni Ceccarelli <gianni.ceccarelli@net-a-porter.com>
170              
171             =head1 COPYRIGHT AND LICENSE
172              
173             This software is copyright (c) 2014 by Net-a-porter.com.
174              
175             This is free software; you can redistribute it and/or modify it under
176             the same terms as the Perl 5 programming language system itself.
177              
178             =cut