blib/lib/WWW/Shopify/Public.pm

filter: Statementstmt 47.8% 34 / 71 Branchbran 36.6% 11 / 30 Conditioncond 33.3% 6 / 18 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 53.8% 7 / 13 Podpod 85.7% 6 / 7 totaltotal 43.6% 65 / 149 CC 23 SCAR 29.1
CC
23
Cov
43%
exchange_token
42.0
scope_compare
22.8
new
6.0
CRAP 18.3
« blib/lib/WWW/Shopify/Common/DBIxGroup.pm ↑ Summary blib/lib/WWW/Shopify/Field/Relation.pm »
1 #!/usr/bin/perl
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use WWW::Shopify;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 WWW::Shopify::Public - Main object representing public app access to a particular Shopify store.
11  
12 =cut
13  
14 =head1 DESCRIPTION
15  
16 Inherits all methods from L<WWW::Shopify>, provides additional mechanisms to modify the used access_token, user-agent and url handler.
17  
18 =cut
19  
20 package WWW::Shopify::Public;
21 1 ▶use parent 'WWW::Shopify';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head1 METHODS
24  
25 =head2 new($url, $api_key, $access_token)
26  
27 Creates a new WWW::Shopify::Public object, which allows you to make calls via the shopify public app interface.
28  
29 You can see an overview of the authentication workflow for a public app here: L<< http://api.shopify.com/authentication.html >>
30  
31 =cut
32  
33 sub new {
34 0 ▶ my $class = shift;
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
35 0 my ($shop_url, $api_key, $access_token) = @_;
36 0 ▶ die new WWW::Shopify::Exception("Can't create a shop without an api key.") unless $api_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $api_key 0 0 0
37 0 my $self = $class->SUPER::new($shop_url);
38 0 $self->api_key($api_key);
39 0 $self->access_token($access_token);
40 0 $self->ua->default_header("X-Shopify-Access-Token" => $access_token);
41 0 return $self;
42 }
43  
44 =head2 api_key([$api_key])
45  
46 Gets/sets the app's access token.
47  
48 =cut
49  
50 0 ▶sub api_key { $_[0]->{_api_key} = $_[1] if defined $_[1]; return $_[0]->{_api_key}; }
Subroutine api_key: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 0 0
51  
52 =head2 access_token([$access_token])
53  
54 Gets/sets the app's access token.
55  
56 =cut
57  
58 0 ▶sub access_token { $_[0]->{_access_token} = $_[1] if defined $_[1]; return $_[0]->{_access_token}; }
Subroutine access_token: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 0 0
59  
60 0 ▶sub is_valid { my $self = shift; $self->SUPER::is_valid(@_); return $self->{_access_token}; }
Subroutine is_valid: not called CC=1 SCAR=6.9
Pod undocumented
61  
62 =head2 authorize_url(@$scope, $redirect)
63  
64 When the shop doesn't have an access_token, this is what you should be redirecting your client to. Inputs should be your scope, as an array, and the url you want to redirect to.
65  
66 =cut
67  
68 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 sub authorize_url {
70 0 ▶ my ($self, $scope, $redirect) = (@_);
Subroutine authorize_url: not called CC=1 SCAR=6.9
Pod documented
71 0 my $hostname = $self->shop_url;
72 0 my %parameters = (
73 'client_id' => $self->api_key,
74 'scope' => join(",", @$scope),
75 'redirect_uri' => $redirect
76 );
77 0 return "https://$hostname/admin/oauth/authorize?" . join("&", map { "$_=" . uri_escape($parameters{$_}) } keys(%parameters));
78 }
79  
80 =head2 scope_compare(@$scope1, @$scope2)
81
82 Determines whether scope1 is more or less permissive than scope2. If the scopes cannot be compared easily
83 (e.g. [write_orders, read_products], [read_products, write_script_tag]), then undef is returned. If scopes
84 can be compared, returns -1, 0 or 1 as appropriate.
85  
86 =cut
87  
88 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
89 1 our @EXPORT_OK = qw(scope_compare);
90  
91 sub scope_compare {
92 5 ▶ my ($scope1, $scope2) = @_;
Subroutine scope_compare: called CC=15 SCAR=31.3
Pod documented
93 5 ▶ die new WWW::Shopify::Exception("Invalid scopes passed to compare.") unless ref($scope1) && ref($scope2) && ref($scope1) eq "ARRAY" && ref($scope2) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $scope1 and ref $scope2 and ref $scope1 eq "ARRAY" and ref $scope2 eq "ARRAY" 0 5 5
Condition33%3/9
ref $scope1 and ref $scope2 and ref $scope1 eq "ARRAY" and ref $scope2 eq "ARRAY"
!ll&&!rl&&r
005
ref $scope1 and ref $scope2 and ref $scope1 eq "ARRAY"
!ll&&!rl&&r
005
ref $scope1 and ref $scope2
!ll&&!rl&&r
005
MC/DC0%0/4
ref $scope1 and ref $scope2 and ref $scope1 eq "ARRAY" and ref $scope2 eq "ARRAY"
ref $scope1 ref $scope2 ref $scope1 eq "ARRAY" ref $scope2 eq "ARRAY"
Truth table20%1/5
A and B and C and D
A= ref $scope1B= ref $scope2C= ref $scope1 eq "ARRAY"D= ref $scope2 eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
94 # If scope2 has more permission settings than scope1, we've jumped up in permissions.
95 5 ▶ return 1 if int(@$scope1) < int(@$scope2);
Branch: 1 branch
BranchTrueFalseTotal
if int @$scope1 < int @$scope2 2 3 5
96 3 ▶ return -1 if (int(@$scope1) > int(@$scope2));
Branch: 1 branch
BranchTrueFalseTotal
if int @$scope1 > int @$scope2 0 3 3
97 # Here, we should have exactly equal amounts of scopes in both arrays, sorted. So they should be the same types down.
98 3 $scope1 = [sort(@$scope1)];
99 3 $scope2 = [sort(@$scope2)];
100 3 for (0..int(@$scope1)-1) {
101 3 ▶ die new WWW::Shopify::Exception("Invalid scope: $_") unless $scope1->[$_] =~ m/(read|write)_(\w+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $scope1->[$_] =~ /(read|write)_(\w+)/ 0 3 3
102 3 my ($scope1_permission, $scope1_type) = ($1, $2);
103 3 ▶ die new WWW::Shopify::Exception("Invalid scope: $_") unless $scope2->[$_] =~ m/(read|write)_(\w+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $scope2->[$_] =~ /(read|write)_(\w+)/ 0 3 3
104 3 my ($scope2_permission, $scope2_type) = ($1, $2);
105 # If we have not the same type, that means we've got some weird permissions going on, so we return undef, because it's
106 # neither less nor more permissive, necessarily, just different.
107 3 ▶ return undef unless $scope1_type eq $scope2_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $scope1_type eq $scope2_type 1 2 3
108 # When we've jumped down to a read in our second scope, we're less permissive.
109 2 ▶ return -1 if $scope2_permission eq "read" && $scope1_permission eq "write";
Branch: 1 branch
BranchTrueFalseTotal
if $scope2_permission eq "read" and $scope1_permission eq "write" 1 1 2
Condition66%2/3
$scope2_permission eq "read" and $scope1_permission eq "write"
!ll&&!rl&&r
101
MC/DC50%1/2
$scope2_permission eq "read" and $scope1_permission eq "write"
$scope2_permission eq "read" $scope1_permission eq "write"
Truth table66%2/3
A and B
A= $scope2_permission eq "read"B= $scope1_permission eq "write"
ABresult
0X0
100
111
110 # When we've jumped up to a write in our
111 1 ▶ return 1 if $scope1_permission eq "read" && $scope2_permission eq "write";
Branch: 1 branch
BranchTrueFalseTotal
if $scope1_permission eq "read" and $scope2_permission eq "write" 1 0 1
Condition33%1/3
$scope1_permission eq "read" and $scope2_permission eq "write"
!ll&&!rl&&r
001
MC/DC0%0/2
$scope1_permission eq "read" and $scope2_permission eq "write"
$scope1_permission eq "read" $scope2_permission eq "write"
Truth table33%1/3
A and B
A= $scope1_permission eq "read"B= $scope2_permission eq "write"
ABresult
0X0
100
111
112 }
113 0 return 0;
114 }
115  
116 =head2 exchange_token($shared_secret, $code)
117  
118 When you have a temporary code, which you should get from authorize_url's redirect and you want to exchange it for a token, you call this.
119  
120 =cut
121  
122 sub exchange_token {
123 0 ▶ my ($self, $shared_secret, $code) = (@_);
Subroutine exchange_token: not called CC=6 SCAR=37.4
Pod documented
124
125 0 my $req = HTTP::Request->new(POST => "https://" . $self->shop_url . "/admin/oauth/access_token");
126 0 $req->content_type('application/x-www-form-urlencoded');
127 0 my %parameters = (
128 'client_id' => $self->api_key,
129 'client_secret' => $shared_secret,
130 'code' => $code
131 );
132 0 $req->content(join("&", map { "$_=" . uri_escape($parameters{$_}) } keys(%parameters)));
133 0 my $res;
134 # If you go through the install too fast, Shopify will give you an access denied thing. So in that case, let's repeat until
135 0 my $success = undef;
136 0 ▶ for (my $i = 0; $i < 5 && !$success; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 5 and not $success) 0 0 0
Condition0%0/3
$i < 5 and not $success
!ll&&!rl&&r
000
MC/DC0%0/2
$i < 5 and not $success
$i < 5 not $success
Truth table0%0/3
A and not(B)
A= $i < 5B= not $success
ABresult
0X0
100
111
137 0 $res = $self->ua->request($req);
138 0 $success = $res->is_success();
139 0 ▶ sleep 1 unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 0 0
140 }
141 0 ▶ die new WWW::Shopify::Exception("Unable to reach server for https://" . $self->shop_url . "/admin/oauth/access_token: " . $res->code() . ".\n" . $res->content) unless $res->is_success();
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
142 0 my $decoded = JSON::decode_json($res->content);
143 0 ▶ die new WWW::Shopify::Exception("Unable to retrieve access token.") unless defined $decoded->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decoded->{"access_token"} 0 0 0
144 0 return $decoded->{access_token};
145 }
146  
147 =head1 SEE ALSO
148  
149 L<WWW::Shopify::Item>, L<WWW::Shopify>
150  
151 =head1 AUTHOR
152  
153 Adam Harrison (adamdharrison@gmail.com)
154  
155 =head1 LICENSE
156  
157 See LICENSE in the main directory.
158  
159 =cut
160  
161 1 1;
« blib/lib/WWW/Shopify/Common/DBIxGroup.pm ↑ Summary blib/lib/WWW/Shopify/Field/Relation.pm »