WallyHome/Role/REST.pm

filter: Statementstmt 91.1% 72 / 79 Branchbran 56.2% 9 / 16 Conditioncond 42.8% 6 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 4 totaltotal 74.2% 101 / 136 CC 15 SCAR 19.1
CC
15
Cov
80%
request
15.3
_simulateTestResponse
5.2
baseUrl
2.0
CRAP 6.8
« WallyHome/Role/Creator.pm ↑ Summary WallyHome/Role/Validator.pm »
1 package Device::WallyHome::Role::REST;
2 2 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use MooseX::AttributeShortcuts;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON::MaybeXS qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Device::WallyHome::Test::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = '0.21.3';
14  
15  
16 #== ATTRIBUTES =================================================================
17  
18 2 has 'apiHostname' => (
19 is => 'rw',
20 isa => 'Str',
21 default => 'api.snsr.net',
22 );
23  
24 2 has 'apiUseHttps' => (
25 is => 'rw',
26 isa => 'Int',
27 default => 1,
28 );
29  
30 2 has 'apiVersion' => (
31 is => 'rw',
32 isa => 'Str',
33 default => 'v2',
34 );
35  
36 2 has 'lastApiError' => (
37 is => 'ro',
38 isa => 'Maybe[Str]',
39 writer => '_lastApiError',
40 );
41  
42 2 has 'token' => (
43 is => 'rw',
44 isa => 'Str',
45 required => 1,
46 );
47  
48 2 has 'userAgentName' => (
49 is => 'lazy',
50 );
51  
52 2 has 'timeout' => (
53 is => 'rw',
54 isa => 'Int',
55 default => '180',
56 );
57  
58 2 has '_userAgent' => (
59 is => 'lazy',
60 );
61  
62 2 has '_testModeIdentifier' => (
63 is => 'rw',
64 isa => 'Maybe[Str]',
65 );
66  
67  
68 #== ATTRIBUTE BUILDERS =========================================================
69  
70 0 ▶sub _build__userAgent {
Subroutine _build__userAgent: not called CC=1 SCAR=6.9
71 0 my ($self) = @_;
72  
73 0 return LWP::UserAgent->new(
74 agent => $self->userAgentName(),
75 timeout => $self->timeout(),
76 );
77 }
78  
79 1 ▶sub _build_userAgentName {
Subroutine _build_userAgentName: called CC=1 SCAR=0.0
80 1 my $VERSION = Device::WallyHome->VERSION;
81  
82 1 return "Device::WallyHome v$VERSION";
83 }
84  
85  
86 #== PUBLIC METHODS =============================================================
87  
88 2 ▶sub baseUrl {
Subroutine baseUrl: called CC=2 SCAR=7.1
Pod undocumented
89 2 my ($self) = @_;
90  
91 2 ▶ my $s = $self->apiUseHttps() ? 's' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->apiUseHttps ? : 2 0 2
92  
93 2 return "http$s://" . $self->apiHostname() . '/' . $self->apiVersion() . '/';
94 }
95  
96 2 ▶sub headers {
Subroutine headers: called CC=1 SCAR=0.0
Pod undocumented
97 2 my ($self) = @_;
98  
99 2 my $headers = HTTP::Headers->new();
100  
101 2 $headers->header('Authorization' => 'Bearer ' . $self->token());
102  
103 2 return $headers;
104 }
105  
106 2 ▶sub request {
Subroutine request: called CC=10 SCAR=27.3
Pod undocumented
107 2 my ($self, $params) = @_;
108  
109 2 ▶ $params //= {};
Condition50%1/2
$params //= {}
l!l
20
MC/DC0%0/1
$params //= {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
110  
111 2 ▶ die 'invalid params: ' . Dumper($params) unless ref($params) && ref($params) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params and ref $params eq "HASH" 0 2 2
Condition33%1/3
ref $params and ref $params eq "HASH"
!ll&&!rl&&r
002
MC/DC0%0/2
ref $params and ref $params eq "HASH"
ref $params ref $params eq "HASH"
Truth table33%1/3
A and B
A= ref $paramsB= ref $params eq "HASH"
ABresult
0X0
100
111
112  
113 2 ▶ my $uri = $params->{uri} // die 'uri required';
Condition50%1/2
$params->{"uri"} // die("uri required")
l!l
20
MC/DC0%0/1
$params->{"uri"} // die("uri required")
$params->{"uri"}
Truth table50%1/2
A
A= $params->{"uri"}
Aresult
11
00
114  
115 2 ▶ my $content = $params->{content} // undef;
Condition50%1/2
$params->{"content"} // undef
l!l
02
MC/DC0%0/1
$params->{"content"} // undef
$params->{"content"}
Truth table50%1/2
A
A= $params->{"content"}
Aresult
11
00
116 2 ▶ my $headers = $params->{headers} // $self->headers();
Condition33%1/3
$params->{"headers"} // $self->headers
l!l&&r!l&&!r
020
MC/DC0%0/2
$params->{"headers"} // $self->headers
$params->{"headers"} $self->headers
Truth table33%1/3
A // B
A= $params->{"headers"}B= $self->headers
ABresult
1X1
011
000
117 2 ▶ my $method = $params->{method} // 'GET';
Condition50%1/2
$params->{"method"} // "GET"
l!l
02
MC/DC0%0/1
$params->{"method"} // "GET"
$params->{"method"}
Truth table50%1/2
A
A= $params->{"method"}
Aresult
11
00
118  
119 2 my $request = HTTP::Request->new($method, $self->wallyUrl($uri), $headers, $content);
120  
121 2 my $responseContent = undef;
122  
123 2 ▶ if (defined $self->_testModeIdentifier()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_testModeIdentifier) { } 2 0 2
124 # Avoid actual API calls for automated testing
125 2 $responseContent = $self->_simulateTestResponse($uri);
126 } else {
127 0 my $response = $self->_userAgent()->request($request);
128  
129 0 $responseContent = $response->content();
130 }
131  
132 2 my $decodedResponse = {};
133  
134 2 eval {
135 2 $decodedResponse = decode_json($responseContent);
136 };
137  
138 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
139 0 $self->_lastApiError($@);
140  
141 0 return undef;
142 }
143  
144 2 return $decodedResponse;
145 }
146  
147 2 ▶sub _simulateTestResponse {
Subroutine _simulateTestResponse: called CC=5 SCAR=16.6
148 2 my ($self, $uri) = @_;
149  
150 2 ▶ die 'testModeIdentifier required' unless defined $self->_testModeIdentifier();
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_testModeIdentifier 0 2 2
151  
152 2 my $testUtil = Device::WallyHome::Test::Data->new();
153  
154 2 my $testResponseFunc = 'sampleResponse_';
155  
156 2 ▶ if ($uri =~ /^places$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($uri =~ /^places$/) { } 1 1 2
elsif ($uri =~ m[^places/[^/]+/sensors]) { } 1 0 1
157 1 $testResponseFunc .= 'places';
158 } elsif ($uri =~ /^places\/[^\/]+\/sensors/) {
159 1 $testResponseFunc .= 'sensors';
160 } else {
161 0 die "invalid/unexpected uri for testing: $uri";
162 }
163  
164 2 $testResponseFunc .= '_' . $self->_testModeIdentifier();
165  
166 2 ▶ die "invalid testResponseFunc: $testResponseFunc" unless $testUtil->can($testResponseFunc);
Branch: 1 branch
BranchTrueFalseTotal
unless $testUtil->can($testResponseFunc) 0 2 2
167  
168 2 return $testUtil->$testResponseFunc();
169 }
170  
171 2 ▶sub wallyUrl {
Subroutine wallyUrl: called CC=1 SCAR=0.0
Pod undocumented
172 2 my ($self, $path) = @_;
173  
174 2 return $self->baseUrl() . $path;
175 }
176  
177 2 1;
« WallyHome/Role/Creator.pm ↑ Summary WallyHome/Role/Validator.pm »