| 1 | package WebService::8tracks::Response; | ||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use HTTP::Status (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | =pod | ||||||||||
| 7 | |||||||||||
| 8 | =head1 NAME | ||||||||||
| 9 | |||||||||||
| 10 | WebService::8tracks::Response - Thin wrapper of 8tracks API response hash | ||||||||||
| 11 | |||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||
| 13 | |||||||||||
| 14 | my $res = $api->user_mixes('dp'); # isa WebService::8tracks::Response | ||||||||||
| 15 | |||||||||||
| 16 | # Currently only is_success/is_error/is_*_error are provided | ||||||||||
| 17 | $res->is_success or die $res->{status}; | ||||||||||
| 18 | |||||||||||
| 19 | # Access data via as normal hashref | ||||||||||
| 20 | my @mixes = @{ $res->{mixes} }; | ||||||||||
| 21 | |||||||||||
| 22 | =cut | ||||||||||
| 23 | |||||||||||
| 24 | 21 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 25 | 21 | my ($class, $args) = @_; | |||||||||
| 26 | 21 | return bless $args, $class; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 21 | ▶ | sub _status_code { | ||||||||
|
Subroutine
_status_code: called CC=2 SCAR=7.1
| |||||||||||
| 30 | 21 | my $self = shift; | |||||||||
| 31 | 21 | ▶ | $self->{status} =~ /^(\d+)/ or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 21 | return $1; | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | =head1 METHODS | ||||||||||
| 36 | |||||||||||
| 37 | =over 4 | ||||||||||
| 38 | |||||||||||
| 39 | =item is_success | ||||||||||
| 40 | |||||||||||
| 41 | =item is_error | ||||||||||
| 42 | |||||||||||
| 43 | =item is_client_error | ||||||||||
| 44 | |||||||||||
| 45 | =item is_server_error | ||||||||||
| 46 | |||||||||||
| 47 | Returns whether API response has corresponding status. Uses HTTP::Status internally. | ||||||||||
| 48 | |||||||||||
| 49 | =back | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | |||||||||||
| 53 | 20 | ▶ | sub is_success { | ||||||||
|
Subroutine
is_success: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 54 | 20 | my $self = shift; | |||||||||
| 55 | 20 | ▶ | my $code = $self->_status_code or return 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 20 | return HTTP::Status::is_success($code); | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 3 | foreach my $is_error (qw(is_error is_client_error is_server_error)) { | |||||||||
| 60 | 1 | ▶ | my $code = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||
| 61 | 1 | my $self = shift; | |||||||||
| 62 | 1 | ▶ | my $code = $self->_status_code or return 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 1 | return HTTP::Status->can($is_error)->($code); | |||||||||
| 64 | 9 | }; | |||||||||
| 65 | 3 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 66 | 9 | *$is_error = $code; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 3 | 1; | |||||||||
| 70 | |||||||||||
| 71 | __END__ | ||||||||||