| 1 | package Twilio::Page; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 18 | ▶ | use 5.016; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | 18 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | 18 | ▶ | use Twilio::Exceptions::DeserializeException; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | 18 | our $VERSION = '2.001'; | |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | 18 | my $PAGING_END_CODE = '20006'; | |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | 18 | my %META_KEYS = map { $_ => 1 } qw( | |||||||||||||||||||||||||||||||||
| 15 | end first_page_uri last_page_uri next_page_uri num_pages page page_size | ||||||||||||||||||||||||||||||||||
| 16 | previous_page_uri schemas start total totalResults uri | ||||||||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | 503 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 20 | 503 | my ($class, $version, $response, $solution) = @_; | |||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | 503 | ▶ | my $self = bless { | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$solution // {}
MC/DC100%1/1
$solution // {}
$solution
Truth table100%2/2
A
A= $solution
| |||||||||||||||||||||||||||||||||||
| 23 | version => $version, | ||||||||||||||||||||||||||||||||||
| 24 | solution => $solution // {}, | ||||||||||||||||||||||||||||||||||
| 25 | records => [], | ||||||||||||||||||||||||||||||||||
| 26 | position => 0, | ||||||||||||||||||||||||||||||||||
| 27 | }, $class; | ||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | 503 | $self->{payload} = $self->process_response($response); | |||||||||||||||||||||||||||||||||
| 30 | 498 | $self->{records} = $self->load_page; | |||||||||||||||||||||||||||||||||
| 31 | 490 | return $self; | |||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | sub build_instance { | ||||||||||||||||||||||||||||||||
|
Subroutine
build_instance: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 35 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 36 | 1 | croak ref($self) . ' must implement build_instance'; | |||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | # Generated pages name their list, which the single-list search in load_page can only guess. | ||||||||||||||||||||||||||||||||||
| 40 | 83 | ▶ | sub records_key { return } | ||||||||||||||||||||||||||||||||
|
Subroutine
records_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | 503 | ▶ | sub process_response { | ||||||||||||||||||||||||||||||||
|
Subroutine
process_response: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 43 | 503 | my ($self, $response) = @_; | |||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||
| 45 | 503 | my $content = $response->get_content; | |||||||||||||||||||||||||||||||||
| 46 | 503 | ▶ | return $content if $response->get_status_code == 200 || _is_paging_end($content); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$response->get_status_code == 200 or _is_paging_end($content)
MC/DC100%2/2
$response->get_status_code == 200 or _is_paging_end($content)
$response->get_status_code == 200
_is_paging_end($content)
Truth table100%3/3
A or B
A= $response->get_status_code == 200B= _is_paging_end($content)
| |||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||
| 48 | 5 | die $self->get_version->exception($response, 'Unable to fetch page'); ## no critic (ErrorHandling::RequireCarping) | |||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | 498 | ▶ | sub load_page { | ||||||||||||||||||||||||||||||||
|
Subroutine
load_page: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 52 | 498 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | 498 | my $payload = $self->{payload}; | |||||||||||||||||||||||||||||||||
| 55 | 498 | ▶ | return [] if _is_paging_end($payload); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||
| 57 | # twilio-php requires exactly one top-level key besides its known list. Counting only | ||||||||||||||||||||||||||||||||||
| 58 | # arrays means a new scalar or object from Twilio cannot break paging. | ||||||||||||||||||||||||||||||||||
| 59 | 495 | ▶ | my $key = $self->records_key // $self->get_meta('key'); | ||||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->records_key // $self->get_meta("key")
MC/DC100%2/2
$self->records_key // $self->get_meta("key")
$self->records_key
$self->get_meta("key")
Truth table100%3/3
A // B
A= $self->records_keyB= $self->get_meta("key")
| |||||||||||||||||||||||||||||||||||
| 60 | 495 | ▶ | if (!defined $key && ref $payload eq 'HASH') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $key and ref $payload eq "HASH"
MC/DC100%2/2
not defined $key and ref $payload eq "HASH"
not defined $key
ref $payload eq "HASH"
Truth table100%3/3
not(A) and B
A= not defined $keyB= ref $payload eq "HASH"
| |||||||||||||||||||||||||||||||||||
| 61 | 47 | ▶ | my @lists = grep { !$META_KEYS{$_} && ref $payload->{$_} eq 'ARRAY' } keys %{$payload}; | ||||||||||||||||||||||||||||||||
|
Condition100%3/3
!$META_KEYS{$_} && ref $payload->{$_} eq 'ARRAY'
MC/DC100%2/2
!$META_KEYS{$_} && ref $payload->{$_} eq 'ARRAY'
!$META_KEYS{$_}
ref $payload->{$_} eq 'ARRAY'
Truth table100%3/3
not(A) && B
A= !$META_KEYS{$_}B= ref $payload->{$_} eq 'ARRAY'
| |||||||||||||||||||||||||||||||||||
| 62 | 47 | ▶ | $key = $lists[0] if @lists == 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | 495 | ▶ | if (!defined $key || ref $payload->{$key} ne 'ARRAY') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $key or ref $payload->{$key} ne "ARRAY"
MC/DC100%2/2
not defined $key or ref $payload->{$key} ne "ARRAY"
not defined $key
ref $payload->{$key} ne "ARRAY"
Truth table100%3/3
not(A) or B
A= not defined $keyB= ref $payload->{$key} ne "ARRAY"
| |||||||||||||||||||||||||||||||||||
| 66 | 8 | Twilio::Exceptions::DeserializeException->throw('Page Records can not be deserialized'); | |||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||
| 68 | 487 | return $payload->{$key}; | |||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||
| 71 | 343 | ▶ | sub has_meta { | ||||||||||||||||||||||||||||||||
|
Subroutine
has_meta: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 72 | 343 | my ($self, $key) = @_; | |||||||||||||||||||||||||||||||||
| 73 | 343 | my $payload = $self->{payload}; | |||||||||||||||||||||||||||||||||
| 74 | 343 | ▶ | return ref $payload eq 'HASH' && ref $payload->{meta} eq 'HASH' && exists $payload->{meta}{$key}; | ||||||||||||||||||||||||||||||||
|
Condition100%6/6
ref $payload eq 'HASH' && ref $payload->{'meta'} eq 'HASH' && exists $payload->{'meta'}{$key}
ref $payload eq "HASH" and ref $payload->{"meta"} eq "HASH"
MC/DC100%3/3
ref $payload eq 'HASH' && ref $payload->{'meta'} eq 'HASH' && exists $payload->{'meta'}{$key}
ref $payload eq "HASH"
ref $payload->{"meta"} eq "HASH"
exists $payload->{'meta'}{$key}
Truth table100%4/4
A and B && C
A= ref $payload eq "HASH"B= ref $payload->{"meta"} eq "HASH"C= exists $payload->{'meta'}{$key}
| |||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||
| 77 | 280 | ▶ | sub get_meta { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_meta: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 78 | 280 | my ($self, $key, $default) = @_; | |||||||||||||||||||||||||||||||||
| 79 | 280 | ▶ | return $self->has_meta($key) ? $self->{payload}{meta}{$key} : $default; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | 57 | ▶ | sub get_next_page_url { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_next_page_url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 83 | 57 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 84 | 57 | return $self->_page_url('next'); | |||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||
| 87 | 4 | ▶ | sub get_previous_page_url { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_previous_page_url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 88 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 89 | 4 | return $self->_page_url('previous'); | |||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||
| 92 | 67 | ▶ | sub get_response { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_response: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 93 | 67 | my ($self, $url) = @_; | |||||||||||||||||||||||||||||||||
| 94 | 67 | ▶ | return if !$url; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 95 | 31 | return $self->get_version->get_domain->get_client->request('GET', $url); | |||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | 31 | ▶ | sub create_page { | ||||||||||||||||||||||||||||||||
|
Subroutine
create_page: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 99 | 31 | my ($self, $response) = @_; | |||||||||||||||||||||||||||||||||
| 100 | 31 | return ref($self)->new($self->get_version, $response, $self->{solution}); | |||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | 64 | ▶ | sub next_page { | ||||||||||||||||||||||||||||||||
|
Subroutine
next_page: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 104 | 64 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 105 | 64 | return $self->_page_at($self->get_next_page_url); | |||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||
| 108 | 3 | ▶ | sub previous_page { | ||||||||||||||||||||||||||||||||
|
Subroutine
previous_page: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 109 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 110 | 3 | return $self->_page_at($self->get_previous_page_url); | |||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||
| 113 | 149 | ▶ | sub next { | ||||||||||||||||||||||||||||||||
|
Subroutine
next: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 114 | 149 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 115 | 149 | ▶ | return if $self->{position} >= @{ $self->{records} }; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 116 | 83 | return $self->instance_for($self->{records}[$self->{position}++]); | |||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||
| 119 | 4 | ▶ | sub rewind { | ||||||||||||||||||||||||||||||||
|
Subroutine
rewind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 120 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 121 | 4 | $self->{position} = 0; | |||||||||||||||||||||||||||||||||
| 122 | 4 | return; | |||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | 390 | ▶ | sub records { | ||||||||||||||||||||||||||||||||
|
Subroutine
records: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 126 | 390 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 127 | 390 | return map { $self->instance_for($_) } @{ $self->{records} }; | |||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||
| 130 | 181 | ▶ | sub get_version { return $_[0]{version} } | ||||||||||||||||||||||||||||||||
|
Subroutine
get_version: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||
| 132 | 417 | ▶ | sub instance_for { | ||||||||||||||||||||||||||||||||
|
Subroutine
instance_for: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 133 | 417 | my ($self, $item) = @_; | |||||||||||||||||||||||||||||||||
| 134 | 417 | return $self->build_instance($item); | |||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||
| 137 | 61 | ▶ | sub _page_url { | ||||||||||||||||||||||||||||||||
|
Subroutine
_page_url: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 138 | 61 | my ($self, $direction) = @_; | |||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||
| 140 | 61 | my $meta_key = "${direction}_page_url"; | |||||||||||||||||||||||||||||||||
| 141 | 61 | ▶ | return $self->get_meta($meta_key) if $self->has_meta($meta_key); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||
| 143 | 55 | my $uri = $self->{payload}{"${direction}_page_uri"}; | |||||||||||||||||||||||||||||||||
| 144 | 55 | ▶ | return if !$uri; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 145 | 24 | return $self->get_version->get_domain->absolute_url($uri); | |||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||
| 148 | 67 | ▶ | sub _page_at { | ||||||||||||||||||||||||||||||||
|
Subroutine
_page_at: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 149 | 67 | my ($self, $url) = @_; | |||||||||||||||||||||||||||||||||
| 150 | 67 | ▶ | my $response = $self->get_response($url) or return; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 151 | 31 | return $self->create_page($response); | |||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||
| 154 | # Reading past the last page answers with this error code, sometimes with status 400. | ||||||||||||||||||||||||||||||||||
| 155 | 505 | ▶ | sub _is_paging_end { | ||||||||||||||||||||||||||||||||
|
Subroutine
_is_paging_end: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 156 | 505 | my ($content) = @_; | |||||||||||||||||||||||||||||||||
| 157 | 505 | ▶ | return ref $content eq 'HASH' && ($content->{code} // q{}) eq $PAGING_END_CODE; | ||||||||||||||||||||||||||||||||
|
Condition100%5/5
ref $content eq 'HASH' && ($content->{'code'} // '') eq $PAGING_END_CODE
$content->{"code"} // ""
MC/DC100%2/2
ref $content eq 'HASH' && ($content->{'code'} // '') eq $PAGING_END_CODE
ref $content eq 'HASH'
$content->{"code"}
Truth table100%3/3
A && B
A= ref $content eq 'HASH'B= $content->{"code"}
| |||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||
| 160 | 18 | 1; | |||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||
| 162 | __END__ | ||||||||||||||||||||||||||||||||||