Mercury/Controller/Bus.pm

filter: Statementstmt 100% 28 / 28 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 100% 40 / 40 CC 3 SCAR 4.1
CC
3
Cov
100%
connect
2.0
_pattern
2.0
BEGIN
1.0
CRAP 1.5
« Mercury/Pattern/PubSub.pm ↑ Summary Mercury/Controller/PubSub.pm »
1 package Mercury::Controller::Bus;
2 5 our $VERSION = '0.016';
3 # ABSTRACT: A messaging pattern where all subscribers share messages
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod =head1 DESCRIPTION
8 #pod
9 #pod =head1 SEE ALSO
10 #pod
11 #pod =cut
12  
13 5 ▶use Mojo::Base 'Mojolicious::Controller';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Mercury::Pattern::Bus;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #pod =method connect
17 #pod
18 #pod Establish a WebSocket message bus to send/receive messages on the given
19 #pod C<topic>. All clients connected to the topic will receive all messages
20 #pod published on the topic.
21 #pod
22 #pod This is a shorter way of doing both C</pub/*topic> and C</sub/*topic>,
23 #pod without the hierarchical message passing.
24 #pod
25 #pod One difference is that by default a sender will not receive a message
26 #pod that they sent. To enable this behavior, pass a true value as the C<echo>
27 #pod query parameter when establishing the websocket.
28 #pod
29 #pod $ua->websocket('/bus/foo?echo=1' => sub { ... });
30 #pod
31 #pod =cut
32  
33 6 ▶sub connect {
Subroutine connect: called CC=2 SCAR=6.9
Pod documented
34 6 my ( $c ) = @_;
35  
36 6 my $topic = $c->stash( 'topic' );
37 6 my $pattern = $c->_pattern( $topic );
38 6 $pattern->add_peer( $c->tx );
39 6 ▶ if ( $c->param( 'echo' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->param("echo")) 1 5 6
40 1 ▶ $c->tx->on( message => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
41 1 my ( $tx, $msg ) = @_;
42 1 $tx->send( $msg );
43 1 } );
44 }
45  
46 6 $c->rendered( 101 );
47 };
48  
49 #pod =method post
50 #pod
51 #pod Post a new message to the given topic without subscribing or
52 #pod establishing a WebSocket connection. This allows new messages to be
53 #pod pushed by any HTTP client.
54 #pod
55 #pod =cut
56  
57 1 ▶sub post {
Subroutine post: called CC=1 SCAR=0.0
Pod documented
58 1 my ( $c ) = @_;
59 1 my $topic = $c->stash( 'topic' );
60 1 my $pattern = $c->_pattern( $topic );
61 1 $pattern->send_message( $c->req->body );
62 1 $c->render(
63 status => 200,
64 text => '',
65 );
66 }
67  
68 #=method _pattern
69 #
70 # my $pattern = $c->_pattern( $topic );
71 #
72 # Get or create the L<Mercury::Pattern::Bus> object for the given
73 # topic.
74 #
75 #=cut
76  
77 7 ▶sub _pattern {
Subroutine _pattern: called CC=2 SCAR=6.9
78 7 my ( $c, $topic ) = @_;
79 7 my $pattern = $c->mercury->pattern( Bus => $topic );
80 7 ▶ if ( !$pattern ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pattern) 3 4 7
81 3 $pattern = Mercury::Pattern::Bus->new;
82 3 $c->mercury->pattern( Bus => $topic => $pattern );
83 }
84 7 return $pattern;
85 }
86  
87 5 1;
88  
89 __END__
« Mercury/Pattern/PubSub.pm ↑ Summary Mercury/Controller/PubSub.pm »