| 1 | package Net::Amazon::RemoteCart; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | #use diagnostics; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Net::Amazon; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Net::Amazon::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Net::Amazon::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | ###use Data::Dumper; | ||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use base qw( Net::Amazon ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=27.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 1 | my($class, %options) = @_; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | if(! exists $options{token}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 0 | die "Mandatory parameter 'token' not defined"; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | if(! exists $options{affiliate_id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | $options{affiliate_id} = "webservices-20"; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | my $self = { | ||||||||||||||||||||||||||||
| 33 | token => $options{token}, | ||||||||||||||||||||||||||||
| 34 | affiliate_id => $options{affiliate_id}, | ||||||||||||||||||||||||||||
| 35 | 1 | }; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Optional attributes | ||||||||||||||||||||||||||||
| 38 | 1 | ▶ | $self->{locale} = $options{locale} if exists $options{locale}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | $self->{cart_id} = $options{cart_id} if exists $options{cart_id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | $self->{hmac} = $options{hmac} if $options{hmac}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | $self->{purchase_url} = $options{purchase_url} if $options{purchase_url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | #Set items only if passed as a hash ref | ||||||||||||||||||||||||||||
| 44 | 1 | ▶ | $self->{items} = $options{items} if ref $options{items} eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Set similar_products only if passed as array ref | ||||||||||||||||||||||||||||
| 47 | 1 | ▶ | $self->{similar_products} = $options{similar_products} if ref $options{similar_products} eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # Set items only if passed as a hash ref | ||||||||||||||||||||||||||||
| 50 | # will hold the raw xmlrefreturned by the latest request | ||||||||||||||||||||||||||||
| 51 | #$self->{xmlref} = $options{xmlref} if ref $options{xmlref} eq 'HASH'; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | $self->{f} = 'xml'; # Always the same | |||||||||||||||||||||||||||
| 54 | 1 | $self->{sims} = 'true'; # Always the same | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | $class->SUPER::help_xml_simple_choose_a_parser(); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | bless $self, ref($class) || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Make accessors for various params | ||||||||||||||||||||||||||||
| 62 | 1 | for my $attr ( qw( locale cart_id hmac purchase_url similar_products ) ) { | |||||||||||||||||||||||||||
| 63 | 5 | $class->SUPER::make_accessor($attr); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | return $self; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | #========================================================== | ||||||||||||||||||||||||||||
| 72 | # synchronize the local instance with Amazon's remote cart | ||||||||||||||||||||||||||||
| 73 | #========================================================== | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub sync { | ||||||||||||||||||||||||||
|
Subroutine
sync: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | ## my $self = shift; | ||||||||||||||||||||||||||||
| 76 | 0 | my ($self, %params) = @_; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ( exists $params{cart_id} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | $self->{cart_id} = $params{cart_id}; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if ( exists $params{hmac} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | $self->{hmac} = $params{hmac}; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if(! exists $self->{cart_id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | die 'Mandatory parameter "cart_id" not defined. $cart->cart_id(mycartid)'; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if(! exists $self->{hmac}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | die 'Mandatory parameter "hmac" not defined. $cart->hmac(myhmac)'; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | my %req_params; | |||||||||||||||||||||||||||
| 95 | 0 | $req_params{"Shopping-Cart"} = 'get'; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | my $res = $self->_request(\%req_params); | |||||||||||||||||||||||||||
| 98 | 0 | return $res; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | #========================================================== | ||||||||||||||||||||||||||||
| 103 | # remove items from cart | ||||||||||||||||||||||||||||
| 104 | #========================================================== | ||||||||||||||||||||||||||||
| 105 | 1 | ▶ | sub remove { | ||||||||||||||||||||||||||
|
Subroutine
remove: called CC=5 SCAR=17.0
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 1 | my ($self, @items) = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | ▶ | if(! exists $self->{cart_id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | die 'Mandatory parameter "cart_id" not defined. Use $cart->cart_id(mycartid)'; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | ▶ | if(! exists $self->{hmac}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | die 'Mandatory parameter "hmac" not defined. Use $cart->hmac(myhmac)'; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 1 | my (%params, $remove); | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 1 | foreach my $asin (@items ){ | |||||||||||||||||||||||||||
| 119 | 1 | my $item_id = $self->{items}{$asin}{item_id}; | |||||||||||||||||||||||||||
| 120 | 1 | ▶ | next unless $item_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 1 | $params{"Item.$item_id"} = 0; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | $params{ShoppingCart} = 'remove'; | |||||||||||||||||||||||||||
| 125 | 1 | my $res = $self->_request(\%params); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1 | return $res; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | #========================================================== | ||||||||||||||||||||||||||||
| 132 | # empty the cart | ||||||||||||||||||||||||||||
| 133 | #========================================================== | ||||||||||||||||||||||||||||
| 134 | 1 | ▶ | sub clear { | ||||||||||||||||||||||||||
|
Subroutine
clear: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 1 | ▶ | if(! exists $self->{cart_id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | die 'Mandatory parameter "cart_id" not defined. Use $cart->cart_id(mycartid)'; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 1 | ▶ | if(! exists $self->{hmac}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | die 'Mandatory parameter "hmac" not defined. Use $cart->hmac(myhmac)'; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 1 | my (%params, $remove); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 1 | $params{ShoppingCart} = 'clear'; | |||||||||||||||||||||||||||
| 148 | 1 | my $res = $self->_request(\%params); | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 1 | return $res; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | #========================================================== | ||||||||||||||||||||||||||||
| 157 | # change quantities | ||||||||||||||||||||||||||||
| 158 | #========================================================== | ||||||||||||||||||||||||||||
| 159 | 1 | ▶ | sub modify { | ||||||||||||||||||||||||||
|
Subroutine
modify: called CC=9 SCAR=27.2
Pod
documented
| |||||||||||||||||||||||||||||
| 160 | 1 | my ($self, %items) = @_; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1 | ▶ | unless ( exists $self->{cart_id} && exists $self->{hmac} ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $self->{"cart_id"} and exists $self->{"hmac"}
MC/DC0%0/2
exists $self->{"cart_id"} and exists $self->{"hmac"}
exists $self->{"cart_id"}
exists $self->{"hmac"}
Truth table33%1/3
A and B
A= exists $self->{"cart_id"}B= exists $self->{"hmac"}
| |||||||||||||||||||||||||||||
| 163 | 0 | die "Cannot modify non-existent cart (i.e. that has no cart_id or hmac)"; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 1 | my (@remove_items, %params, $modify, $remove, $res); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 1 | foreach my $asin (keys %items ){ | |||||||||||||||||||||||||||
| 170 | # If the quantity is set to 0, we'll remove the item | ||||||||||||||||||||||||||||
| 171 | 2 | ▶ | if ($items{$asin} == 0) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 172 | 0 | push @remove_items, $asin; | |||||||||||||||||||||||||||
| 173 | 0 | $remove = 1; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | } elsif (exists $self->{items}{$asin} ){ | ||||||||||||||||||||||||||||
| 176 | 2 | my $item_id = $self->{items}{$asin}{item_id}; | |||||||||||||||||||||||||||
| 177 | 2 | $params{"Item.$item_id"} = $items{$asin}; | |||||||||||||||||||||||||||
| 178 | 2 | $modify = 1; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 1 | ▶ | if ( $remove ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | $res = $self->remove(@remove_items); | |||||||||||||||||||||||||||
| 184 | 0 | ▶ | return $res unless $res->status; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 1 | ▶ | if ( $modify ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 1 | $params{ShoppingCart} = 'modify'; | |||||||||||||||||||||||||||
| 189 | 1 | $res = $self->_request(\%params); | |||||||||||||||||||||||||||
| 190 | #return $res unless $res->status; | ||||||||||||||||||||||||||||
| 191 | #$res->status || die "Failed to modify items in cart: ", join(", ", @{$res->messages}), "\n"; | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | 1 | return $res; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | #=========================================================== | ||||||||||||||||||||||||||||
| 200 | # put stuff in the cart | ||||||||||||||||||||||||||||
| 201 | #=========================================================== | ||||||||||||||||||||||||||||
| 202 | 2 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=7 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 203 | 2 | my ($self, %items) = @_; | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 2 | my (%modify_items, %params, $modify, $add, $res); | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 2 | foreach my $asin (keys %items ){ | |||||||||||||||||||||||||||
| 209 | # If the item was already in the cart, increase the quantity by the requested num | ||||||||||||||||||||||||||||
| 210 | 3 | ▶ | if ($self->{items}{$asin} ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 0 | $modify_items{$asin} = $items{$asin} + $self->{items}{$asin}{quantity}; | |||||||||||||||||||||||||||
| 212 | 0 | $modify = 1; | |||||||||||||||||||||||||||
| 213 | } else { | ||||||||||||||||||||||||||||
| 214 | 3 | $params{"Asin.$asin"} = $items{$asin}; | |||||||||||||||||||||||||||
| 215 | 3 | $add = 1; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 2 | ▶ | if ( $modify ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 0 | $res = $self->modify(%modify_items); | |||||||||||||||||||||||||||
| 221 | 0 | ▶ | return $res unless $res->status; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 2 | ▶ | if ( $add ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 2 | $params{ShoppingCart} = 'add'; | |||||||||||||||||||||||||||
| 227 | 2 | $res = $self->_request(\%params); | |||||||||||||||||||||||||||
| 228 | 2 | ▶ | return $res unless $res->status; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | #$res->status || die "Failed to add items to cart: ", join(", ", @{$res->messages}), "\n"; | ||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 1 | return $res; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | #================================================= | ||||||||||||||||||||||||||||
| 239 | # return an array ref of hashrefs containing | ||||||||||||||||||||||||||||
| 240 | # data about the cart items | ||||||||||||||||||||||||||||
| 241 | #================================================= | ||||||||||||||||||||||||||||
| 242 | 2 | ▶ | sub get_items { | ||||||||||||||||||||||||||
|
Subroutine
get_items: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 243 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 244 | 2 | my @items = values %{$self->{items}}; | |||||||||||||||||||||||||||
| 245 | 2 | return \@items; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | #=============================================== | ||||||||||||||||||||||||||||
| 251 | # First update the cart data from Amzn | ||||||||||||||||||||||||||||
| 252 | # return an array ref of hashrefs containing | ||||||||||||||||||||||||||||
| 253 | # data about the cart items | ||||||||||||||||||||||||||||
| 254 | #=============================================== | ||||||||||||||||||||||||||||
| 255 | 0 | ▶ | sub get_items_online { | ||||||||||||||||||||||||||
|
Subroutine
get_items_online: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 256 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 257 | 0 | $self->sync; | |||||||||||||||||||||||||||
| 258 | 0 | my @items = values %{$self->{items}}; | |||||||||||||||||||||||||||
| 259 | 0 | return \@items; | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | #================================================= | ||||||||||||||||||||||||||||
| 265 | # Get a hashref of data for a single item | ||||||||||||||||||||||||||||
| 266 | # based on its ASIN | ||||||||||||||||||||||||||||
| 267 | #================================================= | ||||||||||||||||||||||||||||
| 268 | 1 | ▶ | sub get_item { | ||||||||||||||||||||||||||
|
Subroutine
get_item: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 269 | 1 | my ($self, $asin) = @_; | |||||||||||||||||||||||||||
| 270 | 1 | return $self->{items}{$asin}; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | #=========================================================== | ||||||||||||||||||||||||||||
| 278 | # calculate the total cost of the items in the cart | ||||||||||||||||||||||||||||
| 279 | #=========================================================== | ||||||||||||||||||||||||||||
| 280 | 4 | ▶ | sub total_cost { | ||||||||||||||||||||||||||
|
Subroutine
total_cost: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 281 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 4 | ▶ | return $self->{total_cost} if exists $self->{total_cost}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 1 | my $items = $self->get_items(); | |||||||||||||||||||||||||||
| 286 | # Assume it's impossible to have more than one currency in the same cart | ||||||||||||||||||||||||||||
| 287 | 1 | my $currency ; | |||||||||||||||||||||||||||
| 288 | 1 | my $total = 0; | |||||||||||||||||||||||||||
| 289 | 1 | foreach ( @{ $items } ){ | |||||||||||||||||||||||||||
| 290 | 2 | my $val; | |||||||||||||||||||||||||||
| 291 | 2 | ($currency, $val) = split /\s+/, $_->{our_price}; | |||||||||||||||||||||||||||
| 292 | 2 | $total += $_->{quantity} * $val; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | # format the total val based on currency | ||||||||||||||||||||||||||||
| 296 | 1 | ▶ | unless ($currency eq 'JPY'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | 1 | $total = sprintf ("%.2f", $total); | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | 1 | return "$currency $total"; | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | #=========================================================== | ||||||||||||||||||||||||||||
| 305 | # Get the total cost of the items in the cart, formatted | ||||||||||||||||||||||||||||
| 306 | # for HTML display | ||||||||||||||||||||||||||||
| 307 | #=========================================================== | ||||||||||||||||||||||||||||
| 308 | 0 | ▶ | sub total_cost_fmt { | ||||||||||||||||||||||||||
|
Subroutine
total_cost_fmt: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 309 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 0 | ▶ | return $self->{total_cost_fmt} if exists $self->{total_cost_fmt}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 0 | return _price_format($self->total_cost()); | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | #========================================================== | ||||||||||||||||||||||||||||
| 319 | # fixes decimals based on currency and changes curency to | ||||||||||||||||||||||||||||
| 320 | # HTML compatible symbol | ||||||||||||||||||||||||||||
| 321 | #========================================================== | ||||||||||||||||||||||||||||
| 322 | 14 | ▶ | sub _price_format { | ||||||||||||||||||||||||||
|
Subroutine
_price_format: called CC=7 SCAR=26.9
| |||||||||||||||||||||||||||||
| 323 | 14 | my $str = shift; | |||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 14 | ▶ | return unless defined $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 14 | my ($currency, $val) = split /\s+/, $str; | |||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | # format the total val based on currency | ||||||||||||||||||||||||||||
| 330 | 14 | ▶ | unless ($currency eq 'JPY'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 331 | 14 | $val = sprintf ("%.2f", $val); | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | # Format the currency | ||||||||||||||||||||||||||||
| 335 | 14 | ▶ | if ($currency eq 'USD') { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 336 | 14 | $currency = '$'; | |||||||||||||||||||||||||||
| 337 | } elsif ($currency eq 'GBP'){ | ||||||||||||||||||||||||||||
| 338 | 0 | $currency = '£'; | |||||||||||||||||||||||||||
| 339 | } elsif ($currency eq 'EUR'){ | ||||||||||||||||||||||||||||
| 340 | 0 | $currency = '&euro'; | |||||||||||||||||||||||||||
| 341 | } elsif ($currency eq 'JPY'){ | ||||||||||||||||||||||||||||
| 342 | 0 | $currency = '¥'; | |||||||||||||||||||||||||||
| 343 | } else { | ||||||||||||||||||||||||||||
| 344 | 0 | $currency = "$currency "; | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | 14 | return "$currency$val"; | |||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | ################################################## | ||||||||||||||||||||||||||||
| 355 | # Slightly altered version of the request method in Amazon.pm | ||||||||||||||||||||||||||||
| 356 | 5 | ▶ | sub _request { | ||||||||||||||||||||||||||
|
Subroutine
_request: called CC=7 SCAR=20.4
| |||||||||||||||||||||||||||||
| 357 | ################################################## | ||||||||||||||||||||||||||||
| 358 | 5 | my($self, $params) = @_; | |||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 5 | my $url = URI->new($self->intl_url(Net::Amazon::Request::amzn_xml_url())); | |||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 5 | my $ref; | |||||||||||||||||||||||||||
| 363 | 5 | my $res = Net::Amazon::Response->new(); | |||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | { | ||||||||||||||||||||||||||||
| 366 | #$params->{locale} = $self->{locale} if $self->{locale}; | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | # Add the cart_id and hmac if they exist, meaning this isn't the first | ||||||||||||||||||||||||||||
| 369 | # request for this cart object | ||||||||||||||||||||||||||||
| 370 | 5 | ▶ | $params->{CartId} = $self->{cart_id} if exists $self->{cart_id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 371 | 5 | ▶ | $params->{Hmac} = $self->{hmac} if exists $self->{hmac}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | # The f and sims params are always req'd so let's put em in here | ||||||||||||||||||||||||||||
| 374 | 5 | $params->{f} = 'xml'; | |||||||||||||||||||||||||||
| 375 | 5 | $params->{sims} = 'true'; | |||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | $url->query_form( | ||||||||||||||||||||||||||||
| 378 | 'dev-t' => $self->{token}, | ||||||||||||||||||||||||||||
| 379 | 't' => $self->{affiliate_id}, | ||||||||||||||||||||||||||||
| 380 | 5 | %$params, | |||||||||||||||||||||||||||
| 381 | ); | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 5 | my $urlstr = $url->as_string; | |||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 0 | ▶ | DEBUG(sub { "URL string [ " . $urlstr . "]" }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 5 | my $xml = $self->fetch_url($urlstr, $res); | |||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 5 | ▶ | if(!defined $xml) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 390 | 0 | return $res; | |||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 0 | ▶ | DEBUG(sub { "Received [ " . $xml . "]" }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 5 | my $xs = XML::Simple->new(); | |||||||||||||||||||||||||||
| 396 | 5 | $ref = $xs->XMLin($xml); | |||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 0 | ▶ | DEBUG(sub { Data::Dumper::Dumper($ref) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 5 | ▶ | if(! defined $ref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 401 | 0 | ERROR("Invalid XML"); | |||||||||||||||||||||||||||
| 402 | 0 | $res->messages( [ "Invalid XML" ]); | |||||||||||||||||||||||||||
| 403 | 0 | $res->status(""); | |||||||||||||||||||||||||||
| 404 | 0 | return $res; | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | 5 | ▶ | if(exists $ref->{ErrorMsg}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | 1 | ▶ | if (ref($ref->{ErrorMsg}) eq "ARRAY") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 410 | # multiple errors, set arrary ref | ||||||||||||||||||||||||||||
| 411 | 0 | $res->messages( $ref->{ErrorMsg} ); | |||||||||||||||||||||||||||
| 412 | } else { | ||||||||||||||||||||||||||||
| 413 | # single error, create array | ||||||||||||||||||||||||||||
| 414 | 1 | $res->messages( [ $ref->{ErrorMsg} ] ); | |||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||
| 416 | 1 | ERROR("Fetch Error: " . $res->message ); | |||||||||||||||||||||||||||
| 417 | 1 | $res->status(""); | |||||||||||||||||||||||||||
| 418 | 1 | return $res; | |||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | # Update the cart data based on the returned xml | ||||||||||||||||||||||||||||
| 422 | 4 | $self->_update_cart_data($ref); | |||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | # We're gonna fall out of this loop here. | ||||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | 4 | $res->status(1); | |||||||||||||||||||||||||||
| 428 | 4 | return $res; | |||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | #=========================================================== | ||||||||||||||||||||||||||||
| 434 | # reload the data returned from an Amazon request into | ||||||||||||||||||||||||||||
| 435 | # the cart obj | ||||||||||||||||||||||||||||
| 436 | #=========================================================== | ||||||||||||||||||||||||||||
| 437 | 4 | ▶ | sub _update_cart_data { | ||||||||||||||||||||||||||
|
Subroutine
_update_cart_data: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 438 | 4 | my ($self, $xmlref) = @_; | |||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | 4 | $self->{xmlref} = $xmlref; | |||||||||||||||||||||||||||
| 441 | 4 | $self->{cart_id} = $xmlref->{ShoppingCart}{CartId}; | |||||||||||||||||||||||||||
| 442 | 4 | $self->{hmac} = $xmlref->{ShoppingCart}{HMAC}; | |||||||||||||||||||||||||||
| 443 | 4 | $self->{purchase_url} = $xmlref->{ShoppingCart}{PurchaseUrl}; | |||||||||||||||||||||||||||
| 444 | 4 | $self->{similar_products} = $xmlref->{ShoppingCart}{SimilarProducts}{Product}; #yeah, really | |||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | 4 | my $items = {}; | |||||||||||||||||||||||||||
| 447 | 4 | my @items; | |||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | # If there is only one item, {ShoppingCart}{Items}{Item} will be | ||||||||||||||||||||||||||||
| 450 | # a hash. If more than one, it will be an array ref. We always want | ||||||||||||||||||||||||||||
| 451 | # an array. | ||||||||||||||||||||||||||||
| 452 | 4 | ▶ | if ( ref $xmlref->{ShoppingCart}{Items}{Item} eq 'ARRAY' ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 453 | 2 | @items = @{ $xmlref->{ShoppingCart}{Items}{Item} }; | |||||||||||||||||||||||||||
| 454 | }else{ | ||||||||||||||||||||||||||||
| 455 | 2 | $items[0] = $xmlref->{ShoppingCart}{Items}{Item}; | |||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | 4 | foreach my $item (@items) { | |||||||||||||||||||||||||||
| 459 | 6 | ▶ | my $asin = $item->{Asin} or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 5 | ▶ | if ( exists $items->{$asin} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 462 | 0 | $items->{$asin}{quantity} += $item->{Quantity}; | |||||||||||||||||||||||||||
| 463 | } else { | ||||||||||||||||||||||||||||
| 464 | $items->{$asin} = { | ||||||||||||||||||||||||||||
| 465 | asin => $item->{Asin}, | ||||||||||||||||||||||||||||
| 466 | quantity => $item->{Quantity}, | ||||||||||||||||||||||||||||
| 467 | item_id => $item->{ItemId}, | ||||||||||||||||||||||||||||
| 468 | product_name => $item->{ProductName}, | ||||||||||||||||||||||||||||
| 469 | merchant_sku => $item->{MerchantSku}, # Haven't yet seen this param for real | ||||||||||||||||||||||||||||
| 470 | list_price => $item->{ListPrice}, | ||||||||||||||||||||||||||||
| 471 | list_price_fmt => &_price_format($item->{ListPrice}), | ||||||||||||||||||||||||||||
| 472 | our_price => $item->{OurPrice}, | ||||||||||||||||||||||||||||
| 473 | 5 | our_price_fmt => &_price_format($item->{OurPrice}), | |||||||||||||||||||||||||||
| 474 | }; | ||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 4 | $self->{items} = $items; | |||||||||||||||||||||||||||
| 479 | 4 | $self->{total_cost} = $self->total_cost(); | |||||||||||||||||||||||||||
| 480 | 4 | $self->{total_cost_fmt} = &_price_format($self->{total_cost}); | |||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | 1 | 1; | |||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | __END__ | ||||||||||||||||||||||||||||