Amazon.pm

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