1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
######################################################################
package Net::Amazon::Response::Blended;
######################################################################
use warnings;
use strict;
use base qw(Net::Amazon::Response);
use Net::Amazon::Property;
use XML::Simple;
our @FORCE_ARRAY_FIELDS = qw(ProductLine);
##################################################
sub new {
##################################################
my($class, %options) = @_;
my $self = $class->SUPER::new(%options);
bless $self, $class; # reconsecrate
}
##################################################
sub as_string {
##################################################
my($self) = @_;
return $self->SUPER::list_as_string($self->properties);
}
##################################################
sub xmlref_add {
##################################################
my($self, $xmlref) = @_;
my $nof_items_added = 0;
unless(ref($self->{xmlref}) eq "HASH" &&
ref($self->{xmlref}->{Details}) eq "ARRAY") {
$self->{xmlref}->{Details} = [];
}
if ($xmlref->{ProductLine} && (ref($xmlref->{ProductLine}) eq "ARRAY")) {
my @lines = @{$xmlref->{ProductLine}}; # Copy the lines
# sort the copies by relevance
@lines = sort { $a->{RelevanceRank} <=> $b->{RelevanceRank} } @lines;
foreach (@lines) {
next unless $_->{ProductInfo}->{Details};
my $details = $_->{ProductInfo}->{Details};
if (ref($details) eq "ARRAY") {
push @{$self->{xmlref}->{Details}}, @$details;
$nof_items_added += scalar @$details;
} else {
push @{$self->{xmlref}->{Details}}, $details;
$nof_items_added++;
}
}
}
return $nof_items_added;
}
##################################################
sub xml_parse {
##################################################
my($self, $xml) = @_;
my $xs = XML::Simple->new();
return $xs->XMLin($xml, ForceArray => [ @FORCE_ARRAY_FIELDS ]);
}
1;
|