API.pm

filter: Statementstmt 63.8% 53 / 83 Branchbran 12.5% 2 / 16 Conditioncond 40.0% 8 / 20 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 61.1% 11 / 18 Podpod 71.4% 5 / 7 totaltotal 51.6% 80 / 155 CC 19 SCAR 32.2
CC
19
Cov
53%
_do_request
90.0
new
7.0
_build_content
5.1
CRAP 25.0
↑ Summary examples.pl »
1 package WWW::Twilio::API;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.21';
8 2 our $Debug = 0;
9  
10 2 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use URI::Escape qw(uri_escape uri_escape_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use List::Util '1.29', 'pairs';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶sub API_URL { 'https://api.twilio.com' }
Subroutine API_URL: not called CC=1 SCAR=6.9
Pod undocumented
16 1 ▶sub API_VERSION { '2010-04-01' }
Subroutine API_VERSION: called CC=1 SCAR=0.0
Pod documented
17  
18 ## NOTE: This is an inside-out object; remove members in
19 ## NOTE: the DESTROY() sub if you add additional members.
20  
21 2 my %account_sid = ();
22 2 my %auth_token = ();
23 2 my %api_version = ();
24 2 my %lwp_callback = ();
25 2 my %utf8 = ();
26  
27 1 ▶sub new {
Subroutine new: called CC=6 SCAR=19.4
Pod documented
28 1 my $class = shift;
29 1 my %args = @_;
30  
31 1 my $self = bless \(my $ref), $class;
32  
33 1 ▶ $account_sid {$self} = $args{AccountSid} || '';
Condition50%1/2
$args{'AccountSid'} || ''
l!l
01
MC/DC0%0/1
$args{'AccountSid'} || ''
$args{'AccountSid'}
Truth table50%1/2
A
A= $args{'AccountSid'}
Aresult
11
00
34 1 ▶ $auth_token {$self} = $args{AuthToken} || '';
Condition50%1/2
$args{'AuthToken'} || ''
l!l
01
MC/DC0%0/1
$args{'AuthToken'} || ''
$args{'AuthToken'}
Truth table50%1/2
A
A= $args{'AuthToken'}
Aresult
11
00
35 1 ▶ $api_version {$self} = $args{API_VERSION} || API_VERSION();
Condition33%1/3
$args{'API_VERSION'} || API_VERSION()
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{'API_VERSION'} || API_VERSION()
$args{'API_VERSION'} API_VERSION()
Truth table33%1/3
A || B
A= $args{'API_VERSION'}B= API_VERSION()
ABresult
1X1
011
000
36 1 ▶ $lwp_callback {$self} = $args{LWP_Callback} || undef;
Condition50%1/2
$args{'LWP_Callback'} || undef
l!l
01
MC/DC0%0/1
$args{'LWP_Callback'} || undef
$args{'LWP_Callback'}
Truth table50%1/2
A
A= $args{'LWP_Callback'}
Aresult
11
00
37 1 ▶ $utf8 {$self} = $args{utf8} || undef;
Condition50%1/2
$args{'utf8'} || undef
l!l
01
MC/DC0%0/1
$args{'utf8'} || undef
$args{'utf8'}
Truth table50%1/2
A
A= $args{'utf8'}
Aresult
11
00
38  
39 1 return $self;
40 }
41  
42 0 ▶sub GET {
Subroutine GET: not called CC=1 SCAR=6.9
Pod documented
43 0 _do_request(shift, METHOD => 'GET', API => shift, @_);
44 }
45  
46 0 ▶sub HEAD {
Subroutine HEAD: not called CC=1 SCAR=6.9
Pod undocumented
47 0 _do_request(shift, METHOD => 'HEAD', API => shift, @_);
48 }
49  
50 0 ▶sub POST {
Subroutine POST: not called CC=1 SCAR=6.9
Pod documented
51 0 _do_request(shift, METHOD => 'POST', API => shift, @_);
52 }
53  
54 0 ▶sub PUT {
Subroutine PUT: not called CC=1 SCAR=6.9
Pod documented
55 0 _do_request(shift, METHOD => 'PUT', API => shift, @_);
56 }
57  
58 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
59 0 _do_request(shift, METHOD => 'DELETE', API => shift, @_);
60 }
61  
62 ## METHOD => GET|POST|PUT|DELETE
63 ## API => Calls|Accounts|OutgoingCallerIds|IncomingPhoneNumbers|
64 ## Recordings|Notifications|etc.
65 0 ▶sub _do_request {
Subroutine _do_request: not called CC=9 SCAR=45.0
66 0 my $self = shift;
67 0 my %args = @_;
68  
69 0 my $lwp = LWP::UserAgent->new;
70 $lwp_callback{$self}->($lwp)
71 0 ▶ if ref($lwp_callback{$self}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $lwp_callback{$self} eq "CODE" 0 0 0
72 0 $lwp->agent("perl-WWW-Twilio-API/$VERSION");
73  
74 0 my $method = delete $args{METHOD};
75  
76 0 my $url = API_URL() . '/' . $api_version{$self};
77 0 ▶ my $api = delete $args{API} || '';
Condition0%0/2
delete $args{'API'} || ''
l!l
00
MC/DC0%0/1
delete $args{'API'} || ''
delete $args{'API'}
Truth table0%0/2
A
A= delete $args{'API'}
Aresult
11
00
78 0 $url .= "/Accounts/" . $account_sid{$self};
79 0 ▶ $url .= ( $api eq 'Accounts' ? '' : "/$api" );
Branch: 1 branch
BranchTrueFalseTotal
$api eq 'Accounts' ? : 0 0 0
80  
81 0 my $content = '';
82 0 ▶ if( keys %args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %args) 0 0 0
83 0 $content = $self->_build_content( %args );
84  
85 0 ▶ if( $method eq 'GET' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "GET") 0 0 0
86 0 $url .= '?' . $content;
87 }
88 }
89  
90 0 my $req = HTTP::Request->new( $method => $url );
91 0 $req->authorization_basic( $account_sid{$self}, $auth_token{$self} );
92 0 ▶ if( $content and $method ne 'GET' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content and $method ne "GET") 0 0 0
Condition0%0/3
$content and $method ne "GET"
!ll&&!rl&&r
000
MC/DC0%0/2
$content and $method ne "GET"
$content $method ne "GET"
Truth table0%0/3
A and B
A= $contentB= $method ne "GET"
ABresult
0X0
100
111
93 0 $req->content_type( 'application/x-www-form-urlencoded' );
94 0 $req->content( $content );
95 }
96  
97 0 local $ENV{HTTPS_DEBUG} = $Debug;
98 0 my $res = $lwp->request($req);
99 0 ▶ print STDERR "Request sent: " . $req->as_string . "\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $WWW::Twilio::API::Debug 0 0 0
100  
101 0 return { code => $res->code,
102 message => $res->message,
103 content => $res->content };
104 }
105  
106 ## builds a string suitable for LWP's content() method
107 2 ▶sub _build_content {
Subroutine _build_content: called CC=5 SCAR=16.3
108 2 my $self = shift;
109  
110 2 ▶ my $escape_method = $utf8{$self} ? \&uri_escape_utf8 : \&uri_escape;
Branch: 1 branch
BranchTrueFalseTotal
$utf8{$self} ? : 0 2 2
111  
112 2 my @args = ();
113 2 for my $pair (pairs @_) {
114 7 my ($key, $val) = @$pair;
115  
116 7 ▶ push @args, &$escape_method($key) . '=' . &$escape_method($val // '');
Condition100%2/2
$val // ""
l!l
61
MC/DC100%1/1
$val // ""
$val
Truth table100%2/2
A
A= $val
Aresult
11
00
117 }
118  
119 2 ▶ return join('&', @args) || '';
Condition50%1/2
join('&', @args) || ''
l!l
20
MC/DC0%0/1
join('&', @args) || ''
join('&', @args)
Truth table50%1/2
A
A= join('&', @args)
Aresult
11
00
120 }
121  
122 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.0
123 1 my $self = $_[0];
124  
125 1 delete $account_sid {$self};
126 1 delete $auth_token {$self};
127 1 delete $api_version {$self};
128 1 delete $lwp_callback{$self};
129 1 delete $utf8 {$self};
130  
131 1 my $super = $self->can("SUPER::DESTROY");
132 1 ▶ goto &$super if $super;
Branch: 1 branch
BranchTrueFalseTotal
if $super 0 1 1
133 }
134  
135 2 1;
136 __END__
↑ Summary examples.pl »