blib/lib/WWW/Picnic.pm

filter: Statementstmt 39.0% 34 / 87 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 45.4% 10 / 22 Podpod 62.5% 5 / 8 totaltotal 34.2% 49 / 143 CC 11 SCAR 28.1
CC
11
Cov
31%
picnic_auth
42.0
request
42.0
__ANON__
2.0
CRAP 16.7
↑ Summary
1 package WWW::Picnic;
2 2 our $AUTHORITY = 'cpan:GETTY';
3 # ABSTRACT: Library to access Picnic Supermarket API
4 2 $WWW::Picnic::VERSION = '0.001';
5 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Digest::MD5 qw( md5_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 has user => (
14 is => 'ro',
15 required => 1,
16 );
17  
18 2 has pass => (
19 is => 'ro',
20 required => 1,
21 );
22  
23 has client_id => ( # ???
24 is => 'ro',
25 1 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 2 );
27  
28 has api_version => (
29 1 ▶ isa => sub { $_[0] >= 15 },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 is => 'ro',
31 1 ▶ default => sub { 15 },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 2 );
33  
34 has country => (
35 is => 'ro',
36 0 ▶ default => sub { 'de' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 2 );
38  
39 sub api_endpoint {
40 0 ▶ my ( $self ) = @_;
Subroutine api_endpoint: not called CC=1 SCAR=6.9
Pod undocumented
41 0 return sprintf('https://storefront-prod.%s.picnicinternational.com/api/%s', $self->country, "".$self->api_version."");
42 }
43  
44 has http_agent => (
45 is => 'ro',
46 lazy => 1,
47 default => sub {
48 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 my $ua = LWP::UserAgent->new;
50 0 $ua->agent($self->http_agent_name);
51 0 return $ua;
52 },
53 2 );
54  
55 has http_agent_name => (
56 is => 'ro',
57 lazy => 1,
58 0 ▶ default => sub { 'okhttp/3.9.0' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 2 );
60  
61 has json => (
62 is => 'ro',
63 lazy => 1,
64 0 ▶ default => sub { return JSON::MaybeXS->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 2 );
66  
67 has _auth_cache => (
68 is => 'ro',
69 1 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
70 2 );
71  
72 sub picnic_auth {
73 0 ▶ my ( $self ) = @_;
Subroutine picnic_auth: not called CC=6 SCAR=37.4
Pod undocumented
74 0 ▶ unless (defined $self->_auth_cache->{auth}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_auth_cache->{"auth"}) 0 0 0
75 0 my $url = URI->new(join('/',$self->api_endpoint,'user','login'));
76 0 my $request = HTTP::Request->new( POST => $url );
77 0 $request->header('Accept' => 'application/json');
78 0 $request->header('Content-Type' => 'application/json; charset=UTF-8');
79 0 $request->content($self->json->encode({
80 key => $self->user,
81 secret => md5_hex($self->pass),
82 client_id => $self->client_id,
83 }));
84 0 my $response = $self->http_agent->request($request);
85 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
86 0 my $auth = $response->header('X-Picnic-Auth');
87 0 ▶ croak __PACKAGE__.": login success, but no auth token!" unless $auth;
Branch: 1 branch
BranchTrueFalseTotal
unless $auth 0 0 0
88 0 my $data = $self->json->decode($response->content);
89 0 ▶ croak __PACKAGE__.": login success, but user id!" unless $data and $data->{user_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $data and $data->{"user_id"} 0 0 0
Condition0%0/3
$data and $data->{"user_id"}
!ll&&!rl&&r
000
MC/DC0%0/2
$data and $data->{"user_id"}
$data $data->{"user_id"}
Truth table0%0/3
A and B
A= $dataB= $data->{"user_id"}
ABresult
0X0
100
111
90 0 $self->_auth_cache->{auth} = $auth;
91 0 $self->_auth_cache->{time} = time;
92 0 $self->_auth_cache->{user_id} = $data->{user_id};
93 } else {
94 0 croak __PACKAGE__.": login failed! ".$response->status_line;
95 }
96 }
97 0 return $self->_auth_cache->{auth};
98 }
99  
100 sub request {
101 0 ▶ my ( $self, @original_args ) = @_;
Subroutine request: not called CC=6 SCAR=37.4
Pod undocumented
102 0 my ( $method, $path, $data, %params ) = @original_args;
103 0 ▶ $data = [] if $method eq 'PUT' and !$data;
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "PUT" and not $data 0 0 0
Condition0%0/3
$method eq "PUT" and not $data
!ll&&!rl&&r
000
MC/DC0%0/2
$method eq "PUT" and not $data
$method eq "PUT" not $data
Truth table0%0/3
A and not(B)
A= $method eq "PUT"B= not $data
ABresult
0X0
100
111
104 0 my $url = URI->new(join('/',$self->api_endpoint,$path));
105 0 ▶ if (%params) {
Branch: 1 branch
BranchTrueFalseTotal
if (%params) 0 0 0
106 0 $url->query_form(%params);
107 }
108 0 my $request = HTTP::Request->new( $method => $url );
109 0 $request->header('Accept' => 'application/json');
110 0 $request->header('X-Picnic-Auth' => $self->picnic_auth );
111 0 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 0 0 0
112 0 $request->header('Content-Type' => 'application/json');
113 0 $request->content($self->json->encode($data));
114 }
115 0 my $response = $self->http_agent->request($request);
116 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
117 0 croak __PACKAGE__.": request to ".$url->as_string." failed! ".$response->status_line;
118 }
119 0 return $self->json->decode($response->content);
120 }
121  
122 sub get_user {
123 0 ▶ my ( $self ) = @_;
Subroutine get_user: not called CC=1 SCAR=6.9
Pod documented
124 0 return $self->request( GET => 'user' );
125 }
126  
127 sub get_cart {
128 0 ▶ my ( $self ) = @_;
Subroutine get_cart: not called CC=1 SCAR=6.9
Pod documented
129 0 return $self->request( GET => 'cart' );
130 }
131  
132 sub clear_cart {
133 0 ▶ my ( $self ) = @_;
Subroutine clear_cart: not called CC=1 SCAR=6.9
Pod documented
134 0 return $self->request( POST => 'cart/clear' );
135 }
136  
137 sub get_delivery_slots {
138 0 ▶ my ( $self ) = @_;
Subroutine get_delivery_slots: not called CC=1 SCAR=6.9
Pod documented
139 0 return $self->request( GET => 'cart/delivery_slots' );
140 }
141  
142 sub search {
143 0 ▶ my ( $self, $term ) = @_;
Subroutine search: not called CC=1 SCAR=6.9
Pod documented
144 0 return $self->request( GET => 'search', undef, search_term => $term );
145 }
146  
147 2 1;
148  
149 __END__
↑ Summary