HabitRPG.pm

filter: Statementstmt 34.0% 93 / 273 Branchbran 0.7% 1 / 128 Conditioncond 42.8% 3 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 68.7% 33 / 48 Podpod 0.0% 0 / 1 totaltotal 28.4% 131 / 461 CC 69 SCAR 41.7
CC
69
Cov
24%
search_tasks
277.6
new_task
147.9
tasks
25.5
CRAP 65.0
↑ Summary HabitRPG/Task.pm »
1 package WebService::HabitRPG;
2 3 ▶use v5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Method::Signatures 20121201;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use WebService::HabitRPG::Task;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶our $DEBUG = $ENV{HRPG_DEBUG} || 0;
Condition50%1/2
$ENV{'HRPG_DEBUG'} || 0
l!l
03
MC/DC0%0/1
$ENV{'HRPG_DEBUG'} || 0
$ENV{'HRPG_DEBUG'}
Truth table50%1/2
A
A= $ENV{'HRPG_DEBUG'}
Aresult
11
00
16 3 our $TAG_PREFIX_CHARACTER = '^';
17  
18 # ABSTRACT: Perl interface to the HabitRPG (Habitica) API
19  
20 3 our $VERSION = '0.30'; # VERSION: Generated by DZP::OurPkg:Version
21  
22  
23 3 has 'api_token' => (is => 'ro'); # aka x-api-key
24 3 has 'user_id' => (is => 'ro'); # aka x-api-user
25 3 has 'agent' => (is => 'rw');
26 3 ▶has 'api_base' => (is => 'ro', default => sub { 'https://habitica.com/api/v3' });
Subroutine __ANON__: called CC=1 SCAR=0.0
27 3 has '_last_json' => (is => 'rw'); # For debugging
28 3 has 'tags' => (is => 'rw');
29 3 ▶has 'tag_prefix' => (is => 'rw', default => sub { '^' });
Subroutine __ANON__: called CC=1 SCAR=0.0
30  
31 # use constant URL_BASE => 'https://habitica.com/api/v3';
32  
33 3 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
34 3 my ($self, $args) = @_;
35
36 3 ▶ my $keep_alive = $args->{keep_alive} // 1;
Condition100%2/2
$args->{"keep_alive"} // 1
l!l
21
MC/DC100%1/1
$args->{"keep_alive"} // 1
$args->{"keep_alive"}
Truth table100%2/2
A
A= $args->{"keep_alive"}
Aresult
11
00
37  
38 # Set a default agent if we don't already have one.
39  
40 3 ▶ if (not $self->agent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->agent) 3 0 3
41 3 $self->agent(
42 WWW::Mechanize->new(
43 agent => "Perl/$], WebService::HabitRPG/" . $self->VERSION,
44 keep_alive => $keep_alive,
45 )
46 );
47 }
48  
49 3 return;
50 }
51  
52  
53 0 ▶method user() { return $self->_get_request( '/user' ); }
Subroutine BEGIN: called CC=1 SCAR=3.1
Subroutine user: not called CC=2 SCAR=12.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
54  
55  
56 0 ▶method tasks($type where qr{^(?: habits | habit | dailys | daily | todos | todo | rewards | reward | )$}x = "") {
Subroutine BEGIN: called CC=1 SCAR=3.0
Subroutine BEGIN: called CC=1 SCAR=3.0
Subroutine tasks: not called CC=6 SCAR=32.4
Branch: 3 branches
BranchTrueFalseTotal
scalar @_ > 0 ? : 0 0 0
unless do { grep {$_ ~~ qr/^(?: habits | habit | dailys | daily | todos | todo | rewards | reward | )$/x;} $type } 0 0 0
if scalar @_ > 1 0 0 0
57 0 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 0 0 0
58 0 ▶ if ($type !~ /s$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /s$/) 0 0 0
59 0 carp "Deprecated type '$type' used in tasks(); please use '${type}s' instead";
60 0 $type .= 's';
61 }
62 0 return $self->_get_tasks( "/tasks/user?type=$type" );
63 }
64 0 return $self->_get_tasks( "/tasks/user" );
65 }
66  
67  
68 0 ▶method get_task($task_id) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine get_task: not called CC=3 SCAR=20.7
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
69  
70 # _get_tasks() always returns an array ref, so we unpack that here.
71  
72 0 return $self->_get_tasks("/task/$task_id")->[0];
73 }
74  
75  
76 0 ▶method new_task(
Subroutine BEGIN: called CC=1 SCAR=2.0
Subroutine new_task: not called CC=13 SCAR=50.0
77 0 ▶ :$type! where qr{^(?: habit | daily | todo | reward )$}x,
Subroutine BEGIN: called CC=1 SCAR=4.0
Branch: 2 branches
BranchTrueFalseTotal
unless exists $args{"type"} 0 0 0
unless do { grep {$_ ~~ qr/^(?: habit | daily | todo | reward )$/x;} $type } 0 0 0
78 0 ▶ :$text!,
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"text"} 0 0 0
79 0 :$completed,
80 0 ▶ :$value = 0,
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'value'} ? : 0 0 0
81 0 ▶ :$note = '',
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'note'} ? : 0 0 0
82 0 ▶ :$up = 1,
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'up'} ? : 0 0 0
83 0 ▶ :$down = 1,
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'down'} ? : 0 0 0
84 0 ▶ :$extend = {},
Branch: 3 branches
BranchTrueFalseTotal
exists $args{'extend'} ? : 0 0 0
if keys %args 0 0 0
if scalar @_ > 16 0 0 0
85 ) {
86  
87 # Magical boolification for JSONification.
88 # TODO: These work with JSON::XS. Do they work with other backends?
89  
90 0 ▶ $up = $up ? \1 : \0;
Branch: 1 branch
BranchTrueFalseTotal
$up ? : 0 0 0
91 0 ▶ $down = $down ? \1 : \0;
Branch: 1 branch
BranchTrueFalseTotal
$down ? : 0 0 0
92  
93 # TODO : The API spec doesn't allow the submission of up/down
94 # values, but I feel that *should* be allowed, otherwise
95 # creating goals isn't full-featured.
96  
97 0 my $payload = $self->_encode_json({
98 type => $type,
99 text => $text,
100 completed => $completed,
101 value => $value,
102 note => $note,
103 up => $up,
104 down => $down,
105 %$extend,
106 });
107  
108 0 my $req = $self->_build_request('POST', '/tasks/user');
109  
110 0 $req->content( $payload );
111  
112 0 return $self->_request( $req );
113  
114 }
115  
116  
117 0 ▶method updown(
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine updown: not called CC=5 SCAR=29.5
118 0 ▶ $task!,
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
119 0 ▶ $direction! where qr{up|down}
Subroutine BEGIN: called CC=1 SCAR=4.3
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 1 0 0 0
unless do { grep {$_ ~~ qr/up|down/;} $direction } 0 0 0
if scalar @_ > 2 0 0 0
120 ) {
121  
122 0 my $req = $self->_build_request('POST', "/tasks/$task/score/$direction");
123 0 $req->header('Content-Length' => 0);
124 0 $req->content('');
125  
126 0 return $self->_request( $req );
127 }
128  
129  
130 # Convenience methods
131 0 ▶method up ($task) { return $self->updown($task, 'up' ); }
Subroutine BEGIN: called CC=1 SCAR=4.4
Subroutine up: not called CC=3 SCAR=20.7
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
132 0 ▶method down($task) { return $self->updown($task, 'down'); }
Subroutine BEGIN: called CC=1 SCAR=4.4
Subroutine down: not called CC=3 SCAR=20.7
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
133  
134  
135 0 ▶method _update(
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine _update: not called CC=4 SCAR=27.6
136 0 ▶ $task!,
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
137 0 ▶ $updates!
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 1 0 0 0
if scalar @_ > 2 0 0 0
138 ) {
139 0 my $payload = $self->_encode_json({
140 %$updates,
141 });
142  
143 0 my $req = $self->_build_request('PUT', "/user/task/$task");
144  
145 0 $req->content( $payload );
146  
147 0 return $self->_request( $req );
148 }
149  
150  
151 # NOTE: We exclude rewards
152 # NOTE: This returns a list of data structures.
153 # NOTE: Case insensitive search
154  
155 0 ▶method search_tasks($search_term, :$all = 0) {
Subroutine BEGIN: called CC=1 SCAR=5.3
Subroutine search_tasks: not called CC=17 SCAR=56.3
Branch: 4 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
exists $args{'all'} ? : 0 0 0
if keys %args 0 0 0
if scalar @_ > 3 0 0 0
156 0 my $tasks = $self->tasks;
157 0 my @matches;
158 my $tag_uuid;
159  
160 0 my $tag_prefix = $self->tag_prefix;
161  
162 # Check to see if we're doing a tag search.
163  
164 0 ▶ if ($search_term =~ /^\Q$tag_prefix\E(?<tag>.*)/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search_term =~ /^\Q$tag_prefix\E(?<tag>.*)/ms) 0 0 0
165 0 ▶ if (not $self->tags) { croak "No tags defined on " . ref($self) . " object!"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->tags) 0 0 0
166 0 $tag_uuid = $self->tags->{ $+{tag} };
167 0 ▶ $tag_uuid or croak "Search for unknown tag: $+{tag}";
Branch: 1 branch
BranchTrueFalseTotal
$tag_uuid or croak(qq[Search for unknown tag: $+{"tag"}]) 0 0 0
168 }
169  
170 0 foreach my $task (@$tasks) {
171  
172 0 ▶ next if $task->type eq 'reward';
Branch: 1 branch
BranchTrueFalseTotal
if $task->type eq "reward" 0 0 0
173 0 ▶ if ($task->completed and not $all) { next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($task->completed and not $all) 0 0 0
Condition0%0/3
$task->completed and not $all
!ll&&!rl&&r
000
MC/DC0%0/2
$task->completed and not $all
$task->completed not $all
Truth table0%0/3
A and not(B)
A= $task->completedB= not $all
ABresult
0X0
100
111
174  
175 # If we're doing a tag search...
176 0 ▶ if ($tag_uuid) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tag_uuid) { } 0 0 0
elsif ($task->id eq $search_term) { } 0 0 0
elsif ($task->text =~ /\Q$search_term\E/i) { } 0 0 0
177 0 ▶ next if not $task->tags; # Skip tagless tasks
Branch: 1 branch
BranchTrueFalseTotal
unless $task->tags 0 0 0
178 0 ▶ push(@matches, $task) if $task->tags->{$tag_uuid};
Branch: 1 branch
BranchTrueFalseTotal
if $task->tags->{$tag_uuid} 0 0 0
179 }
180  
181 # If our search term exactly matches a task ID, then use that.
182 elsif ($task->id eq $search_term) {
183 0 return $task;
184 }
185  
186 # Otherwise, if it contains our search term.
187 elsif ($task->text =~ /\Q$search_term\E/i) {
188 0 push(@matches, $task);
189 }
190 }
191  
192 0 return @matches;
193 }
194  
195 #### Internal use only code beyond this point ####
196  
197 0 ▶method _get_tasks($url) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine _get_tasks: not called CC=5 SCAR=31.7
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
198 0 my $results = $self->_get_request($url);
199  
200 # If we're fetching a single task, it can come back as
201 # an un-wrapped hash. We re-wrap it here if that's the case.
202  
203 0 ▶ if (ref($results) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $results ne "ARRAY") 0 0 0
204 0 $results = [$results];
205 }
206  
207 0 my @tasks;
208  
209 # Somehow we can get back completely undefined results,
210 # hence the grep to only look at defined ones.
211  
212 0 foreach my $raw (grep { defined } @$results) {
213 0 push @tasks, WebService::HabitRPG::Task->new(
214 $raw,
215 );
216 }
217  
218 # Sort based on task type. The old API used to do this for us.
219 0 @tasks = sort { $a->type cmp $b->type } @tasks;
220  
221 0 return \@tasks;
222 }
223  
224 0 ▶method _get_request($url) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine _get_request: not called CC=3 SCAR=21.1
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
225 0 my $req = $self->_build_request('GET', $url);
226 0 return $self->_request( $req );
227 }
228  
229 # I don't like the name here, but this makes our request, and decodes
230 # the JSON-filled result
231  
232 0 ▶method _request($req) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine _request: not called CC=4 SCAR=26.9
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
233 0 my $result = $self->_decode_json($self->agent->request( $req )->decoded_content);
234  
235 0 ▶ if ($result->{'error'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"error"}) 0 0 0
236 0 die $result->{'error'}{'message'};
237 }
238  
239 0 return $result->{'data'};
240 }
241  
242 0 ▶method _build_request($type, $url) {
Subroutine BEGIN: called CC=1 SCAR=4.9
Subroutine _build_request: not called CC=5 SCAR=31.7
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless scalar @_ > 1 0 0 0
if scalar @_ > 2 0 0 0
243  
244 0 ▶ warn "Making $type request to $url" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $WebService::HabitRPG::DEBUG 0 0 0
245  
246 0 my $req = HTTP::Request->new( $type, $self->api_base . $url );
247 0 $req->header( 'Content-Type' => 'application/json');
248 0 $req->header( 'x-api-user' => $self->user_id );
249 0 $req->header( 'x-api-key' => $self->api_token );
250  
251 0 return $req;
252 }
253  
254 3 my $json = JSON->new;
255  
256 0 ▶method _decode_json($string) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine _decode_json: not called CC=5 SCAR=31.5
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
257  
258 0 ▶ warn "Decoding JSON: $string" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $WebService::HabitRPG::DEBUG 0 0 0
259  
260 0 $self->_last_json($string); # For debugging
261 0 my $result = $json->decode( $string );
262  
263 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WebService::HabitRPG::DEBUG) 0 0 0
264 0 warn "JSON decoded to: ", Dumper($result), "\n";
265 }
266  
267 0 return $result;
268 }
269  
270 0 ▶method _encode_json($string) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine _encode_json: not called CC=3 SCAR=20.7
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
271 0 return $json->encode( $string );
272 }
273  
274  
275 3 1;
276  
277 __END__
↑ Summary HabitRPG/Task.pm »