blib/lib/WWW/Shopify.pm

filter: Statementstmt 21.3% 77 / 361 Branchbran 0.0% 0 / 148 Conditioncond 0.0% 0 / 57 MC/DCmcdc 0.0% 0 / 35 Subroutinesub 30.1% 25 / 83 Podpod 55.5% 30 / 54 totaltotal 17.8% 132 / 738 CC 96 SCAR 37.3
CC
96
Cov
14%
use_url
182.0
get_all
132.0
login_admin
110.0
CRAP 41.6
« blib/lib/WWW/Shopify/Model/Item.pm ↑ Summary blib/lib/WWW/Shopify/Login.pm »
1 #!/usr/bin/perl
2  
3 =head1 NAME
4  
5 WWW::Shopify - Main object representing acess to a particular Shopify store.
6  
7 =cut
8  
9 =head1 DISCLAIMER
10  
11 WWW::Shopify is my first official CPAN module, so please bear with me as I try to sort out all the bugs, and deal with the unfamiliar CPAN infrastructure. Don't expect this to work out of the box as of yet, I'm still learning exactly how things are working. Hence some version problems I've been having.
12  
13 Thanks for your understanding.
14  
15 =cut
16  
17 =head1 DESCRIPTION
18  
19 WWW::Shopify represents a way to grab and upload data to a particular shopify store.
20 All that's required is the access token for a particular app, its url, and the API key, or altenratively, if you have a private app, you can substitue the app password for the api key.
21 If you want to use make a private app, use WWW::Shopify::Private. If you want to make a public app, use WWW::Shopify::Public.
22  
23 =cut
24  
25 =head1 EXAMPLES
26  
27 In order to get a list of all products, we can do the following:
28  
29 # Here we instantiate a copy of the public API object, with all the necessary fields.
30 my $SA = new WWW::Shopify::Public($ShopURL, $APIKey, $AccessToken);
31  
32 # Here we call get_all, OO style, and specify the entity we want to get.
33 my @Products = $SA->get_all('Product');
34  
35 In this way, we can get and modify all the different types of shopify stuffs.
36  
37 If you don't want to be using a public app, and just want to make a private app, it's just as easy:
38  
39 # Here we instantiate a copy of the private API object this time, which means we don't need an access token, we just need a password.
40 my $sa = new WWW::Shopify::Private($shop_url, $api_key, $password);
41 my @Products = $SA->get_all('Product');
42  
43 Easy enough.
44  
45 To insert a Webhook, we'd do the following.
46  
47 my $webhook = new WWW::Shopify::Model::Webhook({topic => "orders/create", address => $URL, format => "json"});
48 $SA->create($Webhook);
49  
50 And that's all there is to it. To delete all the webhooks in a store, we'd do:
51  
52 my @Webhooks = $SA->get_all('Webhook');
53 for (@Webhooks) {
54 $SA->delete($_);
55 }
56  
57 Very easy.
58  
59 =cut
60  
61 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 package WWW::Shopify;
66  
67 1 our $VERSION = '0.999';
68  
69 1 ▶use WWW::Shopify::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use WWW::Shopify::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶use Module::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 1 ▶use WWW::Shopify::URLHandler;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 ▶use WWW::Shopify::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 1 ▶use WWW::Shopify::Login;
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76  
77 # Make sure we include all our models so that when people call the model, we actually know what they're talking about.
78 1 ▶BEGIN { eval(join("\n", map { "require $_;" } findallmod WWW::Shopify::Model)); }
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 package WWW::Shopify;
81  
82 1 ▶use Date::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 =head1 METHODS
85  
86 =head2 new($shop_url, [$email, $pass])
87  
88 Creates a new shop, without using the actual API, uses automated form submission to log in.
89  
90 =cut
91  
92 sub new {
93 0 ▶ my ($package, $shop_url, $email, $password) = @_;
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
94 0 ▶ die new WWW::Shopify::Exception("Can't create a shop without a shop url.") unless $shop_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $shop_url 0 0 0
95 0 my $ua = LWP::UserAgent->new( ssl_opts => { SSL_version => 'SSLv3' } );
96 0 $ua->cookie_jar({ });
97 0 $ua->timeout(30);
98 0 $ua->agent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.5 Safari/537.22");
99 0 ▶ $package = "WWW::Shopify::Login" if $package eq "WWW::Shopify";
Branch: 1 branch
BranchTrueFalseTotal
if $package eq "WWW::Shopify" 0 0 0
100 0 my $self = bless { _shop_url => $shop_url, _ua => $ua, _url_handler => undef, _api_calls => 0, _sleep_for_limit => 0, _last_timestamp => undef }, $package;
101 0 $self->url_handler(new WWW::Shopify::URLHandler($self));
102 0 ▶ $self->login_admin($email, $password) if defined $email && defined $password;
Branch: 1 branch
BranchTrueFalseTotal
if defined $email and defined $password 0 0 0
Condition0%0/3
defined $email and defined $password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $email and defined $password
defined $email defined $password
Truth table0%0/3
A and B
A= defined $emailB= defined $password
ABresult
0X0
100
111
103 0 return $self;
104 }
105  
106  
107 0 ▶sub api_calls { $_[0]->{_api_calls} = $_[1] if defined $_[1]; return $_[0]->{_api_calls}; }
Subroutine api_calls: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 0 0
108 0 ▶sub url_handler { $_[0]->{_url_handler} = $_[1] if defined $_[1]; return $_[0]->{_url_handler}; }
Subroutine url_handler: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 0 0
109 0 ▶sub sleep_for_limit { $_[0]->{_sleep_for_limit} = $_[1] if defined $_[1]; return $_[0]->{_sleep_for_limit}; }
Subroutine sleep_for_limit: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 0 0
110 0 ▶sub last_timestamp { $_[0]->{_last_timestamp} = $_[1] if defined $_[1]; return $_[0]->{_last_timestamp}; }
Subroutine last_timestamp: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 0 0
111  
112 =head2 encode_url($url)
113  
114 Basic url encoding, works the same for public apps or logged-in apps.
115  
116 =cut
117  
118 0 ▶sub encode_url { return "https://" . $_[0]->shop_url . $_[1]; }
Subroutine encode_url: not called CC=1 SCAR=6.9
Pod documented
119  
120  
121 =head2 ua([$new_ua])
122  
123 Gets/sets the user agent we're using to access shopify's api. By default we use LWP::UserAgent, with a timeout of 5 seconds.
124  
125 PLEASE NOTE: At the very least, with LWP::UserAgent, at least, on my system, I had to force the SSL layer of the agent to use SSLv3, using the line
126  
127 LWP::UserAgent->new( ssl_opts => { SSL_version => 'SSLv3' } );
128  
129 Otherwise, Shopify does some very weird stuff, and some very weird errors are spit out. Just FYI.
130  
131 =cut
132  
133 0 ▶sub ua { $_[0]->{_ua} = $_[1] if defined $_[1]; return $_[0]->{_ua}; }
Subroutine ua: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 0 0
134  
135  
136 =head2 shop_url([$shop_url])
137  
138 Gets/sets the shop url that we're going to be making calls to.
139  
140 =cut
141  
142 # Modifiable Attributes
143 0 ▶sub shop_url { $_[0]->{_shop_url} = $_[1] if defined $_[1]; return $_[0]->{_shop_url}; }
Subroutine shop_url: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 0 0
144  
145 sub translate_model($) {
146 0 ▶ return $_[1] if $_[1] =~ m/WWW::Shopify::Model/;
Subroutine translate_model: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /WWW::Shopify::Model/ 0 0 0
147 0 return "WWW::Shopify::Model::" . $_[1];
148 }
149  
150 0 ▶sub PULLING_ITEM_LIMIT { return 250; }
Subroutine PULLING_ITEM_LIMIT: not called CC=1 SCAR=6.9
Pod undocumented
151 #sub CALL_LIMIT_REFRESH { return 60*5; }
152 #sub CALL_LIMIT_MAX { return 500; }
153 0 ▶sub CALL_LIMIT_MAX { return 40; }
Subroutine CALL_LIMIT_MAX: not called CC=1 SCAR=6.9
Pod undocumented
154 0 ▶sub CALL_LIMIT_LEAK_TIME { return 1; }
Subroutine CALL_LIMIT_LEAK_TIME: not called CC=1 SCAR=6.9
Pod undocumented
155 0 ▶sub CALL_LIMIT_LEAK_RATE { return 2; }
Subroutine CALL_LIMIT_LEAK_RATE: not called CC=1 SCAR=6.9
Pod undocumented
156  
157 0 ▶sub get_url { return $_[0]->url_handler->get_url($_[1], $_[2], $_[3], $_[4], $_[5]); }
Subroutine get_url: not called CC=1 SCAR=6.9
Pod undocumented
158 0 ▶sub post_url { return $_[0]->url_handler->post_url($_[1], $_[2], $_[3], $_[4], $_[5]); }
Subroutine post_url: not called CC=1 SCAR=6.9
Pod undocumented
159 0 ▶sub put_url { return $_[0]->url_handler->put_url($_[1], $_[2], $_[3], $_[4], $_[5]); }
Subroutine put_url: not called CC=1 SCAR=6.9
Pod undocumented
160 0 ▶sub delete_url { return $_[0]->url_handler->delete_url($_[1], $_[2], $_[3], $_[4], $_[5]); }
Subroutine delete_url: not called CC=1 SCAR=6.9
Pod undocumented
161  
162 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
163 sub use_url {
164 0 ▶ my ($self, $type, $url, @args) = @_;
Subroutine use_url: not called CC=13 SCAR=52.0
Pod undocumented
165 0 my $method = lc($type) . "_url";
166 0 my ($decoded, $response);
167 0 $url = $self->encode_url($url);
168 0 eval {
169 0 ▶ if ($self->sleep_for_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sleep_for_limit) { } 0 0 0
170 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$response or do { do { eval { do { () = $self->$method($url, @args) } }; if (my $exp = $@) { die $exp if not ref $exp or ref $exp ne "WWW::Shopify::Exception::CallLimit"; sleep 1; } } } 0 0 0
171 0 eval { ($decoded, $response) = $self->$method($url, @args); };
172 0 ▶ if (my $exp = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $exp = $@) 0 0 0
173 0 ▶ die $exp if !ref($exp) || ref($exp) ne 'WWW::Shopify::Exception::CallLimit';
Branch: 1 branch
BranchTrueFalseTotal
if not ref $exp or ref $exp ne "WWW::Shopify::Exception::CallLimit" 0 0 0
Condition0%0/3
not ref $exp or ref $exp ne "WWW::Shopify::Exception::CallLimit"
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $exp or ref $exp ne "WWW::Shopify::Exception::CallLimit"
not ref $exp ref $exp ne "WWW::Shopify::Exception::CallLimit"
Truth table0%0/3
not(A) or B
A= not ref $expB= ref $exp ne "WWW::Shopify::Exception::CallLimit"
ABresult
1X1
011
000
174 0 sleep(1);
175 }
176 } while (!$response);
177 } else {
178 0 ($decoded, $response) = $self->$method($url, @args);
179 }
180 };
181 0 ▶ if (my $exp = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $exp = $@) 0 0 0
182 0 ▶ print STDERR Dumper($exp->error) if $ENV{'SHOPIFY_LOG'} && $ENV{'SHOPIFY_LOG'} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SHOPIFY_LOG"} and $ENV{"SHOPIFY_LOG"} > 1 0 0 0
Condition0%0/3
$ENV{"SHOPIFY_LOG"} and $ENV{"SHOPIFY_LOG"} > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"SHOPIFY_LOG"} and $ENV{"SHOPIFY_LOG"} > 1
$ENV{"SHOPIFY_LOG"} $ENV{"SHOPIFY_LOG"} > 1
Truth table0%0/3
A and B
A= $ENV{"SHOPIFY_LOG"}B= $ENV{"SHOPIFY_LOG"} > 1
ABresult
0X0
100
111
183 0 die $exp;
184 }
185 0 ▶ print STDERR uc($type) . " " . $response->request->uri . "\n" if $ENV{'SHOPIFY_LOG'} && $ENV{'SHOPIFY_LOG'} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SHOPIFY_LOG"} and $ENV{"SHOPIFY_LOG"} == 1 0 0 0
Condition0%0/3
$ENV{"SHOPIFY_LOG"} and $ENV{"SHOPIFY_LOG"} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"SHOPIFY_LOG"} and $ENV{"SHOPIFY_LOG"} == 1
$ENV{"SHOPIFY_LOG"} $ENV{"SHOPIFY_LOG"} == 1
Truth table0%0/3
A and B
A= $ENV{"SHOPIFY_LOG"}B= $ENV{"SHOPIFY_LOG"} == 1
ABresult
0X0
100
111
186 0 ▶ print STDERR Dumper($response) if $ENV{'SHOPIFY_LOG'} && $ENV{'SHOPIFY_LOG'} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SHOPIFY_LOG"} and $ENV{"SHOPIFY_LOG"} > 1 0 0 0
Condition0%0/3
$ENV{"SHOPIFY_LOG"} and $ENV{"SHOPIFY_LOG"} > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"SHOPIFY_LOG"} and $ENV{"SHOPIFY_LOG"} > 1
$ENV{"SHOPIFY_LOG"} $ENV{"SHOPIFY_LOG"} > 1
Truth table0%0/3
A and B
A= $ENV{"SHOPIFY_LOG"}B= $ENV{"SHOPIFY_LOG"} > 1
ABresult
0X0
100
111
187 0 $self->last_timestamp(DateTime->from_epoch( epoch => str2time($response->header('Date'))) );
188 0 return ($decoded, $response);
189 }
190  
191 1 ▶use Devel::StackTrace;
Subroutine BEGIN: called CC=1 SCAR=0.0
192 sub resolve_trailing_url {
193 0 ▶ my ($self, $package, $action, $parent) = @_;
Subroutine resolve_trailing_url: not called CC=6 SCAR=37.4
Pod undocumented
194 0 my $method = lc($action) . "_through_parent";
195 0 ▶ if ($package->$method && (!$parent || !$parent->is_shop || $package ne "WWW::Shopify::Model::Metafield")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package->$method and !($parent && $parent->is_shop) || $package ne "WWW::Shopify::Model::Metafield") 0 0 0
Condition0%0/9
$package->$method and !($parent && $parent->is_shop) || $package ne "WWW::Shopify::Model::Metafield"
!ll&&!rl&&r
000
not $parent && $parent->is_shop or $package ne "WWW::Shopify::Model::Metafield"
l!l&&r!l&&!r
000
$parent && $parent->is_shop
!ll&&!rl&&r
000
MC/DC0%0/4
$package->$method and !($parent && $parent->is_shop) || $package ne "WWW::Shopify::Model::Metafield"
$package->$method $parent $parent->is_shop $package ne "WWW::Shopify::Model::Metafield"
Truth table0%0/5
A and not(B && C) or D
A= $package->$methodB= $parentC= $parent->is_shopD= $package ne "WWW::Shopify::Model::Metafield"
ABCDresult
0XXX0
11100
10XX1
110X1
11111
196 0 ▶ die new WWW::Shopify::Exception("Cannot get, no parent specified.") unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 0 0
197 0 return "/admin/" . $parent->plural . "/" . $parent->id . "/" . $package->plural;
198 }
199 0 return "/admin/" . $package->plural;
200 }
201  
202 sub get_all_limit {
203 0 ▶ my ($self, $package, $specs) = @_;
Subroutine get_all_limit: not called CC=4 SCAR=30.0
Pod undocumented
204 0 $package = $self->translate_model($package);
205 0 ▶ $specs->{"limit"} = PULLING_ITEM_LIMIT unless exists $specs->{"limit"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $specs->{"limit"} 0 0 0
206 0 ▶ return () if ($specs->{limit} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $specs->{"limit"} == 0 0 0 0
207 0 ▶ return $self->get_shop if $package->is_shop;
Branch: 1 branch
BranchTrueFalseTotal
if $package->is_shop 0 0 0
208 0 my $url = $self->resolve_trailing_url($package, "get", $specs->{parent}) . ".json";
209 0 my ($decoded, $response) = $self->use_url('get', $url, $specs);
210 0 my @return = map { my $object = $package->from_json($_, $self); $object->associated_parent($specs->{parent}); $object; } @{$decoded->{$package->plural}};
211 0 return @return;
212 }
213  
214 =head2 get_all($self, $package, $filters)
215  
216 Gets up to 249 * CALL_LIMIT objects (currently 124750) from Shopify at once. Goes in a loop until it's got everything. Performs a count first to see where it's at.
217  
218 @products = $sa->get_all("Product")
219  
220 If you don't want this behaviour, use the limit filter.
221  
222 =cut
223  
224 1 ▶use POSIX qw/ceil/;
Subroutine BEGIN: called CC=1 SCAR=0.0
225 1 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
226 sub get_all {
227 0 ▶ my ($self, $package, $specs) = @_;
Subroutine get_all: not called CC=11 SCAR=48.8
Pod documented
228 # We copy our specs so that we don't modify the original hash. Doesn't have to be a deep copy.
229 0 ▶ $specs = {%$specs} if $specs;
Branch: 1 branch
BranchTrueFalseTotal
if $specs 0 0 0
230 0 $package = $self->translate_model($package);
231 0 $self->validate_item($package);
232 0 ▶ return $self->get_shop if $package->is_shop;
Branch: 1 branch
BranchTrueFalseTotal
if $package->is_shop 0 0 0
233 0 ▶ return $self->get_all_limit($package, $specs) if ((defined $specs->{"limit"} && $specs->{"limit"} <= PULLING_ITEM_LIMIT) || !$package->countable());
Branch: 1 branch
BranchTrueFalseTotal
if defined $specs->{"limit"} and $specs->{"limit"} <= PULLING_ITEM_LIMIT() or not $package->countable 0 0 0
Condition0%0/6
defined $specs->{"limit"} and $specs->{"limit"} <= PULLING_ITEM_LIMIT() or not $package->countable
l!l&&r!l&&!r
000
defined $specs->{"limit"} and $specs->{"limit"} <= PULLING_ITEM_LIMIT()
!ll&&!rl&&r
000
MC/DC0%0/3
defined $specs->{"limit"} and $specs->{"limit"} <= PULLING_ITEM_LIMIT() or not $package->countable
defined $specs->{"limit"} $specs->{"limit"} <= PULLING_ITEM_LIMIT() not $package->countable
Truth table0%0/5
A and B or not(C)
A= defined $specs->{"limit"}B= $specs->{"limit"} <= PULLING_ITEM_LIMIT()C= not $package->countable
ABCresult
11X1
0X11
1011
0X00
1000
234
235 0 my @return;
236 0 eval {
237 0 $specs->{page} = 1;
238 0 my @chunk;
239 do {
240 0 @chunk = $self->get_all_limit($package, $specs);
241 0 push(@return, @chunk);
242 0 $specs->{page}++;
243 0 ▶ } while (int(@chunk) == $specs->{limit});
Branch: 1 branch
BranchTrueFalseTotal
int @chunk == $specs->{"limit"} and do { do { @chunk = $self->get_all_limit($package, $specs); push @return, @chunk; ++$specs->{"page"} } } 0 0 0
244 };
245 0 ▶ if (my $exception = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $exception = $@) 0 0 0
246 0 ▶ $exception->extra(\@return) if ref($exception) && $exception->isa('WWW::Shopify::Exception::CallLimit');
Branch: 1 branch
BranchTrueFalseTotal
if ref $exception and $exception->isa("WWW::Shopify::Exception::CallLimit") 0 0 0
Condition0%0/3
ref $exception and $exception->isa("WWW::Shopify::Exception::CallLimit")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $exception and $exception->isa("WWW::Shopify::Exception::CallLimit")
ref $exception $exception->isa("WWW::Shopify::Exception::CallLimit")
Truth table0%0/3
A and B
A= ref $exceptionB= $exception->isa("WWW::Shopify::Exception::CallLimit")
ABresult
0X0
100
111
247 0 die $exception;
248 }
249 0 ▶ return @return if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
250 0 return $return[0];
251 }
252  
253 =head2 get_shop($self)
254  
255 Returns the actual shop object.
256  
257 my $shop = $sa->get_shop;
258  
259 =cut
260  
261 sub get_shop {
262 0 ▶ my ($self) = @_;
Subroutine get_shop: not called CC=1 SCAR=6.9
Pod documented
263 0 my $package = 'WWW::Shopify::Model::Shop';
264 0 my ($decoded, $response) = $self->use_url('get', "/admin/" . $package->singular() . ".json");
265 0 my $object = $package->from_json($decoded->{$package->singular()}, $self);
266 0 return $object;
267 }
268  
269 =head2 get_timestamp($self)
270  
271 Uses a call to Shopify to determine the DateTime on the shopify server. This can be used to synchronize things without worrying about the
272 local clock being out of sync with Shopify.
273  
274 =cut
275  
276 sub get_timestamp {
277 0 ▶ my ($self) = @_;
Subroutine get_timestamp: not called CC=1 SCAR=6.9
Pod documented
278 0 my $ua = $self->ua;
279 0 my ($decoded, $response) = $self->use_url('get', "/admin/shop.json");
280 0 my $date = $response->header('Date');
281 0 my $time = str2time($date);
282 0 return DateTime->from_epoch( epoch => $time );
283 }
284  
285 =head2 get_count($self, $package, $filters)
286  
287 Gets the item count from the shopify store. So if we wanted to count all our orders, we'd do:
288  
289 my $order = $sa->get_count('Order', { status => "any" });
290  
291 It's as easy as that. Keep in mind not all items are countable (who the hell knows why); a glaring exception is assets. Either check the shopify docs, or grep for the sub "countable".
292  
293 =cut
294  
295 sub get_count {
296 0 ▶ my ($self, $package, $specs) = @_;
Subroutine get_count: not called CC=2 SCAR=17.9
Pod documented
297 0 $package = $self->translate_model($package);
298 0 $self->validate_item($package);
299 0 ▶ die "Cannot count $package." unless $package->countable();
Branch: 1 branch
BranchTrueFalseTotal
unless $package->countable 0 0 0
300 0 my ($decoded, $response) = $self->use_url('get', $self->resolve_trailing_url($package, "get", $specs->{parent}) . "/count.json", $specs);
301 0 return $decoded->{'count'};
302 }
303  
304 =head2 get($self, $package, $id)
305  
306 Gets the item from the shopify store. Returns it in local (classed up) form. In order to get an order for example:
307  
308 my $order = $sa->get('Order', 142345);
309  
310 It's as easy as that. If we don't retrieve anything, we return undef.
311  
312 =cut
313  
314 sub get {
315 0 ▶ my ($self, $package, $id, $specs) = @_;
Subroutine get: not called CC=7 SCAR=40.3
Pod documented
316 0 $package = $self->translate_model($package);
317 0 $self->validate_item($package);
318 # We have a special case for asssets, for some arbitrary reason.
319 0 my ($decoded, $response);
320 0 eval {
321 0 ▶ if ($package !~ m/Asset/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $package =~ /Asset/) { } 0 0 0
322 0 ($decoded, $response) = $self->use_url('get', $self->resolve_trailing_url($package, "get", $specs->{parent}) . "/$id.json");
323 } else {
324 0 ▶ die new WWW::Shopify::Exception("MUST have a parent with assets.") unless $specs->{parent};
Branch: 1 branch
BranchTrueFalseTotal
unless $specs->{"parent"} 0 0 0
325 0 ($decoded, $response) = $self->use_url('get', "/admin/themes/" . $specs->{parent}->id . "/assets.json", {'asset[key]' => $id, theme_id => $specs->{parent}->id});
326 }
327 };
328 0 ▶ if (my $exp = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $exp = $@) 0 0 0
329 0 ▶ return undef if ref($exp) && $exp->isa("WWW::Shopify::Exception::NotFound");
Branch: 1 branch
BranchTrueFalseTotal
if ref $exp and $exp->isa("WWW::Shopify::Exception::NotFound") 0 0 0
Condition0%0/3
ref $exp and $exp->isa("WWW::Shopify::Exception::NotFound")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $exp and $exp->isa("WWW::Shopify::Exception::NotFound")
ref $exp $exp->isa("WWW::Shopify::Exception::NotFound")
Truth table0%0/3
A and B
A= ref $expB= $exp->isa("WWW::Shopify::Exception::NotFound")
ABresult
0X0
100
111
330 0 die $exp;
331 }
332 0 my $class = $package->from_json($decoded->{$package->singular()}, $self);
333 # Wow, this is straight up stupid that sometimes we don't get a 404.
334 0 ▶ return undef unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
335 0 $class->associated_parent($specs->{parent});
336 0 return $class;
337 }
338  
339 =head2 search($self, $package, $item, { query => $query })
340  
341 Searches for the item from the shopify store. Not all items are searchable, check the API docs, or grep this module's source code and look for the "searchable" sub.
342  
343 A popular thing to search for is customers by email, you can do so like the following:
344  
345 my $customer = $sa->search("Customer", { query => "email:me@example.com" });
346  
347 =cut
348  
349 sub search {
350 0 ▶ my ($self, $package, $specs) = @_;
Subroutine search: not called CC=8 SCAR=42.8
Pod documented
351 0 $package = $self->translate_model($package);
352 0 ▶ die new WWW::Shopify::Exception("Unable to search $package; it is not marked as searchable in Shopify's API.") unless $package->searchable;
Branch: 1 branch
BranchTrueFalseTotal
unless $package->searchable 0 0 0
353 0 ▶ die new WWW::Shopify::Exception("Must have a query to search.") unless $specs && $specs->{query};
Branch: 1 branch
BranchTrueFalseTotal
unless $specs and $specs->{"query"} 0 0 0
Condition0%0/3
$specs and $specs->{"query"}
!ll&&!rl&&r
000
MC/DC0%0/2
$specs and $specs->{"query"}
$specs $specs->{"query"}
Truth table0%0/3
A and B
A= $specsB= $specs->{"query"}
ABresult
0X0
100
111
354 0 $self->validate_item($package);
355  
356 0 my ($decoded, $response) = $self->use_url('get', $self->resolve_trailing_url($package, "get", $specs->{parent}) . "/search.json", $specs);
357  
358 0 my @return = ();
359 0 foreach my $element (@{$decoded->{$package->plural()}}) {
360 0 my $class = $package->from_json($element, $self);
361 0 ▶ $class->associated_parent($specs->{parent}) if $specs->{parent};
Branch: 1 branch
BranchTrueFalseTotal
if $specs->{"parent"} 0 0 0
362 0 push(@return, $class);
363 }
364 0 ▶ return @return if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
365 0 ▶ return $return[0] if int(@return) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if int @return > 0 0 0 0
366 0 return undef;
367 }
368  
369 =head2 create($self, $item)
370  
371 Creates the item on the shopify store. Not all items are creatable, check the API docs, or grep this module's source code and look for the "creatable" sub.
372  
373 =cut
374  
375 1 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
376 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
377 sub create {
378 0 ▶ my ($self, $item, $options) = @_;
Subroutine create: not called CC=4 SCAR=30.0
Pod documented
379 0 $self->validate_item(ref($item));
380 0 my $specs = {};
381 0 ▶ my $missing = first { !exists $item->{$_} } $item->creation_minimal;
Subroutine __ANON__: not called CC=1 SCAR=6.9
382 0 ▶ die new WWW::Shopify::Exception("Missing minimal creation member: $missing in " . ref($item)) if $missing;
Branch: 1 branch
BranchTrueFalseTotal
if $missing 0 0 0
383 0 ▶ die new WWW::Shopify::Exception(ref($item) . " requires you to login with an admin account.") if $item->needs_login && !$self->logged_in_admin;
Branch: 1 branch
BranchTrueFalseTotal
if $item->needs_login and not $self->logged_in_admin 0 0 0
Condition0%0/3
$item->needs_login and not $self->logged_in_admin
!ll&&!rl&&r
000
MC/DC0%0/2
$item->needs_login and not $self->logged_in_admin
$item->needs_login not $self->logged_in_admin
Truth table0%0/3
A and not(B)
A= $item->needs_loginB= not $self->logged_in_admin
ABresult
0X0
100
111
384 0 $specs = $item->to_json();
385 0 my ($decoded, $response) = $self->use_url($item->create_method, $self->resolve_trailing_url(ref($item), "create", $item->associated_parent) . ".json", {$item->singular() => $specs}, $item->needs_login);
386 0 my $element = $decoded->{$item->singular};
387 0 my $object = ref($item)->from_json($element, $self);
388 0 $object->associated_parent($item->associated_parent);
389 0 return $object;
390 }
391  
392 =head2 update($self, $item)
393  
394 Updates the item from the shopify store. Not all items are updatable, check the API docs, or grep this module's source code and look for the "updatable" sub.
395  
396 =cut
397  
398 sub update {
399 0 ▶ my ($self, $class) = @_;
Subroutine update: not called CC=2 SCAR=17.9
Pod documented
400 0 $self->validate_item(ref($class));
401 0 my %mods = map { $_ => 1 } $class->update_fields;
402 0 my $vars = $class->to_json();
403 0 $vars = { $class->singular => {map { $_ => $vars->{$_} } grep { exists $mods{$_} } keys(%$vars)} };
404  
405 0 my ($decoded, $response);
406 0 ▶ if (ref($class) =~ m/Asset/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($class) =~ /Asset/) { } 0 0 0
407 0 my $url = $self->resolve_trailing_url(ref($class), "update", $class->associated_parent) . ".json";
408 0 ($decoded, $response) = $self->use_url($class->update_method, $url, $vars);
409 }
410 else {
411 0 ($decoded, $response) = $self->use_url($class->update_method, $self->resolve_trailing_url($class, "update", $class->associated_parent) . "/" . $class->id . ".json", $vars);
412 }
413  
414 0 my $element = $decoded->{$class->singular()};
415 0 my $object = ref($class)->from_json($element, $self);
416 0 $object->associated_parent($class->associated_parent);
417 0 return $object;
418 }
419  
420 =head2 delete($self, $item)
421  
422 Deletes the item from the shopify store. Not all items are deletable, check the API docs, or grep this module's source code and look for the "deletable" sub.
423  
424 =cut
425  
426 sub delete {
427 0 ▶ my ($self, $class) = @_;
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
428 0 $self->validate_item(ref($class));
429 0 ▶ if (ref($class) =~ m/Asset/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($class) =~ /Asset/) { } 0 0 0
430 0 my $url = $self->resolve_trailing_url(ref($class), "delete", $class->associated_parent) . ".json?asset[key]=" . $class->key;
431 0 $self->use_url($class->delete_method, $url);
432 }
433 else {
434 0 $self->use_url($class->delete_method, $self->resolve_trailing_url($class, "delete", $class->associated_parent) . "/" . $class->id . ".json");
435 }
436 0 return 1;
437 }
438  
439 # For simple things like activating, enabling, disabling, that are a simple post to a custom URL.
440 # Sometimes returns an object, sometimes returns a 1.
441 1 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
442 sub custom_action {
443 0 ▶ my ($self, $object, $action) = @_;
Subroutine custom_action: not called CC=4 SCAR=30.0
Pod undocumented
444 0 ▶ die new WWW::Shopify::Exception("You can't $action " . $object->plural . ".") unless defined $object && first { $_ eq $action } $object->actions;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object and &first(sub { $_ eq $action; } , $object->actions) 0 0 0
Condition0%0/3
defined $object and &first(sub {
!ll&&!rl&&r
000
MC/DC0%0/2
defined $object and &first(sub { $_ eq $action; } , $object->actions)
defined $object &first(sub { $_ eq $action; } , $object->actions)
Truth table0%0/3
A and B
A= defined $objectB= &first(sub { $_ eq $action; } , $object->actions)
ABresult
0X0
100
111
445 0 my $id = $object->id;
446 0 my $url = $self->resolve_trailing_url($object, $action, $object->associated_parent) . "/$id/$action.json";
447 0 my ($decoded, $response) = $self->use_url('post', $url, {$object->singular() => $object->to_json});
448 0 ▶ return 1 if !$decoded;
Branch: 1 branch
BranchTrueFalseTotal
unless $decoded 0 0 0
449 0 my $element = $decoded->{$object->singular()};
450 0 $object = ref($object)->from_json($element, $self);
451 0 return $object;
452 }
453  
454 =head2 activate($self, $charge), disable($self, $discount), enable($self, $discount), open($self, $order), close($self, $order), cancel($self, $order)
455  
456 Special actions that do what they say.
457  
458 =cut
459  
460 0 ▶sub activate { return $_[0]->custom_action($_[1], "activate"); }
Subroutine activate: not called CC=1 SCAR=6.9
Pod documented
461 0 ▶sub disable { return $_[0]->custom_action($_[1], "disable"); }
Subroutine disable: not called CC=1 SCAR=6.9
Pod documented
462 0 ▶sub enable { return $_[0]->custom_action($_[1], "enable"); }
Subroutine enable: not called CC=1 SCAR=6.9
Pod documented
463 0 ▶sub open { return $_[0]->custom_action($_[1], "open"); }
Subroutine open: not called CC=1 SCAR=6.9
Pod documented
464 0 ▶sub close { return $_[0]->custom_action($_[1], "close"); }
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
465 0 ▶sub cancel { return $_[0]->custom_action($_[1], "cancel"); }
Subroutine cancel: not called CC=1 SCAR=6.9
Pod documented
466 0 ▶sub approve { return $_[0]->custom_action($_[1], "approve"); }
Subroutine approve: not called CC=1 SCAR=6.9
Pod undocumented
467 0 ▶sub remove { return $_[0]->custom_action($_[1], "remove"); }
Subroutine remove: not called CC=1 SCAR=6.9
Pod undocumented
468 0 ▶sub spam { return $_[0]->custom_action($_[1], "spam"); }
Subroutine spam: not called CC=1 SCAR=6.9
Pod undocumented
469 0 ▶sub not_spam { return $_[0]->custom_action($_[1], "not_spam"); }
Subroutine not_spam: not called CC=1 SCAR=6.9
Pod undocumented
470  
471 =head2 login_admin($self, $email, $password)
472  
473 Logs you in to the shop as an admin, allowing you to create and manipulate discount codes, as well as upload files into user-space (not theme space).
474  
475 Doens't get around the API call limit, unfortunately.
476  
477 =cut
478  
479 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
480 sub login_admin {
481 0 ▶ my ($self, $username, $password) = @_;
Subroutine login_admin: not called CC=10 SCAR=47.0
Pod documented
482 0 ▶ return 1 if $self->{last_login_check} && (time - $self->{last_login_check}) < 1000;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"last_login_check"} and time - $self->{"last_login_check"} < 1000 0 0 0
Condition0%0/3
$self->{"last_login_check"} and time - $self->{"last_login_check"} < 1000
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"last_login_check"} and time - $self->{"last_login_check"} < 1000
$self->{"last_login_check"} time - $self->{"last_login_check"} < 1000
Truth table0%0/3
A and B
A= $self->{"last_login_check"}B= time - $self->{"last_login_check"} < 1000
ABresult
0X0
100
111
483 0 my $ua = $self->ua;
484 0 ▶ die new WWW::Shopify::Exception("Unable to login as admin without a cookie jar.") unless defined $ua->cookie_jar;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ua->cookie_jar 0 0 0
485 0 my $res = $ua->get("https://" . $self->shop_url . "/admin/auth/login");
486 0 ▶ die new WWW::Shopify::Exception("Unable to get login page.") unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
487 0 ▶ die new WWW::Shopify::Exception("Unable to find authenticity token.") unless $res->decoded_content =~ m/name="authenticity_token".*?value="(\S+)"/ms;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->decoded_content =~ /name="authenticity_token".*?value="(\S+)"/ms 0 0 0
488 0 my $authenticity_token = $1;
489 0 my $req = POST "https://" . $self->shop_url . "/admin/auth/login", [
490 login => $username,
491 password => $password,
492 remember => 1,
493 commit => "Sign In",
494 authenticity_token => $authenticity_token
495 ];
496 0 $res = $ua->request($req);
497 0 ▶ die new WWW::Shopify::Exception("Unable to complete request: " . $res->decoded_content) unless $res->is_success || $res->code == 302;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success or $res->code == 302 0 0 0
Condition0%0/3
$res->is_success or $res->code == 302
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->is_success or $res->code == 302
$res->is_success $res->code == 302
Truth table0%0/3
A or B
A= $res->is_successB= $res->code == 302
ABresult
1X1
011
000
498 0 ▶ die new WWW::Shopify::Exception("Unable to login: $1.") if $res->decoded_content =~ m/class="status system-error">(.*?)<\/div>/;
Branch: 1 branch
BranchTrueFalseTotal
if $res->decoded_content =~ m[class="status system-error">(.*?)</div>] 0 0 0
499 0 $self->{last_login_check} = time;
500 0 $self->{authenticity_token} = $authenticity_token;
501 0 $res = $self->ua->request(GET "https://" . $self->shop_url . "/admin");
502 0 ▶ die new WWW::Shopify::Exception() unless $res->decoded_content =~ m/meta content="(.*?)" name="csrf-token"/;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->decoded_content =~ /meta content="(.*?)" name="csrf-token"/ 0 0 0
503 0 $self->{authenticity_token} = $1;
504 0 $ua->default_header('X-CSRF-Token' => $self->{authenticity_token});
505 0 return 1;
506 }
507  
508 =head2 logged_in_admin($self)
509  
510 Determines whether or not you're logged in to the Shopify store as an admin.
511  
512 =cut
513  
514 sub logged_in_admin {
515 0 ▶ my ($self) = @_;
Subroutine logged_in_admin: not called CC=6 SCAR=37.4
Pod documented
516 0 ▶ return undef unless $self->{authenticity_token};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"authenticity_token"} 0 0 0
517 0 ▶ return 1 if $self->{last_login_check} && (time - $self->{last_login_check}) < 1000;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"last_login_check"} and time - $self->{"last_login_check"} < 1000 0 0 0
Condition0%0/3
$self->{"last_login_check"} and time - $self->{"last_login_check"} < 1000
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"last_login_check"} and time - $self->{"last_login_check"} < 1000
$self->{"last_login_check"} time - $self->{"last_login_check"} < 1000
Truth table0%0/3
A and B
A= $self->{"last_login_check"}B= time - $self->{"last_login_check"} < 1000
ABresult
0X0
100
111
518 0 my $ua = $self->ua;
519 0 ▶ return undef unless $ua->cookie_jar;
Branch: 1 branch
BranchTrueFalseTotal
unless $ua->cookie_jar 0 0 0
520 0 my $res = $ua->get('https://' . $self->shop_url . '/admin/discounts/count.json');
521 0 ▶ return undef unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
522 0 $self->{last_login_check} = time;
523 0 return 1;
524 }
525  
526 0 ▶sub is_valid { eval { $_[0]->get_shop; }; return undef if ($@); return 1; }
Subroutine is_valid: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
527 sub handleize {
528 0 ▶ my ($self, $handle) = @_;
Subroutine handleize: not called CC=2 SCAR=17.9
Pod undocumented
529 0 ▶ $handle = $self if !ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
530 0 $handle = lc($handle);
531 0 $handle =~ s/\s/-/g;
532 0 $handle =~ s/[^a-z0-9\-]//g;
533 0 $handle =~ s/\-+/-/g;
534 0 return $handle;
535 }
536  
537  
538 =head2 create_private_app()
539  
540 Automates a form submission to generate a private app. Returns a WWW::Shopify::Private with the appropriate credentials. Must be logged in.
541  
542 =cut
543  
544 1 ▶use WWW::Shopify::Private;
Subroutine BEGIN: called CC=1 SCAR=0.0
545 1 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
546 sub create_private_app {
547 0 ▶ my ($self) = @_;
Subroutine create_private_app: not called CC=1 SCAR=6.9
Pod documented
548 0 my $app = $self->create(new WWW::Shopify::Model::APIClient({}));
549 0 my @permissions = $self->get_all("APIPermission");
550 0 ▶ my $permission = first { $_->api_client->api_key eq $app->api_key } @permissions;
Subroutine __ANON__: not called CC=1 SCAR=6.9
551 0 return new WWW::Shopify::Private($self->shop_url, $app->api_key, $permission->access_token);
552 }
553  
554  
555 =head2 delete_private_app($private_api)
556  
557 Removes a private app. Must be logged in.
558  
559 =cut
560  
561 sub delete_private_app {
562 0 ▶ my ($self, $api) = @_;
Subroutine delete_private_app: not called CC=2 SCAR=17.9
Pod documented
563 0 my @apps = $self->get_all("APIPermission");
564 0 ▶ my $app = first { $_->api_client && $_->api_client->api_key eq $api->api_key } @apps;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_->api_client and $_->api_client->api_key eq $api->api_key 0 0 0
565 0 ▶ die new WWW::Shopify::Exception("Can't find app with api key " . $api->api_key) unless $app;
Branch: 1 branch
BranchTrueFalseTotal
unless $app 0 0 0
566 0 return $self->delete(new WWW::Shopify::Model::APIClient({ id => $app->api_client->id }));
567 }
568  
569  
570 # Internal methods.
571 sub validate_item {
572 0 ▶ eval { die unless $_[1]; $_[1]->is_item; };
Subroutine validate_item: not called CC=5 SCAR=34.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 0 0 0
573 0 ▶ die new WWW::Shopify::Exception($_[1] . " is not an item!") if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
574 0 ▶ die new WWW::Shopify::Exception($_[1] . " requires you to login with an admin account.") if $_[1]->needs_login && !$_[0]->logged_in_admin;
Branch: 1 branch
BranchTrueFalseTotal
if $_[1]->needs_login and not $_[0]->logged_in_admin 0 0 0
Condition0%0/3
$_[1]->needs_login and not $_[0]->logged_in_admin
!ll&&!rl&&r
000
MC/DC0%0/2
$_[1]->needs_login and not $_[0]->logged_in_admin
$_[1]->needs_login not $_[0]->logged_in_admin
Truth table0%0/3
A and not(B)
A= $_[1]->needs_loginB= not $_[0]->logged_in_admin
ABresult
0X0
100
111
575 }
576  
577  
578 =head2 upload_files($self, @image_paths)
579  
580 Requires log in. Uploads an array of files/images into the shop's non-theme file/image management system by automating a form submission.
581  
582 $sa->login_admin("email", "password");
583 $sa->upload_files("image1.jpg", "image2.jpg");
584  
585 Gets around the issue that this is not actually exposed to the API.
586  
587 =cut
588  
589 sub upload_files {
590 0 ▶ my ($self, @images) = @_;
Subroutine upload_files: not called CC=5 SCAR=34.0
Pod documented
591 0 ▶ die new WWW::Shopify::Exception("Uploading files/images requires you to login with an admin account.") unless $self->logged_in_admin;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in_admin 0 0 0
592 0 foreach my $path (@images) {
593 0 ▶ die new WWW::Shopify::Exception("Unable to determine extension type.") unless $path =~ m/\.(\w{2,4})$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ /\.(\w{2,4})$/ 0 0 0
594 my $req = POST "https://" . $self->shop_url . "/admin/files.json",
595 Content_Type => "form-data",
596 Accept => "application/json",
597 0 Content => [authenticity_token => $self->{authenticity_token}, "file[file]" => [$path]];
598 0 my $res = $self->ua->request($req);
599 0 ▶ die new WWW::Shopify::Exception("Error uploading $path.") unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
600 }
601 0 return int(@images);
602 }
603  
604 =cut
605  
606 =head1 EXPORTED FUNCTIONS
607  
608 The functions below are exported as part of the package.
609  
610 =cut
611  
612 =head2 calc_webhook_signature($shared_secret, $request_body)
613  
614 Calculates the webhook_signature based off the shared secret and request body passed in.
615  
616 =cut
617  
618 =head2 verify_webhook($shared_secret, $request_body)
619  
620 Shopify webhook authentication. ALMOST the same as login authentication, but, of course, because this is shopify they've got a different system. 'Cause you know, one's not good enough.
621  
622 Follows this: http://wiki.shopify.com/Verifying_Webhooks.
623  
624 =cut
625  
626 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
627 1 our @EXPORT_OK = qw(verify_webhook verify_login verify_proxy calc_webhook_signature calc_login_signature calc_proxy_signature handleize);
628 1 ▶use Digest::MD5 'md5_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
629 1 ▶use Digest::SHA qw(hmac_sha256_hex hmac_sha256_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
630 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
631  
632 sub calc_webhook_signature {
633 0 ▶ my ($shared_secret, $request_body) = @_;
Subroutine calc_webhook_signature: not called CC=3 SCAR=24.8
Pod documented
634 0 ▶ my $calc_signature = hmac_sha256_base64((defined $request_body) ? $request_body : "", $shared_secret);
Branch: 1 branch
BranchTrueFalseTotal
defined $request_body ? : 0 0 0
635 0 ▶ while (length($calc_signature) % 4) { $calc_signature .= '='; }
Branch: 1 branch
BranchTrueFalseTotal
if (length($calc_signature) % 4) 0 0 0
636 0 return $calc_signature;
637 }
638  
639 sub verify_webhook {
640 0 ▶ my ($x_shopify_hmac_sha256, $request_body, $shared_secret) = @_;
Subroutine verify_webhook: not called CC=2 SCAR=17.9
Pod documented
641 0 ▶ return undef unless $x_shopify_hmac_sha256;
Branch: 1 branch
BranchTrueFalseTotal
unless $x_shopify_hmac_sha256 0 0 0
642 0 return $x_shopify_hmac_sha256 eq calc_webhook_signature($shared_secret, $request_body);
643 }
644  
645 =head2 calc_login_signature($shared_secret, $%params)
646  
647 Calculates the login signature based on the shared secret and parmaeter hash passed in.
648  
649 =cut
650  
651 =head2 verify_login($shared_secret, $%params)
652  
653 Shopify app dashboard verification (when someone clicks Login on the app dashboard).
654  
655 This one was kinda random, 'cause they say it's like a webhook, but it's actually like legacy auth.
656  
657 Also, they don't have a code parameter. For whatever reason.
658  
659 =cut
660  
661 sub calc_login_signature {
662 0 ▶ my ($shared_secret, $params) = @_;
Subroutine calc_login_signature: not called CC=1 SCAR=6.9
Pod documented
663 0 return md5_hex($shared_secret . join("", map { "$_=" . $params->{$_} } (sort(grep { $_ ne "signature" } keys(%$params)))));
664 }
665  
666 sub verify_login {
667 0 ▶ my ($shared_secret, $params) = @_;
Subroutine verify_login: not called CC=2 SCAR=17.9
Pod documented
668 0 ▶ return undef unless $params->{signature};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"signature"} 0 0 0
669 0 return calc_login_signature($shared_secret, $params) eq $params->{signature};
670 }
671  
672 =head2 calc_proxy_signature($shared_secret, $%params)
673  
674 Based on shared secret/hash of parameters passed in, calculates the proxy signature.
675  
676 =cut
677  
678 =head2 verify_proxy($shared_secret, %$params)
679  
680 This is SLIGHTLY different from the above two. For, as far as I can tell, no reason.
681  
682 =cut
683  
684 sub calc_proxy_signature {
685 0 ▶ my ($shared_secret, $params) = @_;
Subroutine calc_proxy_signature: not called CC=2 SCAR=17.9
Pod documented
686 return hmac_sha256_hex(join("", sort(map {
687 0 my $p = $params->{$_};
688 0 ▶ "$_=" . (ref($p) eq "ARRAY" ? join("$_=", @$p) : $p);
Branch: 1 branch
BranchTrueFalseTotal
ref $p eq 'ARRAY' ? : 0 0 0
689 0 } (grep { $_ ne "signature" } keys(%$params)))), $shared_secret);
690 }
691  
692 sub verify_proxy {
693 0 ▶ my ($shared_secret, $params) = @_;
Subroutine verify_proxy: not called CC=2 SCAR=17.9
Pod documented
694 0 ▶ return undef unless $params->{signature};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"signature"} 0 0 0
695 0 return calc_proxy_signature($shared_secret, $params) eq $params->{signature};
696 }
697  
698 =head1 SEE ALSO
699  
700 L<WWW::Shopify::Public>, L<WWW::Shopify::Private>, L<WWW::Shopify::Test>, L<WWW::Shopify::Item>, L<WWW::Shopify::Common::DBIx>
701  
702 =head1 AUTHOR
703  
704 Adam Harrison (adamdharrison@gmail.com)
705  
706 =head1 LICENSE
707  
708 Copyright (C) 2014 Adam Harrison
709  
710 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
711  
712 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
713  
714 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
715  
716 =cut
717  
718 1 1;
« blib/lib/WWW/Shopify/Model/Item.pm ↑ Summary blib/lib/WWW/Shopify/Login.pm »