WebApi/Client.pm

filter: Statementstmt 90.2% 65 / 72 Branchbran 66.6% 16 / 24 Conditioncond 75.0% 6 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 3 totaltotal 82.4% 103 / 125 CC 16 SCAR 15.7
CC
16
Cov
84%
request_json
10.0
request
6.0
_build_ua
5.0
CRAP 4.8
« WebApi/Files.pm ↑ Summary WebApi/Chat.pm »
1 package WebService::Slack::WebApi::Client;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use HTTP::AnyUA;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use WebService::Slack::WebApi::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use Class::Accessor::Lite::Lazy (
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 new => 1,
12 rw => [qw/ team_domain token opt useragent /],
13 ro_lazy => [qw/ ua /],
14 5 );
15  
16 7 ▶sub _build_ua {
Subroutine _build_ua: called CC=5 SCAR=16.2
17 7 my $self = shift;
18 7 my $ua;
19 # Before introducing the parameter 'ua' to WebService::Slack::WebApi
20 # we used Furl. So let's keep ourselves backward compatible!
21 7 ▶ if( $self->useragent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->useragent) { } 4 3 7
22 4 $ua = HTTP::AnyUA->new( ua => $self->useragent );
23 } else {
24 # Attn. Using expression form of eval because otherwise
25 # the "use" would be executed before arriving to eval.
26 3 ▶ eval 'use Furl; 1;' or do {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "use Furl; 1;" or do { my $msg = "Illegal parameters. Unable to use package Furl. If no 'ua' is defined, we use Furl by default"; "WebService::Slack::WebApi::Exception::IllegalParameters"->throw("message", $msg) } 3 0 3
27 0 my $msg = 'Illegal parameters. Unable to use package Furl.'
28 . ' If no \'ua\' is defined, we use Furl by default';
29 0 WebService::Slack::WebApi::Exception::IllegalParameters->throw(
30 message => $msg,
31 );
32 };
33 3 ▶ my %opt = %{ $self->opt // +{} };
Condition100%2/2
$self->opt // {}
l!l
21
MC/DC100%1/1
$self->opt // {}
$self->opt
Truth table100%2/2
A
A= $self->opt
Aresult
11
00
34 3 my $env_proxy = delete $opt{env_proxy};
35 3 my $furl = Furl->new(%opt);
36 3 ▶ $furl->env_proxy if $env_proxy;
Branch: 1 branch
BranchTrueFalseTotal
if $env_proxy 1 2 3
37 3 $ua = HTTP::AnyUA->new( ua => $furl );
38 }
39 7 return $ua;
40 }
41  
42 1177 ▶sub base_url {
Subroutine base_url: called CC=2 SCAR=7.1
Pod undocumented
43 1177 my $self = shift;
44 1177 ▶ my $team_domain = $self->team_domain ? $self->team_domain . '.' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->team_domain ? : 0 1177 1177
45 1177 return sprintf 'https://%sslack.com/api', $team_domain;
46 }
47  
48 1161 ▶sub request {
Subroutine request: called CC=6 SCAR=17.9
Pod undocumented
49 1161 my ($self, $path, $params) = @_;
50  
51 1161 my %headers;
52 1161 ▶ if( $self->token && $params->{'http_auth'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->token and $params->{"http_auth"}) 1 1160 1161
Condition100%3/3
$self->token and $params->{"http_auth"}
!ll&&!rl&&r
111591
MC/DC100%2/2
$self->token and $params->{"http_auth"}
$self->token $params->{"http_auth"}
Truth table100%3/3
A and B
A= $self->tokenB= $params->{"http_auth"}
ABresult
0X0
100
111
53 1 my $msg = 'Illegal parameters. You have defined \'token\' but the '
54 . ' method you are using defines its own HTTP Authorization header.';
55 1 WebService::Slack::WebApi::Exception::IllegalParameters->throw(
56 message => $msg,
57 );
58 }
59 1160 ▶ if( $self->token ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->token) { } 1159 1 1160
elsif ($params->{'http_auth'}) { } 1 0 1
60 1159 $headers{ 'Authorization' } = 'Bearer ' . $self->token;
61 } elsif( $params->{'http_auth'} ) {
62 1 $headers{ 'Authorization' } = $params->{'http_auth'};
63 }
64 1160 my %options = ( headers => \%headers );
65 my $response = $self->ua->post_form(
66 $self->base_url . $path,
67 [
68 1160 %{ $params },
69 ],
70 \%options,
71 );
72 1160 ▶ return decode_json $response->{content} if $response->{success};
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"success"} 1159 1 1160
73  
74 1 WebService::Slack::WebApi::Exception::FailureResponse->throw(
75 message => 'request failed.',
76 response => $response,
77 );
78 }
79  
80 17 ▶sub request_json {
Subroutine request_json: called CC=6 SCAR=23.0
Pod undocumented
81 17 my ($self, $path, $params) = @_;
82  
83 17 my %headers = ( 'Content-Type' => 'application/json' );
84 17 ▶ if( $self->token && $params->{'http_auth'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->token and $params->{"http_auth"}) 0 17 17
Condition33%1/3
$self->token and $params->{"http_auth"}
!ll&&!rl&&r
0170
MC/DC0%0/2
$self->token and $params->{"http_auth"}
$self->token $params->{"http_auth"}
Truth table33%1/3
A and B
A= $self->tokenB= $params->{"http_auth"}
ABresult
0X0
100
111
85 0 my $msg = 'Illegal parameters. You have defined \'token\' but the '
86 . ' method you are using defines its own HTTP Authorization header.';
87 0 WebService::Slack::WebApi::Exception::IllegalParameters->throw(
88 message => $msg,
89 );
90 }
91 17 ▶ if( $self->token ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->token) { } 17 0 17
elsif ($params->{'http_auth'}) { } 0 0 0
92 17 $headers{ 'Authorization' } = 'Bearer ' . $self->token;
93 } elsif( $params->{'http_auth'} ) {
94 0 $headers{ 'Authorization' } = $params->{'http_auth'};
95 0 delete $params->{'http_auth'}; # slack will not allow this in the json body
96 }
97  
98 # should really add error handling here.
99 17 my $json_body = JSON->new->utf8(1)->pretty(0)->canonical->encode($params);
100 17 my %options = ( headers => \%headers, content => $json_body );
101
102 17 my $response = $self->ua->request(
103 'POST',
104 $self->base_url . $path,
105 \%options,
106 );
107 17 ▶ return decode_json $response->{content} if $response->{success};
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"success"} 17 0 17
108  
109 0 WebService::Slack::WebApi::Exception::FailureResponse->throw(
110 message => 'request_json failed.',
111 response => $response,
112 );
113 }
114  
115 5 1;
116  
« WebApi/Files.pm ↑ Summary WebApi/Chat.pm »