Amazon/Property.pm

filter: Statementstmt 98.3% 118 / 120 Branchbran 92.5% 37 / 40 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 4 totaltotal 95.1% 178 / 187 CC 26 SCAR 18.1
CC
26
Cov
97%
new
11.0
factory
10.0
_best_effort_price
4.0
CRAP 6.1
« Amazon.pm ↑ Summary Amazon/Request.pm »
1 ######################################################################
2 package Net::Amazon::Property;
3 ######################################################################
4 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 24 ▶use base qw(Net::Amazon);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 24 ▶use Net::Amazon::Property::Book;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use Net::Amazon::Property::CE;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use Net::Amazon::Property::DigitalMusicTrack;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 24 ▶use Net::Amazon::Property::DVD;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 24 ▶use Net::Amazon::Property::Music;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 24 ▶use Net::Amazon::Property::Software;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 24 ▶use Net::Amazon::Property::VideoGames;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 24 ▶use Net::Amazon::Attribute::ReviewSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 24 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 24 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # read: a poor man's XPath
21 # NOTE: Igor Sutton Lopes has a module called Hash::Path that does exactly
22 # what I am doing here. He beat me to the punch. :)
23 24 our %DEFAULT_ATTRIBUTES_XPATH = (
24 Availability => [qw(Offers Offer OfferListing Availability)],
25 Catalog => [qw(ItemAttributes ProductGroup)],
26 Binding => [qw(ItemAttributes Binding)],
27 CollectibleCount => [qw(OfferSummary TotalCollectible)],
28 CollectiblePrice => [qw(OfferSummary LowestCollectiblePrice FormattedPrice)],
29 ImageUrlLarge => [qw(LargeImage URL)],
30 LargeImageUrl => [qw(LargeImage URL)],
31 LargeImageWidth => [qw(LargeImage Width content)],
32 LargeImageHeight => [qw(LargeImage Height content)],
33 ListPrice => [qw(ItemAttributes ListPrice FormattedPrice)],
34 Manufacturer => [qw(ItemAttributes Manufacturer)],
35 MediumImageUrl => [qw(MediumImage URL)],
36 ImageUrlMedium => [qw(MediumImage URL)],
37 MediumImageWidth => [qw(MediumImage Width content)],
38 MediumImageHeight => [qw(MediumImage Height content)],
39 OurPrice => [qw(Offers Offer OfferListing Price FormattedPrice)],
40 ImageUrlSmall => [qw(SmallImage URL)],
41 SmallImageUrl => [qw(SmallImage URL)],
42 SmallImageWidth => [qw(SmallImage Width content)],
43 SmallImageHeight => [qw(SmallImage Height content)],
44 SuperSaverShipping => [qw(Offers Offer OfferListing IsEligibleForSuperSaverShipping)],
45 Title => [qw(ItemAttributes Title)],
46 title => [qw(ItemAttributes Title)],
47 ThirdPartyNewCount => [qw(OfferSummary TotalNew)],
48 ThirdPartyNewPrice => [qw(OfferSummary LowestNewPrice FormattedPrice)],
49 TotalOffers => [qw(Offers TotalOffers)],
50 UsedCount => [qw(OfferSummary TotalUsed)],
51 UsedPrice => [qw(OfferSummary LowestUsedPrice FormattedPrice)],
52 RawListPrice => [qw(ItemAttributes ListPrice Amount)],
53 CurrencyCode => [qw(ItemAttributes ListPrice CurrencyCode)],
54 ReleaseDate => [qw(ItemAttributes ReleaseDate)],
55 SimilarProducts => [qw(SimilarProducts SimilarProduct)],
56 );
57  
58 24 our @DEFAULT_ATTRIBUTES = qw(
59 SalesRank ASIN DetailPageURL ProductDescription
60 NumMedia NumberOfOfferings
61 );
62  
63 24 our %COMPATIBLE_ATTRIBUTES = (
64 'Asin' => 'ASIN',
65 'url' => 'DetailPageURL',
66 'Media' => 'Binding',
67 'ProductName' => 'title',
68 );
69  
70 24 __PACKAGE__->make_accessor($_) for @DEFAULT_ATTRIBUTES;
71 24 __PACKAGE__->make_accessor($_) for keys %DEFAULT_ATTRIBUTES_XPATH;
72 24 __PACKAGE__->make_accessor($_) for qw(year review_set image_set offer_set);
73 24 __PACKAGE__->make_array_accessor($_) for qw(browse_nodes similar_asins);
74 24 __PACKAGE__->make_compatible_accessor($_, $COMPATIBLE_ATTRIBUTES{$_}) for keys %COMPATIBLE_ATTRIBUTES;
75  
76 ##################################################
77 395 ▶sub new {
Subroutine new: called CC=11 SCAR=24.0
Pod undocumented
78 ##################################################
79 395 my($class, %options) = @_;
80  
81 395 ▶ if(!$options{xmlref}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"xmlref"}) 0 395 395
82 0 die "Mandatory param xmlref missing";
83 }
84  
85 395 my $self = {
86 %options,
87 };
88  
89 395 bless $self, $class;
90  
91 # Set default attributes
92 395 for my $attr (@DEFAULT_ATTRIBUTES) {
93 2370 $self->$attr($options{xmlref}->{$attr});
94 }
95  
96 395 for my $attr (keys %DEFAULT_ATTRIBUTES_XPATH) {
97 12640 my $value = __PACKAGE__->walk_hash_ref($options{xmlref}, $DEFAULT_ATTRIBUTES_XPATH{$attr});
98 12640 $self->$attr($value);
99 }
100  
101 395 ▶ if (defined $options{xmlref}->{OfferSummary}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"xmlref"}{"OfferSummary"}) 384 11 395
102 384 $self->NumberOfOfferings($self->UsedCount() + $self->ThirdPartyNewCount() + $self->CollectibleCount());
103 }
104 395 ▶ if (defined $options{xmlref}->{EditorialReviews}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"xmlref"}{"EditorialReviews"}) 1 394 395
105 1 $self->ProductDescription($options{xmlref}->{EditorialReviews}->{EditorialReview}->[0]->{Content});
106 }
107  
108 395 my @browse_nodes;
109 395 ▶ if (ref $options{xmlref}->{BrowseNodes}->{BrowseNode} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $options{"xmlref"}{"BrowseNodes"}{"BrowseNode"} eq "ARRAY") 15 380 395
110 15 for my $bn (@{$options{xmlref}->{BrowseNodes}->{BrowseNode}}) {
111 65 push @browse_nodes, $bn->{Name};
112  
113 # Walk the BrowseNode Ancestors and collect the other BrowseNode Ids
114 65 ▶ for (my $ref = $bn->{Ancestors}->{BrowseNode}; defined $ref; $ref = $ref->{Ancestors}->{BrowseNode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref) 220 65 285
115 # The BrowseNodeId is also available...
116 220 ▶ push @browse_nodes, $ref->{Name} if defined($ref->{Name});
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref->{"Name"} 205 15 220
117 }
118 }
119 }
120 395 $self->browse_nodes(\@browse_nodes);
121  
122 395 my @similars;
123 395 for my $similar (@{$options{xmlref}->{SimilarProducts}->{SimilarProduct}}) {
124 # You could also capture the Title as well...
125 75 push @similars, $similar->{ASIN};
126 }
127 395 $self->similar_asins(\@similars);
128
129 395 return $self;
130 }
131  
132 ##################################################
133 92 ▶sub as_string {
Subroutine as_string: called CC=3 SCAR=11.1
Pod undocumented
134 ##################################################
135 92 my($self) = @_;
136  
137 92 my $result = "\"" . $self->Title . "\", ";
138  
139 92 ▶ if($self->{xmlref}->{Manufacturer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"xmlref"}{"Manufacturer"}) 0 92 92
140 0 $result .= "$self->{xmlref}->{Manufacturer}, ";
141 }
142  
143 92 ▶ $result .= $self->year() . ", " if $self->year();
Branch: 1 branch
BranchTrueFalseTotal
if $self->year 1 91 92
144  
145 92 $result .= $self->_best_effort_price() . ", ";
146 92 $result .= $self->ASIN();
147  
148 92 return $result;
149 }
150  
151 ##################################################
152 225 ▶sub _best_effort_price {
Subroutine _best_effort_price: called CC=4 SCAR=13.9
153 ##################################################
154 225 my($self) = @_;
155  
156 225 my $price;
157 225 ▶ if ($self->OurPrice()) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->OurPrice) { } 150 75 225
elsif ($self->ThirdPartyNewPrice) { } 18 57 75
elsif ($self->UsedPrice) { } 12 45 57
158 150 $price = $self->OurPrice();
159 } elsif ($self->ThirdPartyNewPrice()) {
160 18 $price = $self->ThirdPartyNewPrice();
161 } elsif ($self->UsedPrice()) {
162 12 $price = $self->UsedPrice();
163 } else {
164 45 $price = '[$unknown]';
165 }
166  
167 225 return $price;
168 }
169  
170  
171 ##################################################
172 395 ▶sub factory {
Subroutine factory: called CC=10 SCAR=23.0
Pod undocumented
173 ##################################################
174 395 my(%options) = @_;
175  
176 395 my $xmlref = $options{xmlref};
177 395 ▶ die "Called factory without xmlref" unless $xmlref;
Branch: 1 branch
BranchTrueFalseTotal
unless $xmlref 0 395 395
178  
179 #DEBUG(sub {"factory xmlref=" . Data::Dumper::Dumper($xmlref)});
180  
181 395 my $catalog = $xmlref->{ItemAttributes}->{ProductGroup};
182 395 my $obj;
183  
184 395 ▶ if(0) {
Branch: 7 branches
BranchTrueFalseTotal
if (do { $catalog eq 'Book' or $catalog eq 'eBooks' }) { } 153 242 395
elsif ($catalog eq 'Music') { } 70 172 242
elsif ($catalog eq 'DVD') { } 48 124 172
elsif ($catalog eq 'Software') { } 8 116 124
elsif ($catalog eq 'Video Games') { } 14 102 116
elsif ($catalog eq 'CE') { } 6 96 102
elsif ($catalog eq 'Digital Music Track') { } 30 66 96
185 ▶ } elsif($catalog eq "Book" || $catalog eq "eBooks") {
Condition100%3/3
$catalog eq "Book" or $catalog eq "eBooks"
l!l&&r!l&&!r
14211242
MC/DC100%2/2
$catalog eq "Book" or $catalog eq "eBooks"
$catalog eq "Book" $catalog eq "eBooks"
Truth table100%3/3
A or B
A= $catalog eq "Book"B= $catalog eq "eBooks"
ABresult
1X1
011
000
186 153 DEBUG("Creating new Book Property");
187 153 $obj = Net::Amazon::Property::Book->new(xmlref => $xmlref);
188 } elsif($catalog eq "Music") {
189 70 DEBUG("Creating new Music Property");
190 70 $obj = Net::Amazon::Property::Music->new(xmlref => $xmlref);
191 } elsif($catalog eq "DVD") {
192 48 DEBUG("Creating new DVD Property");
193 48 $obj = Net::Amazon::Property::DVD->new(xmlref => $xmlref);
194 } elsif($catalog eq "Software") {
195 8 DEBUG("Creating new Software Property");
196 8 $obj = Net::Amazon::Property::Software->new(xmlref => $xmlref);
197 } elsif($catalog eq "Video Games") {
198 14 DEBUG("Creating new Video Games Property");
199 14 $obj = Net::Amazon::Property::VideoGames->new(xmlref => $xmlref);
200 } elsif($catalog eq "CE") { # Consumer Electronics?
201 6 DEBUG("Creating new CE Property");
202 6 $obj = Net::Amazon::Property::CE->new(xmlref => $xmlref);
203 } elsif($catalog eq "Digital Music Track") {
204 30 DEBUG("Creating new Digital Music Track");
205 30 $obj = Net::Amazon::Property::DigitalMusicTrack->new(xmlref => $xmlref);
206 } else {
207 # print "UNKNOWN CATALOG: ", Data::Dumper::Dumper($xmlref), "\n";
208 # die "%Error: there is no property defined for type '$catalog'\n";
209 66 DEBUG("Creating new Default Property ($catalog)");
210 66 $obj = Net::Amazon::Property->new(xmlref => $xmlref);
211 }
212  
213 395 return $obj;
214 }
215  
216 ##################################################
217 329 ▶sub init_via_xmlref {
Subroutine init_via_xmlref: called CC=2 SCAR=6.9
Pod undocumented
218 ##################################################
219 329 my($self, $xmlref) = @_;
220  
221 329 my $reviewset = Net::Amazon::Attribute::ReviewSet->new();
222  
223 329 ▶ if(exists $xmlref->{CustomerReviews}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $xmlref->{"CustomerReviews"}) 1 328 329
224 1 $reviewset->init_via_xmlref($xmlref->{CustomerReviews});
225 }
226  
227 329 $self->review_set($reviewset);
228 }
229  
230  
231 ##################################################
232 ##################################################
233  
234  
235 ##################################################
236  
237 24 1;
238  
239 __END__
« Amazon.pm ↑ Summary Amazon/Request.pm »