File Coverage

blib/lib/Marketplace/Rakuten/Response.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Marketplace::Rakuten::Response;
2              
3 1     1   4 use strict;
  1         1  
  1         23  
4 1     1   3 use warnings;
  1         0  
  1         19  
5              
6 1     1   3 use Moo;
  1         1  
  1         5  
7 1     1   210 use MooX::Types::MooseLike::Base qw(Str Bool HashRef);
  1         1  
  1         46  
8 1     1   420 use XML::LibXML::Simple qw/XMLin/;
  0            
  0            
9             use Marketplace::Rakuten::Utils;
10             use Data::Dumper;
11             use namespace::clean;
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             has success => (is => 'ro', isa => Bool);
60             has url => (is => 'ro', isa => Str);
61             has status => (is => 'ro', isa => Str);
62             has reason => (is => 'ro', isa => Str);
63             has content => (is => 'ro', isa => Str);
64             has headers => (is => 'ro', isa => HashRef);
65              
66             has data => (is => 'lazy');
67             sub _build_data {
68             my $self = shift;
69             my $data;
70             if (my $xml = $self->content) {
71             eval { $data = XMLin($xml,
72             ForceArray => [ qw/error order/ ],
73             # prevent <name> to be used as key in the structure
74             KeyAttr => [],
75             ) };
76             warn "Faulty xml! $@" . $xml if $@;
77             }
78             Marketplace::Rakuten::Utils::turn_empty_hashrefs_into_empty_strings($data);
79             return $data;
80             }
81              
82             =head1 METHODS
83              
84             =head2 is_success
85              
86             Check that the http status is ok and that there are no errors.
87              
88             =cut
89              
90             sub is_success {
91             my $self = shift;
92             if ($self->success && !$self->errors &&
93             defined($self->data->{success}) &&
94             $self->data->{success} >= 0) {
95             return 1;
96             }
97             return 0;
98             }
99              
100             =head2 errors
101              
102             Return, if any, a list of hashrefs with errors. The I<expected> keys
103             of each element are: C<code> C<message> C<help>
104              
105             http://webservice.rakuten.de/documentation/howto/error
106              
107             Return an arrayref of error, if any. If there is no error, return
108             undef.
109              
110             =cut
111              
112             sub errors {
113             my $self = shift;
114             if (my $errors = $self->data->{errors}) {
115             if (my $error = $errors->{error}) {
116             return $error;
117             }
118             else {
119             die "Unexpected error content!" . Dumper($errors);
120             }
121             }
122             return undef;
123             }
124              
125             =head2 error_string
126              
127             The errors returned as a single string.
128              
129             =cut
130              
131             sub error_string {
132             my $self = shift;
133             if (my $errors = $self->errors) {
134             my @out;
135             foreach my $err (@$errors) {
136             my @pieces;
137             foreach my $k (qw/code message help/) {
138             push @pieces, $err->{$k} if $err->{$k};
139             }
140             if (@pieces) {
141             push @out, join(' ', @pieces);
142             }
143             }
144             if (@out) {
145             return join("\n", @out) . "\n";
146             }
147             }
148             return '';
149             }
150              
151             1;