| 1 | package Twilio::Jwt::Client::ScopeURI; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use 5.016; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | ▶ | use Twilio::Exceptions::TwilioException; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 2 | our $VERSION = '2.001'; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 2 | my $PARTS_OF_A_SCOPE = 3; | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 20 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 13 | 20 | my ($class, $service, $privilege, $params) = @_; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 20 | return bless { service => $service, privilege => $privilege, params => _pairs($params) }, $class; | |||||||||||||||||||
| 16 | } | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 1 | ▶ | sub get_service { return $_[0]{service} } | ||||||||||||||||||
|
Subroutine
get_service: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 19 | 14 | ▶ | sub get_privilege { return $_[0]{privilege} } | ||||||||||||||||||
|
Subroutine
get_privilege: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 5 | ▶ | sub get_params { | ||||||||||||||||||
|
Subroutine
get_params: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 22 | 5 | my ($self) = @_; | |||||||||||||||||||
| 23 | 5 | return { map { $_->[0] => $_->[1] } @{ $self->{params} } }; | |||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 4 | ▶ | sub add_param { | ||||||||||||||||||
|
Subroutine
add_param: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 27 | 4 | my ($self, $name, $value) = @_; | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 4 | $self->{params} = [grep { $_->[0] ne $name } @{ $self->{params} }]; | |||||||||||||||||||
| 30 | 4 | push @{ $self->{params} }, [$name, $value]; | |||||||||||||||||||
| 31 | 4 | return $self; | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 20 | ▶ | sub to_string { | ||||||||||||||||||
|
Subroutine
to_string: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 35 | 20 | my ($self) = @_; | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 20 | my $uri = "scope:$self->{service}:$self->{privilege}"; | |||||||||||||||||||
| 38 | 20 | ▶ | return $uri if !@{ $self->{params} }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | 19 | return "$uri?" . $self->build_query($self->{params}); | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 9 | ▶ | sub parse { | ||||||||||||||||||
|
Subroutine
parse: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 43 | 9 | my ($class, $uri) = @_; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 9 | ▶ | if (($uri // q{}) !~ /\Ascope:/) { | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$uri // ""
MC/DC100%1/1
$uri // ""
$uri
Truth table100%2/2
A
A= $uri
| |||||||||||||||||||||
| 46 | 2 | Twilio::Exceptions::TwilioException->throw('Not a scope URI according to scheme'); | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 7 | my ($scope, $query) = split /[?]/, $uri, 2; | |||||||||||||||||||
| 50 | 7 | my (undef, $service, $privilege) = split /:/, $scope, $PARTS_OF_A_SCOPE; | |||||||||||||||||||
| 51 | 7 | ▶ | if (!length($privilege // q{})) { | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$privilege // ""
MC/DC100%1/1
$privilege // ""
$privilege
Truth table100%2/2
A
A= $privilege
| |||||||||||||||||||||
| 52 | 2 | Twilio::Exceptions::TwilioException->throw('Not enough parts for scope URI'); | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 5 | ▶ | my @pairs = map { [_decoded_pair($_)] } split /&/, $query // q{}; | ||||||||||||||||||
|
Condition100%2/2
$query // ""
MC/DC100%1/1
$query // ""
$query
Truth table100%2/2
A
A= $query
| |||||||||||||||||||||
| 56 | 5 | return $class->new($service, $privilege, \@pairs); | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | # The same text as http_build_query in PHP, which is what Twilio's clients have always been sent: | ||||||||||||||||||||
| 60 | # a space is a plus, only these few characters stay as they are, and undefined values are skipped. | ||||||||||||||||||||
| 61 | 34 | ▶ | sub build_query { | ||||||||||||||||||
|
Subroutine
build_query: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 62 | 34 | my ($class, $params) = @_; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 34 | my @given = grep { defined $_->[1] } @{ _pairs($params) }; | |||||||||||||||||||
| 65 | 34 | return join q{&}, map { _encoded($_->[0]) . q{=} . _encoded($_->[1]) } @given; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | # Pairs keep the order a caller chose. A hash has none, so its names are sorted. | ||||||||||||||||||||
| 69 | 54 | ▶ | sub _pairs { | ||||||||||||||||||
|
Subroutine
_pairs: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 70 | 54 | my ($params) = @_; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 54 | ▶ | return [@{$params}] if ref $params eq 'ARRAY'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 15 | ▶ | my %given = %{ $params // {} }; | ||||||||||||||||||
|
Condition100%2/2
$params // {}
MC/DC100%1/1
$params // {}
$params
Truth table100%2/2
A
A= $params
| |||||||||||||||||||||
| 75 | 15 | return [map { [$_, $given{$_}] } sort keys %given]; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 96 | ▶ | sub _encoded { | ||||||||||||||||||
|
Subroutine
_encoded: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 79 | 96 | my ($text) = @_; | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 96 | my $encoded = _utf8_bytes($text); | |||||||||||||||||||
| 82 | 96 | $encoded =~ s/([^A-Za-z0-9\-_. ])/sprintf '%%%02X', ord $1/ge; | |||||||||||||||||||
| 83 | 96 | $encoded =~ tr/ /+/; | |||||||||||||||||||
| 84 | 96 | return $encoded; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | # As in Twilio::Http::Client: valid UTF-8 bytes are already encoded, and encoding them again | ||||||||||||||||||||
| 88 | # would corrupt text from a script without "use utf8". | ||||||||||||||||||||
| 89 | 96 | ▶ | sub _utf8_bytes { | ||||||||||||||||||
|
Subroutine
_utf8_bytes: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 90 | 96 | my ($value) = @_; | |||||||||||||||||||
| 91 | 96 | my $text = "$value"; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 96 | ▶ | if (!utf8::is_utf8($text)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 94 | my $probe = $text; | |||||||||||||||||||
| 95 | 94 | ▶ | return $text if utf8::decode($probe); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 3 | utf8::encode($text); | |||||||||||||||||||
| 99 | 3 | return $text; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 6 | ▶ | sub _decoded_pair { | ||||||||||||||||||
|
Subroutine
_decoded_pair: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 103 | 6 | my ($pair) = @_; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 6 | my ($name, $value) = split /=/, $pair, 2; | |||||||||||||||||||
| 106 | 6 | ▶ | return (_decoded($name), _decoded($value // q{})); | ||||||||||||||||||
|
Condition100%2/2
$value // ""
MC/DC100%1/1
$value // ""
$value
Truth table100%2/2
A
A= $value
| |||||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 12 | ▶ | sub _decoded { | ||||||||||||||||||
|
Subroutine
_decoded: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 110 | 12 | my ($text) = @_; | |||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 12 | $text =~ tr/+/ /; | |||||||||||||||||||
| 113 | 12 | $text =~ s/%([0-9A-Fa-f]{2})/chr hex $1/ge; | |||||||||||||||||||
| 114 | 12 | utf8::decode($text); | |||||||||||||||||||
| 115 | 12 | return $text; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 2 | 1; | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | __END__ | ||||||||||||||||||||