| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | return if $!{EAGAIN} || $!{EINTR}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$!{"EAGAIN"} or $!{"EINTR"}
MC/DC0%0/2
$!{"EAGAIN"} or $!{"EINTR"}
$!{"EAGAIN"}
$!{"EINTR"}
Truth table0%0/3
A or B
A= $!{"EAGAIN"}B= $!{"EINTR"}
| |||||||||||||||||||||||||||||
| 127 | 0 | die "Error reading from socket: $!"; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | return if $res == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | return 1 if $rout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | return if $!{EINTR} || $!{EAGAIN}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$!{"EINTR"} or $!{"EAGAIN"}
MC/DC0%0/2
$!{"EINTR"} or $!{"EAGAIN"}
$!{"EINTR"}
$!{"EAGAIN"}
Truth table0%0/3
A or B
A= $!{"EINTR"}B= $!{"EAGAIN"}
| |||||||||||||||||||||||||||||
| 143 | 0 | die "Error reading from server"; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | die "Disconnected" if $nbytes == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | return if $nbytes == -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 155 | 0 | my $chunk = $1; | |||||||||||||||||||||||||||
| 156 | 0 | $self->_readbuf($buf); | |||||||||||||||||||||||||||
| 157 | 0 | ▶ | warn "New event:\n$chunk" if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||