Mercury/Controller/PubSub.pm

filter: Statementstmt 81.4% 22 / 27 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 5 / 6 Podpod 100% 3 / 3 totaltotal 84.2% 32 / 38 CC 2 SCAR 3.6
CC
2
Cov
83%
post
2.0
_pattern
2.0
BEGIN
1.0
CRAP 1.4
« Mercury/Controller/Bus.pm ↑ Summary Mercury/Controller/PubSub/Cascade.pm »
1 package Mercury::Controller::PubSub;
2 6 our $VERSION = '0.016';
3 # ABSTRACT: Pub/sub message pattern controller
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod # myapp.pl
8 #pod use Mojolicious::Lite;
9 #pod plugin 'Mercury';
10 #pod websocket( '/pub/*topic' )
11 #pod ->to( controller => 'PubSub', action => 'pub' );
12 #pod websocket( '/sub/*topic' )
13 #pod ->to( controller => 'PubSub', action => 'sub' );
14 #pod
15 #pod =head1 DESCRIPTION
16 #pod
17 #pod This controller enables a L<pubE<sol>sub pattern|Mercury::Pattern::PubSub> on
18 #pod a pair of endpoints (L<publish|/publish> and L<subscribe|/subscribe>.
19 #pod
20 #pod For more information on the pub/sub pattern, see L<Mercury::Pattern::PubSub>.
21 #pod
22 #pod =head1 SEE ALSO
23 #pod
24 #pod =over
25 #pod
26 #pod =item L<Mercury::Pattern::PubSub>
27 #pod
28 #pod =item L<Mercury::Controller::PubSub::Cascade>
29 #pod
30 #pod =item L<Mercury>
31 #pod
32 #pod =back
33 #pod
34 #pod =cut
35  
36 6 ▶use Mojo::Base 'Mojolicious::Controller';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 6 ▶use Mercury::Pattern::PubSub;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 #pod =method publish
40 #pod
41 #pod $app->routes->websocket( '/pub/*topic' )
42 #pod ->to( controller => 'PubSub', action => 'publish' );
43 #pod
44 #pod Controller action to connect a websocket as a publisher. A publish
45 #pod client sends messages through the socket. The message will be sent to
46 #pod all of the connected subscribers.
47 #pod
48 #pod This endpoint requires a C<topic> in the stash.
49 #pod
50 #pod =cut
51  
52 1 ▶sub publish {
Subroutine publish: called CC=1 SCAR=0.0
Pod documented
53 1 my ( $c ) = @_;
54 1 my $pattern = $c->_pattern( $c->stash( 'topic' ) );
55 1 $pattern->add_publisher( $c->tx );
56 1 $c->rendered( 101 );
57 }
58  
59 #pod =method subscribe
60 #pod
61 #pod $app->routes->websocket( '/sub/*topic' )
62 #pod ->to( controller => 'PubSub', action => 'subscribe' );
63 #pod
64 #pod Controller action to connect a websocket as a subscriber. A subscriber
65 #pod will recieve every message sent by publishers.
66 #pod
67 #pod This endpoint requires a C<topic> in the stash.
68 #pod
69 #pod =cut
70  
71 2 ▶sub subscribe {
Subroutine subscribe: called CC=1 SCAR=0.0
Pod documented
72 2 my ( $c ) = @_;
73 2 my $pattern = $c->_pattern( $c->stash( 'topic' ) );
74 2 $pattern->add_subscriber( $c->tx );
75 2 $c->rendered( 101 );
76 }
77  
78 #pod =method post
79 #pod
80 #pod Post a new message to the given topic without subscribing or
81 #pod establishing a WebSocket connection. This allows new messages to be
82 #pod pushed by any HTTP client.
83 #pod
84 #pod =cut
85  
86 0 ▶sub post {
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
87 0 my ( $c ) = @_;
88 0 my $topic = $c->stash( 'topic' );
89 0 my $pattern = $c->_pattern( $topic );
90 0 $pattern->send_message( $c->req->body );
91 0 $c->render(
92 status => 200,
93 text => '',
94 );
95 }
96  
97 #=method _pattern
98 #
99 # my $pattern = $c->_pattern( $topic );
100 #
101 # Get or create the L<Mercury::Pattern::PubSub> object for the given
102 # topic.
103 #
104 #=cut
105  
106 3 ▶sub _pattern {
Subroutine _pattern: called CC=2 SCAR=6.9
107 3 my ( $c, $topic ) = @_;
108 3 my $pattern = $c->mercury->pattern( PubSub => $topic );
109 3 ▶ if ( !$pattern ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pattern) 1 2 3
110 1 $pattern = Mercury::Pattern::PubSub->new;
111 1 $c->mercury->pattern( PubSub => $topic => $pattern );
112 }
113 3 return $pattern;
114 }
115  
116 6 1;
117  
118 __END__
« Mercury/Controller/Bus.pm ↑ Summary Mercury/Controller/PubSub/Cascade.pm »