| 1 | package Marketplace::Rakuten::Response; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use MooX::Types::MooseLike::Base qw(Str Bool HashRef); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use XML::LibXML::Simple qw/XMLin/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Marketplace::Rakuten::Utils; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | Marketplace::Rakuten::Response - Class to handle the responses from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | webservice.rakuten.de | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | The constructors keys map the L<HTTP::Tiny> keys, so you can feed the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | constructor straight with the response. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | =head1 ACCESSORS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | =head2 success | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | Boolean indicating whether the operation returned a 2XX status code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | =head2 url | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | URL that provided the response. This is the URL of the request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | unless there were redirections, in which case it is the last URL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | queried in a redirection chain | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | =head2 status | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | The HTTP status code of the response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | =head2 reason | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | The response phrase returned by the server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | =head2 content | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | The body of the response. If the response does not have any content or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | if a data callback is provided to consume the response body, this will | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | be the empty string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | =head2 headers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | A hashref of header fields. All header field names will be normalized | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | to be lower case. If a header is repeated, the value will be an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | arrayref; it will otherwise be a scalar string containing the value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | =head2 data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | The parsed data from xml, if any. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 4 | has success => (is => 'ro', isa => Bool); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 4 | has url => (is => 'ro', isa => Str); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 4 | has status => (is => 'ro', isa => Str); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 4 | has reason => (is => 'ro', isa => Str); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 4 | has content => (is => 'ro', isa => Str); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 4 | has headers => (is => 'ro', isa => HashRef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 4 | has data => (is => 'lazy'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | sub _build_data { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_data: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 4 | my $data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 4 | ▶ | if (my $xml = $self->content) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 4 | eval { $data = XMLin($xml, ForceArray => [ qw/error order/ ]) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 4 | ▶ | warn "Faulty xml! $@" . $xml if $@; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 4 | Marketplace::Rakuten::Utils::turn_empty_hashrefs_into_empty_strings($data); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | return $data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | =head2 is_success | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | Check that the http status is ok and that there are no errors. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | sub is_success { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_success: called CC=5 SCAR=21.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 2 | ▶ | if ($self->success && !$self->errors && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
$self->success and not $self->errors and defined $self->data->{"success"} and $self->data->{"success"} >= 0
$self->success and not $self->errors and defined $self->data->{"success"}
$self->success and not $self->errors
MC/DC0%0/4
$self->success and not $self->errors and defined $self->data->{"success"} and $self->data->{"success"} >= 0
$self->success
not $self->errors
defined $self->data->{"success"}
$self->data->{"success"} >= 0
Truth table20%1/5
A and not(B) and C and D
A= $self->successB= not $self->errorsC= defined $self->data->{"success"}D= $self->data->{"success"} >= 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | defined($self->data->{success}) && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | $self->data->{success} >= 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 2 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | =head2 errors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | Return, if any, a list of hashrefs with errors. The I<expected> keys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | of each element are: C<code> C<message> C<help> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | http://webservice.rakuten.de/documentation/howto/error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | Return an arrayref of error, if any. If there is no error, return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | undef. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | sub errors { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
errors: called CC=3 SCAR=12.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | ▶ | if (my $errors = $self->data->{errors}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | ▶ | if (my $error = $errors->{error}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | return $error; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | die "Unexpected error content!" . Dumper($errors); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | =head2 error_string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | The errors returned as a single string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | sub error_string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error_string: called CC=7 SCAR=20.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | ▶ | if (my $errors = $self->errors) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | my @out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 1 | foreach my $err (@$errors) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | my @pieces; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 1 | foreach my $k (qw/code message help/) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 3 | ▶ | push @pieces, $err->{$k} if $err->{$k}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | ▶ | if (@pieces) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | push @out, join(' ', @pieces); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | ▶ | if (@out) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 1 | return join("\n", @out) . "\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | return ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||