Shopify/Nest.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 106 CC 11 SCAR 21.0
CC
11
Cov
0%
use_url
20.0
children
12.0
new
6.0
CRAP 8.2
1 #!/usr/bin/perl
2  
3 use strict;
4 use warnings;
5  
6 =head1 NAME
7  
8 WWW::Shopify::Nest - Object representing a group of Shopify APIs, which can be alternated over to get around the call limit.
9  
10 =cut
11  
12 =head1 DESCRIPTION
13  
14 Inherits all methods from L<WWW::Shopify>. This is not a toy. Don't use this unless there's a good reason; be nice to Shopify and don't swamp their servers.
15  
16 =cut
17  
18 package WWW::Shopify::Nest;
19 use parent 'WWW::Shopify';
20  
21 =head1 METHODS
22  
23 =head2 new($shop_url, $email, $pass, $app_count)
24  
25 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.
26  
27 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.
28  
29 =cut
30  
31 my @total_nests = ();
32  
33 sub new {
34 my $package = shift;
35 my ( $hostname, $email, $password, $app_count ) = @_;
36 die new WWW::Shopify::Exception("Requires an app count of at least 1.") unless $app_count >= 1;
37 my $self = $package->SUPER::new(@_);
38 $self->generate_children($app_count);
39 $self->{initialized} = 1;
40 push(@total_nests, $self);
41 return $self;
42 }
43  
44 END {
45 $_->free_children for (@total_nests);
46 }
47  
48 =head2 generate_children($app_count)
49  
50 Called by the constructor. Generates $app_count children.
51  
52 =cut
53  
54 sub generate_children {
55 my ($self, $app_count) = @_;
56 $self->{children} = [map { $self->create_private_app } 1..$app_count];
57 }
58  
59  
60 =head2 children($self)
61  
62 Returns an array of children L<WWW::Shopify::Private>, the size of $app_count. The child at index 0 is always the active child.
63  
64 =cut
65  
66 sub children {
67 return undef unless $_[0]->{children};
68 return @{$_[0]->{children}} if wantarray;
69 return $_[0]->{children};
70 }
71  
72 =head2 free_children
73  
74 Deletes the specified child APIs from shopify.
75  
76 =cut
77  
78 sub free_children {
79 my ($self) = @_;
80 $self->delete_private_app($_) for ($self->children);
81 $self->{children} = [];
82 }
83  
84  
85 =head2
86  
87 =head2 DESTROY($self)
88  
89 Destructor. Calls free_children on children.
90  
91 =cut
92  
93  
94 =head2 use_url($self, $method, $url, $specs, $hash)
95  
96 Determines whether we've hit the call limit. If we have, reperforms the call with a new API.
97  
98 If the entire stack of APIs is exhausted, we die with the original error.
99  
100 =cut
101  
102 sub use_url {
103 my ($self, $method, $url, $specs, $hash) = @_;
104 my @children = $self->children;
105 my $method_name = lc($method) . "_url";
106 my $index = 0;
107 while (1) {
108 eval {
109 my @return = $children[$index]->$method_name($url, $specs, $hash);
110 push(@children, shift(@children)) for (1..$index);
111 $self->{children} = \@children;
112 return @return;
113 };
114 if ($@ && ref($@) && ref($@) eq "WWW::Shopify::Exception::CallLimit") {
115 # If we've hit the end of the road, die with our exception.
116 die $@ if ($index++ == int(@children));
117 }
118 elsif ($@) {
119 die $@;
120 }
121 }
122 }
123  
124 sub get_url { return shift->SUPER::get_url(@_) if !$_[0]->{initalized}; return $_[0]->use_url("GET", $_[1], $_[2], $_[3]); }
125 sub post_url { return shift->SUPER::post_url(@_) if !$_[0]->{initalized};return $_[0]->use_url("POST", $_[1], $_[2], $_[3]); }
126 sub put_url { return shift->SUPER::put_url(@_) if !$_[0]->{initalized}; return $_[0]->use_url("PUT", $_[1], $_[2], $_[3]); }
127 sub delete_url { return shift->SUPER::delete_url(@_) if !$_[0]->{initalized}; return $_[0]->use_url("DELETE", $_[1], $_[2], $_[3]); }
128  
129 1;