Shopify/Multiplexer.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 119 CC 11 SCAR 32.0
CC
11
Cov
0%
use_url
56.0
new
30.0
children
2.0
CRAP 24.5
1 #!/usr/bin/perl
2  
3 use strict;
4 use warnings;
5  
6 =head1 NAME
7  
8 WWW::Shopify::Multiplexer - Object representing a group of Shopify APIs, which are multiplexed to increase your call limit.
9  
10 =cut
11  
12  
13 package WWW::Shopify::Multiplexer;
14 use base 'WWW::Shopify';
15  
16 use Data::Dumper;
17  
18 =head1 METHODS
19  
20 =head2 new($shop_url, $email, $pass, $app_count)
21  
22 Creates a new shop, without using the actual API, uses automated form submission to log in. Then creates $app_count private apps in the store to use as slaves to conduct business.
23  
24 In order to make sure that these extra apps don't end up clogging up your shop, we keep track of all nests, and make sure to delete all the apps at END time.
25  
26 =cut
27  
28 sub new {
29 my $package = shift;
30 my ( @sa ) = @_;
31 die new WWW::Shopify::Exception("Requires an API count of at least 1.") unless int(@sa) >= 1;
32 my $shop_url = $sa[0]->shop_url;
33 my $class = ref($sa[0]);
34 die new WWW::Shopify::Exception("Requires all APIs to access the same store.") unless int(grep { $_->shop_url eq $shop_url } @sa) == int(@sa);
35 die new WWW::Shopify::Exception("Requires all APIs to be the same class for now.") unless int(grep { ref($_) eq $class } @sa) == int(@sa);
36 die new WWW::Shopify::Exception("Requires a shop URL.") if int(grep { !$_->shop_url } @sa) > 0;
37 my $self = $package->SUPER::new($sa[0]->shop_url, $sa[0]->api_key);
38 $self->{children} = [@sa];
39 $self->{index} = 0;
40 return $self;
41 }
42  
43 sub children { return @{$_[0]->{children}}; }
44  
45 sub last_used_sa { return $_[0]->{children}->[$_[0]->{index}]; }
46  
47 =head2 use_url($self, $method, $url, $specs, $hash)
48  
49 Determines whether we've hit the call limit. If we have, reperforms the call with a new API.
50  
51 If the entire stack of APIs is exhausted, we die with the original error.
52  
53 =cut
54  
55 sub use_url {
56 my ($self, $method, $original_url, $specs, $hash) = @_;
57 my @children = $self->children;
58 my $method_name = lc($method) . "_url";
59 while (1) {
60 my ($decoded, $response) = eval {
61 my $url = $children[$self->{index}]->encode_url($original_url);
62 $children[$self->{index}]->$method_name($url, $specs, $hash);
63 };
64 my $exp = $@;
65 if ($exp && ref($exp) && ref($exp) eq "WWW::Shopify::Exception::CallLimit") {
66 # If we've hit the end of the road, die with our exception.
67 if (++$self->{index} == int(@children)) {
68 $self->{index} = 0;
69 if ($self->sleep_for_limit) {
70 sleep(1);
71 } else {
72 die $exp;
73 }
74 }
75 next;
76 }
77 elsif ($exp) {
78 die $exp;
79 } else {
80 $self->{index} = ($self->{index} + 1) % int(@children);
81 }
82 print STDERR uc($method) . " " . $response->request->uri . "\n" if $ENV{'SHOPIFY_LOG'} && $ENV{'SHOPIFY_LOG'} == 1;
83 print STDERR Dumper($response) if $ENV{'SHOPIFY_LOG'} && $ENV{'SHOPIFY_LOG'} > 1;
84 return ($decoded, $response);
85 }
86 }
87  
88 sub get_url { return $_[0]->use_url("GET", $_[1], $_[2], $_[3]); }
89 sub post_url { return $_[0]->use_url("POST", $_[1], $_[2], $_[3]); }
90 sub put_url { return $_[0]->use_url("PUT", $_[1], $_[2], $_[3]); }
91 sub delete_url { return $_[0]->use_url("DELETE", $_[1], $_[2], $_[3]); }
92  
93 sub ql {
94 my ($self) = @_;
95 my @children = $self->children;
96 return $children[0]->ql;
97 }
98  
99 1;