blib/lib/MessagePack/RPC/HTTP/Client.pm

filter: Statementstmt 52.0% 26 / 50 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 54.5% 6 / 11 Podpod 0.0% 0 / 5 totaltotal 41.5% 32 / 77 CC 6 SCAR 16.1
CC
6
Cov
43%
get_result
12.0
new
6.0
send_request
6.0
CRAP 5.0
↑ Summary
1 package MessagePack::RPC::HTTP::Client;
2 1 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Data::MessagePack;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Furl;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = "0.02";
12  
13 1 my $HEADER = ["Content-Type" => "application/x-msgpack"];
14  
15 # copy & paste from msgpack-rpc-over-http/lib/msgpack-rpc-over-http.rb
16 1 my $REQUEST = 0; # [0, msgid, method, param]
17 1 my $RESPONSE = 1; # [1, msgid, error, result]
18 1 my $NOTIFY = 2; # [2, method, param]
19  
20 1 my $NO_METHOD_ERROR = 0x01;
21 1 my $ARGUMENT_ERROR = 0x02;
22  
23 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
24 0 my ($this, $url, %options) = @_;
25 my $self = bless +{
26 url => $url,
27 http_client => Furl->new(
28 agent => "msgpack-rpc-over-http client (perl $VERSION)",
29 0 ▶ timeout => $options{timeout} || 5,
Condition0%0/2
$options{'timeout'} || 5
l!l
00
MC/DC0%0/1
$options{'timeout'} || 5
$options{'timeout'}
Truth table0%0/2
A
A= $options{'timeout'}
Aresult
11
00
30 ),
31 seqid => 0,
32 }, $this;
33 0 $self;
34 }
35  
36 0 ▶sub call {
Subroutine call: not called CC=1 SCAR=6.9
Pod undocumented
37 0 my ($self, $method, @args) = @_;
38 0 $self->send_request($method, \@args);
39 }
40  
41 0 ▶sub send_request { # param: ArrayRef
Subroutine send_request: not called CC=2 SCAR=17.9
Pod undocumented
42 0 my ($self, $method, $param) = @_;
43 0 my $data = $self->create_request_body($method, $param);
44 # $furl->post($url :Str, $headers :ArrayRef[Str], $content :Any)
45 0 my $res = $self->{http_client}->post($self->{url}, $HEADER, $data);
46 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
47 0 croak "ServerError: check server log or status";
48 }
49 0 my $body = $res->body;
50 0 $self->get_result($body);
51 }
52  
53 1 my $SEQID_MAX = ( 1 << 31 );
54  
55 0 ▶sub create_request_body {
Subroutine create_request_body: not called CC=2 SCAR=17.9
Pod undocumented
56 0 my ($self, $method, $param) = @_;
57 0 my $msgid = ($self->{seqid})++;
58 0 ▶ $self->{seqid} = 0 if $self->{seqid} >= $SEQID_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"seqid"} >= $SEQID_MAX 0 0 0
59 0 Data::MessagePack->pack([$REQUEST, $msgid, $method, $param]);
60 }
61  
62 0 ▶sub get_result {
Subroutine get_result: not called CC=3 SCAR=24.8
Pod undocumented
63 0 my ($self, $body) = @_;
64 # type, msgid, err, res
65 0 my ($type, $msgid, $err, $res) = @{ Data::MessagePack->unpack($body) };
66 0 ▶ if ($type != $RESPONSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type != $RESPONSE) 0 0 0
67 0 croak "Unknown message type $type";
68 }
69 0 ▶ if (not defined($err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $err) { } 0 0 0
70 0 return $res;
71 } else {
72 0 croak "RemoteError $err: $res";
73 }
74 }
75  
76 1 1;
77 __END__
↑ Summary