lib/Message/Passing/Output/PocketIO.pm

filter: Statementstmt 87.9% 73 / 83 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.1% 23 / 27 Podpod 50.0% 1 / 2 totaltotal 85.9% 98 / 114 CC 2 SCAR 2.1
CC
2
Cov
87%
consume
2.0
__ANON__
2.0
__ANON__
2.0
CRAP 1.2
↑ Summary
1 =head1 NAME
2  
3 Message::Passing::Output::PocketIO - Output passing message as PocketIO Server
4  
5 =head1 DESCRIPTION
6  
7 Run a webserver using PocketIO, then we can see the passing messages by browser. Even notify by chrome desktop notification api.
8  
9 =head1 SYNOPSIS
10  
11 $ message-passing --input STDIN --output PocketIO
12 {"foo" : "bar"}
13 $ open http://localhost:8080/
14  
15 =head1 METHODS
16
17 =head2 consume ($msg)
18
19 =head1 SEE ALSO
20
21 =over
22
23 =item L<Message::Passing>
24
25 =item L<http://logstash.net>
26
27 =item L<http://socket.io>
28  
29 =item L<PocketIO>
30  
31 =back
32
33 =head1 AUTHOR
34  
35 chenryn, E<lt>rao.chenlin@gmail.com<gt>
36  
37 =head1 COPYRIGHT AND LICENSE
38  
39 Copyright (C) 2012 by chenryn
40  
41 This library is free software; you can redistribute it and/or modify
42 it under the same terms as Perl itself, either Perl version 5.14.2 or,
43 at your option, any later version of Perl 5 you may have available.
44  
45 =cut
46  
47 package Message::Passing::Output::PocketIO;
48 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use MooX::Types::MooseLike::Base qw/ Str ArrayRef /;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use Scalar::Util qw/ weaken /;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use AnyMQ;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶use PocketIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use Twiggy::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use Plack::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use Data::Section::Simple qw/ get_data_section /;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 1 our $VERSION = '0.01';
64
65 1 with qw/ Message::Passing::Role::Output /;
66
67 has 'hostname' => (
68 is => 'ro',
69 isa => Str,
70 1 ▶ default => sub { '127.0.0.1' },
Subroutine __ANON__: called CC=1 SCAR=0.0
71 1 );
72
73 has 'port' => (
74 is => 'ro',
75 isa => Str,
76 0 ▶ default => sub { 8080 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 1 );
78
79 has _messages => (
80 is => 'ro',
81 isa => ArrayRef,
82 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
83 1 clearer => '_clear_messages',
84 lazy => 1,
85 );
86  
87  
88 1 has handle => (
89 is => 'ro',
90 builder => '_build_handle',
91 lazy => 1,
92 );
93  
94 1 has anymq => (
95 is => 'ro',
96 builder => '_build_anymq',
97 lazy => 1,
98 );
99  
100 1 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
101 1 my $self = shift;
102 1 $self->handle;
103 }
104  
105 1 ▶sub _build_anymq {
Subroutine _build_anymq: called CC=1 SCAR=0.0
106 1 AnyMQ->topic('message-passing');
107 };
108
109 1 ▶sub _build_handle {
Subroutine _build_handle: called CC=1 SCAR=0.5
110 1 my $self = shift;
111 1 weaken($self);
112  
113 1 my $listener = AnyMQ->new_listener($self->anymq);
114 1 ▶ $listener->poll(sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
115 1 my $message = shift;
116 1 push @{ $self->_messages }, $message;
117 1 });
118  
119 1 my $server = Twiggy::Server->new(
120 host => $self->hostname,
121 port => $self->port,
122 );
123 1 ▶ my $app = builder {
Subroutine __ANON__: called CC=1 SCAR=2.4
124 mount '/socket.io' => PocketIO->new(
125 1 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=2.0
126 1 my $class = shift;
127 $class->on(
128 0 ▶ 'message' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
129 0 my $class = shift;
130 0 $class->send( to_json($self->_messages) );
131 0 $self->_clear_messages;
132 }
133 1 );
134
135 }
136 1 );
137 0 ▶ mount '/socket.io.min.js' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 my $js = get_data_section('socket.io.min.js');
139 return [
140 0 200,
141 [ 'Content-Type' => 'application/x-javascript' ],
142 [ $js ],
143 ];
144 1 };
145 0 ▶ mount '/' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
146 0 my $url = 'http://' . $self->hostname . ':' . $self->port;
147 0 my $html = get_data_section('index.html');
148 0 $html =~ s/###URL###/$url/;
149 return [
150 0 200,
151 [ 'Content-Type' => 'text/html' ],
152 [ $html ],
153 ];
154 1 };
155 1 };
156 1 $server->register_service($app);
157 }
158  
159 1 ▶sub consume {
Subroutine consume: called CC=2 SCAR=7.1
Pod documented
160 1 my ( $self, $msg ) = @_;
161 1 ▶ $msg = from_json($msg) unless ref($msg);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $msg 0 1 1
162 1 $self->anymq->publish({ msg => $msg });
163 }
164  
165  
166 # Preloaded methods go here.
167  
168 1 1;
169 __DATA__
↑ Summary