Ebay/Response.pm

filter: Statementstmt 25.4% 28 / 110 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 31.0% 9 / 29 Podpod 100% 18 / 18 totaltotal 25.8% 55 / 213 SCAR 62.1
CC
29
Cov
18%
fees
110.0
errors_as_string
56.0
is_failure
20.0
CRAP 499.3
« Ebay.pm ↑ Summary Ebay/Order.pm »
1 package Marketplace::Ebay::Response;
2  
3 4 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Marketplace::Ebay::Response - Generic response parser for ebay api calls
17  
18 =head1 SYNOPSIS
19  
20 my $ebay = Marketplace::Ebay->new(...);
21 my $res = $ebay->api_call('GeteBayOfficialTime', {});
22 if (defined($res)) {
23 my $parsed = Marketplace::Ebay::Response->new(struct => $res);
24 print "OK" if $parsed->is_success;
25 if (defined $parsed->fees) {
26 print "Total fees are " . $parsed->total_fees;
27 }
28 }
29  
30 =head1 ACCESSORS
31  
32 The constructor asks for a C<struct> key where the C<api_call> return
33 value should be saved. This module provide some convenience routines.
34  
35 =head2 struct
36  
37 The XML data deserialized into a perl hashref. It should be the return
38 value of L<Marketplace::Ebay>'s api_call, if defined.
39  
40 =cut
41  
42 4 has struct => (is => 'ro',
43 isa => HashRef,
44 required => 1);
45  
46  
47 =head1 SHORTCUTS
48  
49 Given that we can't know beforehand which kind of response we have,
50 depending on the API version and on the used XSD, the convention for
51 all these shortcuts is to return undef when we can't reliably provide
52 an answer (this is true for booleans as well, which return 0 or 1).
53 C<undef> means that we don't know, so you're recommend to inspect the
54 C<struct> yourself to make sense of the unknown, if you're expecting
55 something.
56  
57 =head2 is_success
58  
59 Boolean.
60  
61 =head2 version
62  
63 The API version of the remote site.
64  
65 =head2 item_id
66  
67 The ItemID (if any) of the response
68  
69 =head2 ack
70  
71 The Ack key of the response (acknowledge)
72  
73 =head2 start_time
74  
75 The StartTime (if any) of the response (auction start time)
76  
77 =head2 start_time_dt
78  
79 The StartTime (if any) of the response (auction start time) as a
80 DateTime object.
81  
82 =head2 end_time
83  
84 The EndTime (if any) of the response (auction end time)
85  
86 =head2 end_time_dt
87  
88 The EndTime (if any) as a DateTime object
89  
90 =head2 timestamp
91  
92 The timestamp of the response.
93  
94 =head2 timestamp_dt
95  
96 The timestamp of the response as a DateTime object.
97  
98 =head2 errors
99  
100 The unmodified Errors structure
101  
102 =head2 errors_as_string
103  
104 A single string with the errors found in the response. If you need
105 more detailed info, you have to inspect C<errors> yourself.
106  
107 =head2 is_failure
108  
109 Return true if the acknowledge says C<Failure> or C<PartialFailure>.
110  
111 =head2 is_warning
112  
113 Return true if the acknowledge says C<Warning>.
114  
115 =head2 request_ok
116  
117 Return true if the response was successful or just with warnings.
118  
119 =head2 sku
120  
121 The SKU key of the response data.
122  
123 =cut
124  
125 sub ack {
126 0 ▶ return shift->_get_struct_key('Ack');
Subroutine ack: not called CC=1 SCAR=6.9
Pod documented
127 }
128  
129 sub is_success {
130 0 ▶ my $self = shift;
Subroutine is_success: not called CC=3 SCAR=24.8
Pod documented
131 0 my $ack = $self->ack;
132 0 ▶ return unless $ack;
Branch: 1 branch
BranchTrueFalseTotal
unless $ack 0 0 0
133 0 ▶ if ($ack eq 'Success') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ack eq 'Success') { } 0 0 0
134 0 return 1;
135 }
136 else {
137 0 return 0;
138 }
139 }
140  
141 sub is_warning {
142 0 ▶ my $self = shift;
Subroutine is_warning: not called CC=3 SCAR=24.8
Pod documented
143 0 my $ack = $self->ack;
144 0 ▶ return unless $ack;
Branch: 1 branch
BranchTrueFalseTotal
unless $ack 0 0 0
145 0 ▶ if ($ack eq 'Warning') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ack eq 'Warning') { } 0 0 0
146 0 return 1;
147 }
148 else {
149 0 return 0;
150 }
151 }
152  
153 sub is_failure {
154 0 ▶ my $self = shift;
Subroutine is_failure: not called CC=4 SCAR=30.0
Pod documented
155 0 my $ack = $self->ack;
156 0 ▶ return unless $ack;
Branch: 1 branch
BranchTrueFalseTotal
unless $ack 0 0 0
157 0 ▶ if ($ack eq 'Failure' or $ack eq 'PartialFailure') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ack eq 'Failure' or $ack eq 'PartialFailure') { } 0 0 0
Condition0%0/3
$ack eq "Failure" or $ack eq "PartialFailure"
l!l&&r!l&&!r
000
MC/DC0%0/2
$ack eq "Failure" or $ack eq "PartialFailure"
$ack eq "Failure" $ack eq "PartialFailure"
Truth table0%0/3
A or B
A= $ack eq "Failure"B= $ack eq "PartialFailure"
ABresult
1X1
011
000
158 0 return 1;
159 }
160 else {
161 0 return 0;
162 }
163 }
164  
165 sub request_ok {
166 0 ▶ my $self = shift;
Subroutine request_ok: not called CC=3 SCAR=24.8
Pod documented
167 0 ▶ if ($self->is_success || $self->is_warning) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_success or $self->is_warning) { } 0 0 0
Condition0%0/3
$self->is_success or $self->is_warning
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->is_success or $self->is_warning
$self->is_success $self->is_warning
Truth table0%0/3
A or B
A= $self->is_successB= $self->is_warning
ABresult
1X1
011
000
168 0 return 1;
169 }
170 else {
171 0 return 0;
172 }
173 }
174  
175 sub version {
176 0 ▶ return shift->_get_struct_key('Version');
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
177 }
178  
179 sub item_id {
180 0 ▶ return shift->_get_struct_key('ItemID');
Subroutine item_id: not called CC=1 SCAR=6.9
Pod documented
181 }
182  
183 sub start_time {
184 0 ▶ return shift->_get_struct_key('StartTime');
Subroutine start_time: not called CC=1 SCAR=6.9
Pod documented
185 }
186  
187 sub start_time_dt {
188 0 ▶ return shift->_dt_from_string('start_time');
Subroutine start_time_dt: not called CC=1 SCAR=6.9
Pod documented
189 }
190  
191 sub end_time {
192 0 ▶ return shift->_get_struct_key('EndTime');
Subroutine end_time: not called CC=1 SCAR=6.9
Pod documented
193 }
194  
195 sub end_time_dt {
196 0 ▶ return shift->_dt_from_string('end_time');
Subroutine end_time_dt: not called CC=1 SCAR=6.9
Pod documented
197 }
198  
199 sub _dt_from_string {
200 0 ▶ my ($self, $method) = @_;
Subroutine _dt_from_string: not called CC=2 SCAR=17.9
201 0 ▶ if (my $string = $self->$method) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $string = $self->$method) 0 0 0
202 0 return DateTime::Format::ISO8601->parse_datetime($string);
203 }
204 0 return;
205 }
206  
207 sub sku {
208 0 ▶ shift->_get_struct_key('SKU');
Subroutine sku: not called CC=1 SCAR=6.9
Pod documented
209 }
210  
211  
212 sub timestamp {
213 0 ▶ return shift->_get_struct_key('Timestamp');
Subroutine timestamp: not called CC=1 SCAR=6.9
Pod documented
214 }
215  
216 sub timestamp_dt {
217 0 ▶ return shift->_dt_from_string('timestamp');
Subroutine timestamp_dt: not called CC=1 SCAR=6.9
Pod documented
218 }
219  
220  
221 sub errors {
222 0 ▶ return shift->_get_struct_key('Errors');
Subroutine errors: not called CC=1 SCAR=6.9
Pod documented
223 }
224  
225 sub _get_struct_key {
226 0 ▶ my ($self, $key) = @_;
Subroutine _get_struct_key: not called CC=2 SCAR=17.9
227 0 my $struct = $self->struct;
228 0 ▶ if (exists $struct->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $struct->{$key}) 0 0 0
229 0 return $struct->{$key};
230 }
231 0 return;
232 }
233  
234 sub errors_as_string {
235 0 ▶ my $self = shift;
Subroutine errors_as_string: not called CC=7 SCAR=40.3
Pod documented
236 0 ▶ if (my $errors = $self->errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $errors = $self->errors) 0 0 0
237 0 ▶ if (ref($errors) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $errors eq 'ARRAY') { } 0 0 0
238 0 my @errors;
239 0 my $count = 0;
240 0 foreach my $error (@$errors) {
241 0 $count++;
242 0 my @err_details = ($count . '.');
243 0 foreach my $key (qw/SeverityCode
244 ErrorClassification
245 ErrorCode
246 ShortMessage
247 LongMessage/) {
248 0 ▶ if (exists $error->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $error->{$key}) 0 0 0
249 0 ▶ if (defined $error->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error->{$key}) 0 0 0
250 0 push @err_details, $error->{$key};
251 }
252 }
253 }
254 0 push @errors, join(' ', @err_details);
255 }
256 0 return join("\n", @errors) . "\n";
257 }
258 else {
259 0 die "Array expected! Please fix this code";
260 }
261 }
262 0 return;
263 }
264  
265 =head2 fees
266  
267 The fees detail returned by an add_item (or equivalent) call.
268  
269 =head2 total_listing_fee
270  
271 As per documentation: The total cost of all listing features is found
272 in the Fees container whose Name is ListingFee. This does not reflect
273 the full cost of listing and selling an item on eBay, for the Final
274 Value Fee cannot be calculated by eBay until the listing has ended,
275 when a final sale price is known. Total cost is then the sum of the
276 Final Value Fee and the Fee corresponding to ListingFee.
277  
278 L<http://developer.ebay.com/DevZone/guides/ebayfeatures/Development/Listing-Fees.html>
279  
280 =cut
281  
282 sub fees {
283 0 ▶ my $self = shift;
Subroutine fees: not called CC=10 SCAR=47.0
Pod documented
284 0 my $struct = $self->struct;
285 0 ▶ if (exists $struct->{Fees}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $struct->{"Fees"}) 0 0 0
286 0 ▶ if (exists $struct->{Fees}->{Fee}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $struct->{"Fees"}{"Fee"}) 0 0 0
287 0 my $fees = $struct->{Fees}->{Fee};
288 0 ▶ if ($fees && @$fees) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fees and @$fees) 0 0 0
Condition0%0/3
$fees and @$fees
!ll&&!rl&&r
000
MC/DC0%0/2
$fees and @$fees
$fees @$fees
Truth table0%0/3
A and B
A= $feesB= @$fees
ABresult
0X0
100
111
289 0 my %out;
290 0 FEE: foreach my $fee (@$fees) {
291 # we hope this structure is stable...
292 0 foreach my $k (qw/Name Fee/) {
293 0 ▶ unless (exists $fee->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $fee->{$k}) 0 0 0
294 0 warn "$k not found in fee:" . Dumper($fee);
295 0 next FEE;
296 }
297 }
298 0 ▶ $out{$fee->{Name}} ||= 0;
Condition0%0/2
$out{$fee->{"Name"}} ||= 0
l!l
00
MC/DC0%0/1
$out{$fee->{"Name"}} ||= 0
$out{$fee->{"Name"}}
Truth table0%0/2
A
A= $out{$fee->{"Name"}}
Aresult
11
00
299 0 $out{$fee->{Name}} += $fee->{Fee}->{_};
300 }
301 0 foreach my $k (keys %out) {
302 0 my $float = $out{$k};
303 0 $out{$k} = sprintf('%.2f', $float);
304 }
305 0 return \%out;
306 }
307 }
308 }
309 0 return;
310 }
311  
312 sub total_listing_fee {
313 0 ▶ my $self = shift;
Subroutine total_listing_fee: not called CC=3 SCAR=24.8
Pod documented
314 0 ▶ if (my $fees = $self->fees) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fees = $self->fees) 0 0 0
315 0 ▶ if (exists $fees->{ListingFee}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fees->{"ListingFee"}) 0 0 0
316 0 return $fees->{ListingFee};
317 }
318 }
319 0 return;
320 }
321  
322  
323 4 1;
« Ebay.pm ↑ Summary Ebay/Order.pm »