API/Resource.pm

filter: Statementstmt 71.8% 23 / 32 Branchbran 25.0% 1 / 4 Conditioncond 20.0% 3 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 52.2% 35 / 67 CC 10 SCAR 27.4
CC
10
Cov
53%
request
30.1
new
4.4
BEGIN
1.0
CRAP 15.5
« API/Resource/Carts.pm ↑ Summary API/Client.pm »
1 # Copyright 2013 Digital River, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14  
15 package Net::MyCommerce::API::Resource;
16  
17 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Net::MyCommerce::API::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Net::MyCommerce::API::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =pod
23  
24 =head1 NAME
25
26 Net::MyCommerce::API::Resource
27
28 =head1 VERSION
29
30 version 1.0.1
31
32 =cut
33
34 3 our $VERSION = '1.0.1';
35
36 =head1 METHODS
37  
38 =head2 new (%args)
39  
40 Create a new resource object.
41  
42 $args{timeout}: timeout in seconds
43  
44 =cut
45  
46 1 ▶sub new {
Subroutine new: called CC=4 SCAR=14.9
Pod documented
47 1 my ($pkg, %args) = @_;
48 $args{client} = Net::MyCommerce::API::Client->new(
49 host => $args{host} || 'http://api.mycommerce.com',
50 prefix => $args{prefix} || '/api/v1',
51 1 ▶ timeout => $args{timeout} || 60,
Condition50%3/6
$args{'host'} || 'http://api.mycommerce.com'
l!l
01
$args{'prefix'} || '/api/v1'
l!l
01
$args{'timeout'} || 60
l!l
01
MC/DC0%0/1
$args{'host'} || 'http://api.mycommerce.com'
$args{'host'}
MC/DC0%0/1
$args{'prefix'} || '/api/v1'
$args{'prefix'}
MC/DC0%0/1
$args{'timeout'} || 60
$args{'timeout'}
Truth table50%1/2
A
A= $args{'host'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'prefix'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'timeout'}
Aresult
11
00
52 sendJSON => 1,
53 getJSON => 1,
54 );
55 1 $args{token} = Net::MyCommerce::API::Token->new(%args);
56 1 return bless { %args }, $pkg;
57 }
58  
59 =head2 request (%opts)
60  
61 Generic resource request call.
62  
63 my $resource = Net::MyCommerce::API::Resource->new();
64 my ($error, $result) = $resource->request( path => $path, params => $params );
65 if ($error) {
66 print "Error occurred: $error\n";
67 } else {
68 my $content = $result->{content};
69 my $status_code = $result->{status_code};
70 my $headers => $result->{headers};
71 }
72  
73 =cut
74  
75 1 ▶sub request {
Subroutine request: called CC=7 SCAR=34.0
Pod documented
76 1 my ($self, %opts) = @_;
77 1 my ($terror, $token) = $self->{token}->lookup();
78 1 ▶ if ($terror) {
Branch: 1 branch
BranchTrueFalseTotal
if ($terror) 1 0 1
79 1 $self->{token}->reset();
80 1 return ($terror, {});
81 }
82 0 $opts{token_id} = $token->{id};
83 0 my ($error, $result) = $self->{client}->request(%opts);
84 0 ▶ $result ||= {};
Condition0%0/2
$result ||= {}
l!l
00
MC/DC0%0/1
$result ||= {}
$result
Truth table0%0/2
A
A= $result
Aresult
11
00
85 0 ▶ $result->{content} ||= {};
Condition0%0/2
$result->{"content"} ||= {}
l!l
00
MC/DC0%0/1
$result->{"content"} ||= {}
$result->{"content"}
Truth table0%0/2
A
A= $result->{"content"}
Aresult
11
00
86 0 ▶ $result->{status_code} ||= 0;
Condition0%0/2
$result->{"status_code"} ||= 0
l!l
00
MC/DC0%0/1
$result->{"status_code"} ||= 0
$result->{"status_code"}
Truth table0%0/2
A
A= $result->{"status_code"}
Aresult
11
00
87 0 ▶ if ($result->{content}{error} && $result->{content}{error} eq 'invalid_token') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"content"}{"error"} and $result->{"content"}{"error"} eq "invalid_token") 0 0 0
Condition0%0/3
$result->{"content"}{"error"} and $result->{"content"}{"error"} eq "invalid_token"
!ll&&!rl&&r
000
MC/DC0%0/2
$result->{"content"}{"error"} and $result->{"content"}{"error"} eq "invalid_token"
$result->{"content"}{"error"} $result->{"content"}{"error"} eq "invalid_token"
Truth table0%0/3
A and B
A= $result->{"content"}{"error"}B= $result->{"content"}{"error"} eq "invalid_token"
ABresult
0X0
100
111
88 0 $self->{token}->reset();
89 0 $error = 'token no longer valid';
90 }
91 0 return ($error, $result);
92 }
93  
94 3 1;
« API/Resource/Carts.pm ↑ Summary API/Client.pm »