blib/lib/Net/Flowdock/Stream.pm

filter: Statementstmt 40.8% 29 / 71 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 6 / 12 Podpod 50.0% 1 / 2 totaltotal 29.2% 36 / 123 CC 17 SCAR 31.3
CC
17
Cov
28%
_socket_is_readable
42.0
_read_next_chunk
42.0
BUILD
20.0
CRAP 22.9
↑ Summary
1 package Net::Flowdock::Stream;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $Net::Flowdock::Stream::AUTHORITY = 'cpan:DOY';
4 }
5 {
6 1 $Net::Flowdock::Stream::VERSION = '0.01';
7 }
8 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # ABSTRACT: Streaming API for Flowdock
10  
11 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Net::HTTPS::NB;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16  
17 1 has token => (
18 is => 'ro',
19 isa => 'Str',
20 );
21  
22  
23 1 has email => (
24 is => 'ro',
25 isa => 'Str',
26 );
27  
28  
29 1 has password => (
30 is => 'ro',
31 isa => 'Str',
32 );
33  
34  
35 1 has flows => (
36 traits => ['Array'],
37 isa => 'ArrayRef[Str]',
38 required => 1,
39 handles => {
40 flows => 'elements',
41 },
42 );
43  
44 1 has socket_timeout => (
45 is => 'ro',
46 isa => 'Num',
47 default => 0.01,
48 );
49  
50 1 has debug => (
51 is => 'rw',
52 isa => 'Bool',
53 default => 0,
54 );
55  
56 1 has _socket => (
57 is => 'rw',
58 isa => 'Net::HTTPS::NB',
59 );
60  
61 1 has _readbuf => (
62 traits => ['String'],
63 is => 'rw',
64 isa => 'Str',
65 default => '',
66 handles => {
67 _append_readbuf => 'append',
68 },
69 );
70  
71 has _events => (
72 is => 'rw',
73 isa => 'ArrayRef[HashRef]', # XXX make these into objects
74 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 1 );
76  
77 0 ▶sub BUILD {
Subroutine BUILD: not called CC=4 SCAR=30.0
Pod undocumented
78 0 my $self = shift;
79  
80 0 my $auth;
81 0 ▶ if (my $token = $self->token) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $token = $self->token) { } 0 0 0
elsif (() = ($self->email, $self->password)) { } 0 0 0
82 0 $auth = $token;
83 }
84 elsif (my ($email, $pass) = ($self->email, $self->password)) {
85 0 $auth = "$email:$pass";
86 }
87 else {
88 0 die "You must supply either your token or your email and password";
89 }
90  
91 0 my $s = Net::HTTPS::NB->new(Host => 'stream.flowdock.com');
92 0 my $flows = join(',', $self->flows);
93 0 $s->write_request(
94 GET => "/flows?filter=$flows" =>
95 Authorization => 'Basic ' . MIME::Base64::encode($auth),
96 Accept => 'application/json',
97 );
98  
99 0 my ($code, $message, %headers) = $s->read_response_headers;
100 0 ▶ die "Unable to connect: $message"
Branch: 1 branch
BranchTrueFalseTotal
unless $code == 200 0 0 0
101 unless $code == 200;
102  
103 0 $self->_socket($s);
104 }
105  
106  
107 0 ▶sub get_next_event {
Subroutine get_next_event: not called CC=2 SCAR=17.9
Pod documented
108 0 my $self = shift;
109  
110 0 ▶ return unless $self->_socket_is_readable;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_socket_is_readable 0 0 0
111  
112 0 $self->_read_next_chunk;
113  
114 0 return $self->_process_readbuf;
115 }
116  
117 0 ▶sub _socket_is_readable {
Subroutine _socket_is_readable: not called CC=6 SCAR=37.4
118 0 my $self = shift;
119  
120 0 my ($rin, $rout) = ('');
121 0 vec($rin, fileno($self->_socket), 1) = 1;
122  
123 0 my $res = select($rout = $rin, undef, undef, $self->socket_timeout);
124  
125 0 ▶ if ($res == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res == -1) 0 0 0
126 0 ▶ return if $!{EAGAIN} || $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EAGAIN"} or $!{"EINTR"} 0 0 0
Condition0%0/3
$!{"EAGAIN"} or $!{"EINTR"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$!{"EAGAIN"} or $!{"EINTR"}
$!{"EAGAIN"} $!{"EINTR"}
Truth table0%0/3
A or B
A= $!{"EAGAIN"}B= $!{"EINTR"}
ABresult
1X1
011
000
127 0 die "Error reading from socket: $!";
128 }
129  
130 0 ▶ return if $res == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $res == 0 0 0 0
131  
132 0 ▶ return 1 if $rout;
Branch: 1 branch
BranchTrueFalseTotal
if $rout 0 0 0
133  
134 0 return;
135 }
136  
137 0 ▶sub _read_next_chunk {
Subroutine _read_next_chunk: not called CC=6 SCAR=37.4
138 0 my $self = shift;
139  
140 0 my $nbytes = $self->_socket->read_entity_body(my $buf, 4096);
141 0 ▶ if (!defined $nbytes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nbytes) 0 0 0
142 0 ▶ return if $!{EINTR} || $!{EAGAIN};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} or $!{"EAGAIN"} 0 0 0
Condition0%0/3
$!{"EINTR"} or $!{"EAGAIN"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$!{"EINTR"} or $!{"EAGAIN"}
$!{"EINTR"} $!{"EAGAIN"}
Truth table0%0/3
A or B
A= $!{"EINTR"}B= $!{"EAGAIN"}
ABresult
1X1
011
000
143 0 die "Error reading from server";
144 }
145 0 ▶ die "Disconnected" if $nbytes == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $nbytes == 0 0 0 0
146 0 ▶ return if $nbytes == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $nbytes == -1 0 0 0
147  
148 0 $self->_append_readbuf($buf);
149 }
150  
151 0 ▶sub _process_readbuf {
Subroutine _process_readbuf: not called CC=3 SCAR=24.8
152 0 my $self = shift;
153  
154 0 ▶ if ((my $buf = $self->_readbuf) =~ s/^([^\x0d]*)\x0d//) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $buf = $self->_readbuf) =~ s/^([^\x0d]*)\x0d//) 0 0 0
155 0 my $chunk = $1;
156 0 $self->_readbuf($buf);
157 0 ▶ warn "New event:\n$chunk" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
158 0 return decode_json($chunk);
159 }
160  
161 0 return;
162 }
163  
164 1 __PACKAGE__->meta->make_immutable;
165 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
166  
167  
168 1 1;
169  
170 __END__
↑ Summary