| 1 | package WebService::HealthGraph::Response; | ||||||||||||||||||||
| 2 | 2 | $WebService::HealthGraph::Response::VERSION = '0.000004'; | |||||||||||||||||||
| 3 | 2 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 2 | ▶ | use Array::Iterator (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use JSON::MaybeXS qw( decode_json ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use Types::Standard qw( Bool CodeRef InstanceOf Maybe Ref ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use Types::URI qw( Uri ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | # records could be an ArrayRef. other than that, we should be mostly dealing | ||||||||||||||||||||
| 11 | # with a HashRef | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 2 | has _auto_pagination => ( | |||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||
| 15 | isa => Bool, | ||||||||||||||||||||
| 16 | init_arg => 'auto_pagination', | ||||||||||||||||||||
| 17 | default => 1, | ||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 2 | has content => ( | |||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||
| 22 | isa => Maybe [Ref], | ||||||||||||||||||||
| 23 | lazy => 1, | ||||||||||||||||||||
| 24 | clearer => '_clear_content', | ||||||||||||||||||||
| 25 | builder => '_build_content', | ||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 2 | has _get => ( | |||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||
| 30 | isa => CodeRef, | ||||||||||||||||||||
| 31 | init_arg => 'get', | ||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 2 | has _iterator => ( | |||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||
| 36 | isa => InstanceOf ['Array::Iterator'], | ||||||||||||||||||||
| 37 | clearer => '_clear_iterator', | ||||||||||||||||||||
| 38 | builder => '_build_iterator', | ||||||||||||||||||||
| 39 | lazy => 1, | ||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | has next_page_uri => ( | ||||||||||||||||||||
| 43 | is => 'ro', | ||||||||||||||||||||
| 44 | isa => Maybe [Uri], | ||||||||||||||||||||
| 45 | clearer => '_clear_next_page_uri', | ||||||||||||||||||||
| 46 | coerce => 1, | ||||||||||||||||||||
| 47 | lazy => 1, | ||||||||||||||||||||
| 48 | 0 | ▶ | default => sub { $_[0]->content ? $_[0]->content->{next} : undef }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 2 | ); | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 2 | has raw => ( | |||||||||||||||||||
| 52 | is => 'ro', | ||||||||||||||||||||
| 53 | isa => InstanceOf ['HTTP::Response'], | ||||||||||||||||||||
| 54 | handles => { code => 'code' }, | ||||||||||||||||||||
| 55 | required => 1, | ||||||||||||||||||||
| 56 | clearer => '_clear_raw', | ||||||||||||||||||||
| 57 | writer => '_set_raw', | ||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 2 | has success => ( | |||||||||||||||||||
| 61 | is => 'ro', | ||||||||||||||||||||
| 62 | isa => Bool, | ||||||||||||||||||||
| 63 | lazy => 1, | ||||||||||||||||||||
| 64 | clearer => '_clear_success', | ||||||||||||||||||||
| 65 | builder => '_build_success', | ||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 0 | ▶ | sub _build_content { | ||||||||||||||||||
|
Subroutine
_build_content: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 69 | 0 | my $self = shift; | |||||||||||||||||||
| 70 | 0 | my $content = $self->raw->decoded_content; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 0 | ▶ | return $content ? decode_json($content) : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 0 | ▶ | sub _build_iterator { | ||||||||||||||||||
|
Subroutine
_build_iterator: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 76 | 0 | my $self = shift; | |||||||||||||||||||
| 77 | 0 | my $content = $self->content->{items}; | |||||||||||||||||||
| 78 | 0 | ▶ | $content = [$content] unless ref $content eq 'ARRAY'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 0 | return Array::Iterator->new($content); | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | ▶ | sub _build_success { | ||||||||||||||||||
|
Subroutine
_build_success: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 83 | 0 | my $self = shift; | |||||||||||||||||||
| 84 | 0 | ▶ | return $self->raw->is_success && !$self->raw->header('X-Died'); | ||||||||||||||||||
|
Condition0%0/3
$self->raw->is_success && !$self->raw->header('X-Died')
MC/DC0%0/2
$self->raw->is_success && !$self->raw->header('X-Died')
$self->raw->is_success
!$self->raw->header('X-Died')
Truth table0%0/3
A && not(B)
A= $self->raw->is_successB= !$self->raw->header('X-Died')
| |||||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 0 | ▶ | sub next { | ||||||||||||||||||
|
Subroutine
next: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 88 | 0 | my $self = shift; | |||||||||||||||||||
| 89 | 0 | my $row = $self->_iterator->get_next; | |||||||||||||||||||
| 90 | 0 | ▶ | return $row if $row; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 0 | ▶ | if ( $self->_auto_pagination ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 0 | my $result = $self->_next_page; | |||||||||||||||||||
| 93 | 0 | ▶ | return unless $result; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 0 | $self->_reset; | |||||||||||||||||||
| 95 | 0 | $self->_set_raw( $result->raw ); | |||||||||||||||||||
| 96 | 0 | return $self->_iterator->get_next; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | 0 | return; | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | ▶ | sub _next_page { | ||||||||||||||||||
|
Subroutine
_next_page: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 102 | 0 | my $self = shift; | |||||||||||||||||||
| 103 | 0 | ▶ | return unless $self->next_page_uri; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | 0 | return $self->_get->( $self->next_page_uri ); | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 0 | ▶ | sub _reset { | ||||||||||||||||||
|
Subroutine
_reset: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 108 | 0 | my $self = shift; | |||||||||||||||||||
| 109 | 0 | $self->_clear_raw; | |||||||||||||||||||
| 110 | 0 | $self->_clear_success; | |||||||||||||||||||
| 111 | 0 | $self->_clear_content; | |||||||||||||||||||
| 112 | 0 | $self->_clear_iterator; | |||||||||||||||||||
| 113 | 0 | $self->_clear_next_page_uri; | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 2 | 1; | |||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =pod | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =encoding UTF-8 | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =head1 NAME | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | WebService::HealthGraph::Response - Generic response object for WebService::HealthGraph | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =head1 VERSION | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | version 0.000004 | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =head1 CONSTRUCTOR ARGUMENTS | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =over | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | =item get | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | A C<CodeRef> which can be used to fetch subsequent pages. Only useful if | ||||||||||||||||||||
| 137 | C<auto_pagination> is C<true>. | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | =item raw | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | An L<HTTP::Response> object. | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | =back | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =head2 content | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | Returns either a C<HashRef> or an C<ArrayRef> of the content, depending on what | ||||||||||||||||||||
| 148 | the HealthGraph API returns. | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =head2 next | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | This method iterates over the items in the response content, returning one | ||||||||||||||||||||
| 153 | HashRef at a time. | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | =head2 next_page_uri | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | Returns the URL of the next page of results, in the form of a L<URI> object. | ||||||||||||||||||||
| 158 | Returns C<undef> if there is no next page. | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =head2 raw | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | Returns the raw L<HTTP::Response> object. | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =head2 success | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | Returns true if the HTTP request was fetched and parsed successfully. | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | =head1 AUTHOR | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | Olaf Alders <olaf@wundercounter.com> | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | This software is copyright (c) 2016 by Olaf Alders. | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 177 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | =cut | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | __END__ | ||||||||||||||||||||