Amazon.pm

filter: Statementstmt 44.3% 78 / 176 Branchbran 15.8% 13 / 82 Conditioncond 30.4% 7 / 23 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 76.4% 13 / 17 Podpod 57.1% 4 / 7 totaltotal 36.5% 115 / 315 SCAR 66.8
CC
52
Cov
35%
search
306.0
asin
131.3
new
35.0
CRAP 798.9
↑ Summary Amazon/Item.pm »
1 package XML::Amazon;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use LWP::Simple qw ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use XML::Amazon::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use XML::Amazon::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper qw ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use URI::Escape qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Digest::SHA qw(hmac_sha256_hex hmac_sha256_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = '0.12';
16  
17 sub new{
18 1 ▶ my($pkg, %options) = @_;
Subroutine new: called CC=17 SCAR=35.5
Pod documented
19 1 ▶ die 'No Access Key ID provided!' unless $options{'token'};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"token"} 0 1 1
20 1 ▶ die 'No Secret Access Key provided!' unless $options{'sak'};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"sak"} 0 1 1
21 1 my $locale = $options{'locale'};
22 1 ▶ $locale ||= "us";
Condition50%1/2
$locale ||= "us"
l!l
10
MC/DC0%0/1
$locale ||= "us"
$locale
Truth table50%1/2
A
A= $locale
Aresult
11
00
23 1 ▶ die "Invalid locale" unless $locale eq "jp" || $locale eq "uk" || $locale eq "fr" || $locale eq "us" || $locale eq "de"|| $locale eq "ca";
Branch: 1 branch
BranchTrueFalseTotal
unless $locale eq "jp" or $locale eq "uk" or $locale eq "fr" or $locale eq "us" or $locale eq "de" or $locale eq "ca" 0 1 1
Condition33%5/15
$locale eq "jp" or $locale eq "uk" or $locale eq "fr" or $locale eq "us" or $locale eq "de" or $locale eq "ca"
l!l&&r!l&&!r
100
$locale eq "jp" or $locale eq "uk" or $locale eq "fr" or $locale eq "us" or $locale eq "de"
l!l&&r!l&&!r
100
$locale eq "jp" or $locale eq "uk" or $locale eq "fr" or $locale eq "us"
l!l&&r!l&&!r
100
$locale eq "jp" or $locale eq "uk" or $locale eq "fr"
l!l&&r!l&&!r
100
$locale eq "jp" or $locale eq "uk"
l!l&&r!l&&!r
010
MC/DC0%0/6
$locale eq "jp" or $locale eq "uk" or $locale eq "fr" or $locale eq "us" or $locale eq "de" or $locale eq "ca"
$locale eq "jp" $locale eq "uk" $locale eq "fr" $locale eq "us" $locale eq "de" $locale eq "ca"
Truth table14%1/7
A or B or C or D or E or F
A= $locale eq "jp"B= $locale eq "uk"C= $locale eq "fr"D= $locale eq "us"E= $locale eq "de"F= $locale eq "ca"
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
24 1 ▶ my $associate = $options{'associate'} || 'webservices-20';
Condition50%1/2
$options{'associate'} || 'webservices-20'
l!l
01
MC/DC0%0/1
$options{'associate'} || 'webservices-20'
$options{'associate'}
Truth table50%1/2
A
A= $options{'associate'}
Aresult
11
00
25
26 1 my $url;
27
28 1 ▶ $url = 'ecs.amazonaws.jp' if $locale eq "jp";
Branch: 1 branch
BranchTrueFalseTotal
if $locale eq "jp" 0 1 1
29 1 ▶ $url = 'ecs.amazonaws.co.uk' if $locale eq "uk";
Branch: 1 branch
BranchTrueFalseTotal
if $locale eq "uk" 1 0 1
30 1 ▶ $url = 'ecs.amazonaws.fr' if $locale eq "fr";
Branch: 1 branch
BranchTrueFalseTotal
if $locale eq "fr" 0 1 1
31 1 ▶ $url = 'ecs.amazonaws.de' if $locale eq "de";
Branch: 1 branch
BranchTrueFalseTotal
if $locale eq "de" 0 1 1
32 1 ▶ $url = 'ecs.amazonaws.ca' if $locale eq "ca";
Branch: 1 branch
BranchTrueFalseTotal
if $locale eq "ca" 0 1 1
33 1 ▶ $url = 'ecs.amazonaws.com' if $locale eq "us";
Branch: 1 branch
BranchTrueFalseTotal
if $locale eq "us" 0 1 1
34
35
36 my $req = {
37 'Service' => 'AWSECommerceService',
38 1 'AWSAccessKeyId' => $options{'token'},
39 'AssociateTag' => $associate,
40 'Version' => '2009-03-31',
41 'Timestamp' => strftime( "%Y-%m-%dT%TZ", gmtime() )
42 };
43
44 bless{
45 token => $options{'token'},
46 1 sak => $options{'sak'},
47 associate => $associate,
48 locale => $locale,
49 url => $url,
50 req => $req,
51 data => undef,
52 success => '0'
53 }, $pkg;
54 }
55  
56 sub get{
57 0 ▶ my $self = shift;
Subroutine get: not called CC=3 SCAR=24.8
Pod undocumented
58 0 my $type = shift;
59 0 my $query = shift;
60 0 my $field = shift;
61 0 ▶ $self->asin($query) if $type eq "asin";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "asin" 0 0 0
62 0 ▶ $self->search($query,$field) if $type eq "search";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "search" 0 0 0
63 }
64  
65 sub asin{
66 1 ▶ my $self = shift;
Subroutine asin: called CC=15 SCAR=48.8
Pod documented
67 1 my $asin = shift;
68 1 my $url = $self->{url};
69 1 my $ITEM = XML::Amazon::Item->new();
70
71 1 ▶ warn 'Apparently not an appropriate ASIN' if $asin =~ /[^a-zA-Z0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
if $asin =~ /[^a-zA-Z0-9]/ 0 1 1
72
73 1 my %params = %{$self->{req}};
74
75 1 $params{'Operation'} = 'ItemLookup';
76 1 $params{'ResponseGroup'} = 'Images,ItemAttributes';
77 1 $params{'ItemId'} = $asin;
78 1 $params{'Version'} = '2009-03-31';
79
80 1 my $data = $self->get_data(\%params);
81
82 1 my $xs = new XML::Simple(SuppressEmpty => undef, ForceArray => ['Creator', 'Author', 'Artist', 'Director', 'Actor']);
83 1 my $pl = $xs->XMLin($data);
84 0 $self->{data} = $pl;
85
86 0 ▶ if ($pl->{Items}->{Item}->{ASIN}){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{'Items'}{'Item'}{'ASIN'}) { } 0 0 0
87 0 $ITEM->{asin} = $pl->{Items}->{Item}->{ASIN};
88 0 $ITEM->{title} = $pl->{Items}->{Item}->{ItemAttributes}->{Title};
89 0 $ITEM->{type} = $pl->{Items}->{Item}->{ItemAttributes}->{ProductGroup};
90
91  
92 0 ▶ if ($pl->{Items}->{Item}->{ItemAttributes}->{Author}->[0]){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}{"ItemAttributes"}{"Author"}[0]) 0 0 0
93 0 ▶ for (my $i = 0; $pl->{Items}->{Item}->{ItemAttributes}->{Author}->[$i]; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}{"ItemAttributes"}{"Author"}[$i]) 0 0 0
94 0 $ITEM->{authors}->[$i] = $pl->{Items}->{Item}->{ItemAttributes}->{Author}->[$i];
95 }
96 }
97
98 0 ▶ if ($pl->{Items}->{Item}->{ItemAttributes}->{Artist}->[0]){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}{"ItemAttributes"}{"Artist"}[0]) 0 0 0
99 0 ▶ for (my $i = 0; $pl->{Items}->{Item}->{ItemAttributes}->{Artist}->[$i]; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}{"ItemAttributes"}{"Artist"}[$i]) 0 0 0
100 0 $ITEM->{artists}->[$i] = $pl->{Items}->{Item}->{ItemAttributes}->{Artist}->[$i];
101 }
102 }
103 0 ▶ if ($pl->{Items}->{Item}->{ItemAttributes}->{Actor}->[0]){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}{"ItemAttributes"}{"Actor"}[0]) 0 0 0
104 0 ▶ for (my $i = 0; $pl->{Items}->{Item}->{ItemAttributes}->{Actor}->[$i]; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}{"ItemAttributes"}{"Actor"}[$i]) 0 0 0
105 0 $ITEM->{actors}->[$i] = $pl->{Items}->{Item}->{ItemAttributes}->{Actor}->[$i];
106 }
107 }
108
109 0 ▶ if ($pl->{Items}->{Item}->{ItemAttributes}->{Director}->[0]){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}{"ItemAttributes"}{"Director"}[0]) 0 0 0
110 0 ▶ for (my $i = 0; $pl->{Items}->{Item}->{ItemAttributes}->{Director}->[$i]; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}{"ItemAttributes"}{"Director"}[$i]) 0 0 0
111 0 $ITEM->{directors}->[$i] = $pl->{Items}->{Item}->{ItemAttributes}->{Director}->[$i];
112 }
113 }
114  
115
116 0 ▶ if ($pl->{Items}->{Item}->{ItemAttributes}->{Creator}->[0]->{content}){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}{"ItemAttributes"}{"Creator"}[0]{"content"}) 0 0 0
117 0 ▶ for (my $i = 0; $pl->{Items}->{Item}->{ItemAttributes}->{Creator}->[$i]->{content}; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}{"ItemAttributes"}{"Creator"}[$i]{"content"}) 0 0 0
118 0 $ITEM->{creators}->[$i] = $pl->{Items}->{Item}->{ItemAttributes}->{Creator}->[$i]->{content};
119 }
120 }
121  
122
123 0 $ITEM->{price} = $pl->{Items}->{Item}->{ItemAttributes}->{ListPrice}->{FormattedPrice};
124 0 $ITEM->{author} = $ITEM->{authors}->[0];
125 0 $ITEM->{url} = $pl->{Items}->{Item}->{DetailPageURL};
126 0 $ITEM->{publisher} = $pl->{Items}->{Item}->{ItemAttributes}->{Publisher};
127 0 $ITEM->{smallimage} = $pl->{Items}->{Item}->{SmallImage}->{URL};
128 0 $ITEM->{mediumimage} = $pl->{Items}->{Item}->{MediumImage}->{URL};
129 0 ▶ $ITEM->{mediumimage} = $ITEM->{smallimage} unless $ITEM->{mediumimage};
Branch: 1 branch
BranchTrueFalseTotal
unless $ITEM->{"mediumimage"} 0 0 0
130 0 $ITEM->{largeimage} = $pl->{Items}->{Item}->{LargeImage}->{URL};
131 0 ▶ $ITEM->{largeimage} = $ITEM->{mediumimage} unless $ITEM->{largeimage};
Branch: 1 branch
BranchTrueFalseTotal
unless $ITEM->{"largeimage"} 0 0 0
132
133 0 $self->{success} = '1';
134 0 return $ITEM;
135 }
136 else{
137 0 $self->{success} = '0';
138 0 warn 'No item found';
139 0 return '';
140 }
141 }
142  
143 sub search{
144 0 ▶ my($self, %options) = @_;
Subroutine search: not called CC=17 SCAR=57.2
Pod documented
145 0 my $keywords = $options{'keywords'};
146 0 ▶ my $type = $options{'type'} || "Blended";
Condition0%0/2
$options{'type'} || 'Blended'
l!l
00
MC/DC0%0/1
$options{'type'} || 'Blended'
$options{'type'}
Truth table0%0/2
A
A= $options{'type'}
Aresult
11
00
147 0 ▶ my $page = $options{'page'} || 1;
Condition0%0/2
$options{'page'} || 1
l!l
00
MC/DC0%0/1
$options{'page'} || 1
$options{'page'}
Truth table0%0/2
A
A= $options{'page'}
Aresult
11
00
148
149 0 my %params = %{$self->{req}};
150
151 0 $params{'Operation'} = 'ItemSearch';
152 0 $params{'SearchIndex'} = $type;
153 0 $params{'ResponseGroup'} = 'Images,ItemAttributes';
154 0 $params{'Keywords'} = $keywords;
155 0 $params{'ItemPage'} = $page;
156
157 0 my $data = $self->get_data(\%params);
158
159 0 my $xs = new XML::Simple(SuppressEmpty => undef, ForceArray => ['Item', 'Creator', 'Author', 'Artist', 'Actor', 'Director']);
160 0 my $pl = $xs->XMLin($data);
161 0 $self->{data} = $pl;
162
163 0 my $collection = XML::Amazon::Collection->new();
164 0 ▶ if ($pl->{Items}->{Item}->[0]->{ASIN}){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{'Items'}{'Item'}[0]{'ASIN'}) { } 0 0 0
165 0 $collection->{total_results} = $pl->{Items}->{TotalResults};
166 0 $collection->{total_pages} = $pl->{Items}->{TotalPages};
167 0 $collection->{current_page} = $pl->{Items}->{Request}->{ItemSearchRequest}->{ItemPage};
168  
169 0 ▶ for (my $i = 0; $pl->{Items}->{Item}->[$i]; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}[$i]) 0 0 0
170
171 0 my $new_item = XML::Amazon::Item->new();
172
173 0 $new_item->{asin} = $pl->{Items}->{Item}->[$i]->{ASIN};
174 0 $new_item->{title} = $pl->{Items}->{Item}->[$i]->{ItemAttributes}->{Title};
175 0 $new_item->{publisher} = $pl->{Items}->{Item}->[$i]->{ItemAttributes}->{Publisher};
176 0 $new_item->{url} = $pl->{Items}->{Item}->[$i]->{DetailPageURL};
177 0 $new_item->{type} = $pl->{Items}->{Item}->[$i]->{ItemAttributes}->{ProductGroup};
178  
179 0 ▶ if ($pl->{Items}->{Item}->[$i]->{ItemAttributes}->{Author}->[0]){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}[$i]{"ItemAttributes"}{"Author"}[0]) 0 0 0
180 0 ▶ for (my $j = 0; $pl->{Items}->{Item}->[$i]->{ItemAttributes}->{Author}->[$j]; $j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}[$i]{"ItemAttributes"}{"Author"}[$j]) 0 0 0
181 0 $new_item->{authors}->[$j] = $pl->{Items}->{Item}->[$i]->{ItemAttributes}->{Author}->[$j];
182 }
183 }
184
185 0 ▶ if ($pl->{Items}->{Item}->[$i]->{ItemAttributes}->{Artist}->[0]){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}[$i]{"ItemAttributes"}{"Artist"}[0]) 0 0 0
186 0 ▶ for (my $j = 0; $pl->{Items}->{Item}->[$i]->{ItemAttributes}->{Artist}->[$j]; $j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}[$i]{"ItemAttributes"}{"Artist"}[$j]) 0 0 0
187 0 $new_item->{artists}->[$j] = $pl->{Items}->{Item}->[$i]->{ItemAttributes}->{Artist}->[$j];
188 }
189 }
190
191 0 ▶ if ($pl->{Items}->{Item}->[$i]->{ItemAttributes}->{Creator}->[0]->{content}){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}[$i]{"ItemAttributes"}{"Creator"}[0]{"content"}) 0 0 0
192 0 ▶ for (my $j = 0; $pl->{Items}->{Item}->[$i]->{ItemAttributes}->{Creator}->[$j]->{content}; $j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}[$i]{"ItemAttributes"}{"Creator"}[$j]{"content"}) 0 0 0
193 0 $new_item->{creators}->[$j] = $pl->{Items}->{Item}->[$i]->{ItemAttributes}->{Creator}->[$j]->{content};
194 }
195 }
196
197 0 ▶ if ($pl->{Items}->{Item}->[$i]->{ItemAttributes}->{Director}->[0]){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}[$i]{"ItemAttributes"}{"Director"}[0]) 0 0 0
198 0 ▶ for (my $j = 0; $pl->{Items}->{Item}->[$i]->{ItemAttributes}->{Director}->[$j]; $j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}[$i]{"ItemAttributes"}{"Director"}[$j]) 0 0 0
199 0 $new_item->{directors}->[$j] = $pl->{Items}->{Item}->[$i]->{ItemAttributes}->{Director}->[$j];
200 }
201 }
202
203
204 0 ▶ if ($pl->{Items}->{Item}->[$i]->{ItemAttributes}->{Actor}->[0]){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}[$i]{"ItemAttributes"}{"Actor"}[0]) 0 0 0
205 0 ▶ for (my $j = 0; $pl->{Items}->{Item}->[$i]->{ItemAttributes}->{Actor}->[$j]; $j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($pl->{"Items"}{"Item"}[$i]{"ItemAttributes"}{"Actor"}[$j]) 0 0 0
206 0 $new_item->{actors}->[$j] = $pl->{Items}->{Item}->[$i]->{ItemAttributes}->{Actor}->[$j];
207 }
208 }
209  
210  
211
212 0 $new_item->{price} = $pl->{Items}->{Item}->[$i]->{ItemAttributes}->{ListPrice}->{FormattedPrice};
213 0 $new_item->{smallimage} = $pl->{Items}->{Item}->[$i]->{SmallImage}->{URL};
214 0 $new_item->{mediumimage} = $pl->{Items}->{Item}->[$i]->{MediumImage}->{URL};
215 0 ▶ $new_item->{mediumimage} = $new_item->{smallimage} unless $new_item->{mediumimage};
Branch: 1 branch
BranchTrueFalseTotal
unless $new_item->{"mediumimage"} 0 0 0
216 0 $new_item->{largeimage} = $pl->{Items}->{Item}->[$i]->{LargeImage}->{URL};
217 0 ▶ $new_item->{largeimage} = $new_item->{mediumimage} unless $new_item->{largeimage};
Branch: 1 branch
BranchTrueFalseTotal
unless $new_item->{"largeimage"} 0 0 0
218
219 0 $collection->add_Amazon($new_item);
220 }
221 0 $self->{success} = '1';
222 0 return $collection;
223  
224 }
225  
226 else{
227 0 $self->{success} = '0';
228 0 warn 'No item found';
229 0 return '';
230 }
231 }
232  
233 sub is_success{
234 0 ▶ my $self = shift;
Subroutine is_success: not called CC=1 SCAR=6.9
Pod documented
235 0 return $self->{success};
236
237 }
238  
239 sub get_data {
240 1 ▶ my $self = shift;
Subroutine get_data: called CC=4 SCAR=13.9
Pod undocumented
241 1 my $params = shift;
242
243 1 my $url = $self->{url};
244 1 my @param;
245 1 foreach my $key (sort { $a cmp $b } keys %{$params}){
246 8 push @param, $key . '=' . URI::Escape::uri_escape(${$params}{$key}, "^A-Za-z0-9\-_.~");
247 }
248
249 1 my $string_to_sign = 'GET' . "\n" . $url . "\n" . '/onca/xml' . "\n" . join('&', @param);
250
251 1 my $sign = hmac_sha256_base64($string_to_sign,$self->{sak});
252
253 1 ▶ while (length($sign) % 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($sign) % 4) 1 1 2
254 1 $sign .= '=';
255 }
256
257 1 push @param, 'Signature=' . URI::Escape::uri_escape($sign, "^A-Za-z0-9\-_.~");
258
259 1 ▶ my $data = LWP::Simple::get('http://' . $url . '/onca/xml?' . join('&', @param))
Branch: 1 branch
BranchTrueFalseTotal
my $data = &LWP::Simple::get('http://' . $url . '/onca/xml?' . join("&", @param)) or warn "Couldn't get the XML" 1 0 1
260 or warn 'Couldn\'t get the XML';
261 1 return $data;
262
263 }
264  
265 sub Dumper{
266 0 ▶ my $self = shift;
Subroutine Dumper: not called CC=1 SCAR=6.9
Pod undocumented
267 0 print Data::Dumper::Dumper($self->{data});
268 }
269  
270 1 1;
271 __END__
↑ Summary Amazon/Item.pm »