SlimTimer.pm

filter: Statementstmt 49.7% 87 / 175 Branchbran 3.3% 1 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 61.1% 33 / 54 Podpod n/a totaltotal 46.7% 121 / 259 CC 18 SCAR 17.0
CC
18
Cov
43%
__ANON__
25.8
__ANON__
17.5
__ANON__
16.8
CRAP 5.5
↑ Summary SlimTimer/TimeEntry.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package WebService::SlimTimer;
5  
6 # ABSTRACT: Provides interface to SlimTimer web service.
7  
8  
9 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use MooseX::Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use MooseX::Types::Moose qw(Int Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use WebService::SlimTimer::Task;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use WebService::SlimTimer::TimeEntry;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use WebService::SlimTimer::Types qw(TimeStamp OptionalTimeStamp);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 our $VERSION = '0.005'; # VERSION
22 2 our $DEBUG = 0;
23  
24 2 has api_key => ( is => 'ro', isa => Str, required => 1 );
25  
26 2 has user_id => ( is => 'ro', isa => Int, writer => '_set_user_id' );
27 2 has access_token => ( is => 'ro', isa => Str, writer => '_set_access_token',
28 predicate => 'is_logged_in'
29 );
30  
31 2 has _user_agent => ( is => 'ro', builder => '_create_ua', lazy => 1 );
32  
33 # Returns just the first line of possibly multiline string passed as argument.
34 sub _first_line
35 0 ▶{
Subroutine _first_line: not called CC=1 SCAR=6.9
36 0 my $text = shift;
37 0 $text =~ s/\n.*//s;
38 0 return $text;
39 }
40  
41 # Return a string representation of a TimeStamp.
42 0 ▶method _format_time(TimeStamp $timestamp) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=0.4
43 2 ▶ use DateTime::Format::RFC3339;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 0 return DateTime::Format::RFC3339->format_datetime($timestamp)
45 2 }
46  
47 # Create the LWP object that we use. This is currently trivial but provides a
48 # central point for customizing its creation later.
49 0 ▶method _create_ua() {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=3.5
50 0 my $ua = LWP::UserAgent->new;
51 0 return $ua;
52 2 }
53  
54 # Common part of _request() and _post(): submit the request and check that it
55 # didn't fail.
56 0 ▶method _submit($req, Str $error) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=4 SCAR=28.2
57 0 my $res = $self->_user_agent->request($req);
58  
59 0 ▶ print DateTime->now() . ": received reply.\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $WebService::SlimTimer::DEBUG 0 0 0
60 0 ▶ print "Reply contents:\n" . $res->content . "\n" if $DEBUG >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $WebService::SlimTimer::DEBUG >= 2 0 0 0
61  
62 0 ▶ if ( !$res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
63 0 die "$error: " . $res->status_line
64 }
65  
66 0 return Load($res->content)
67 2 }
68  
69 # A helper method for creating and submitting an HTTP request without
70 # any body parameters, e.g. a GET or DELETE.
71 0 ▶method _request(Str $method, Str $url, Str :$error!, HashRef :$params) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=2 SCAR=15.9
72 0 my $uri = URI->new($url);
73 0 $uri->query_form(
74 api_key => $self->api_key,
75 access_token => $self->access_token,
76 %$params
77 );
78 0 my $req = HTTP::Request->new($method, $uri);
79  
80 0 ▶ print DateTime->now() . ": " . _first_line($req->as_string) . ".\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $WebService::SlimTimer::DEBUG 0 0 0
81  
82 0 $req->header(Accept => 'application/x-yaml');
83  
84 0 return $self->_submit($req, $error)
85 2 }
86  
87 # Another helper for POST and PUT requests.
88 0 ▶method _post(Str $method, Str $url, HashRef $params, Str :$error!) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=4 SCAR=28.6
89 0 my $req = HTTP::Request->new($method, $url);
90  
91 0 $params->{'api_key'} = $self->api_key;
92  
93 # POST request is used to log in so we can be called before we have the
94 # access token and need to check for this explicitly.
95 0 ▶ if ( $self->is_logged_in ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_logged_in) 0 0 0
96 0 $params->{'access_token'} = $self->access_token;
97 }
98  
99 0 ▶ print DateTime->now() . ": " . _first_line($req->as_string) . ".\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $WebService::SlimTimer::DEBUG 0 0 0
100  
101 0 $req->content(Dump($params));
102  
103 0 ▶ print "Parameters:\n" . $req->as_string . "\n" if $DEBUG >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $WebService::SlimTimer::DEBUG >= 2 0 0 0
104  
105 0 $req->header(Accept => 'application/x-yaml');
106 0 $req->content_type('application/x-yaml');
107  
108 0 return $self->_submit($req, $error)
109 2 }
110  
111  
112 # Provide a simple single-argument ctor instead of default Moose one taking a
113 # hash with all attributes values.
114 around BUILDARGS => sub
115 1 ▶{
Subroutine __ANON__: called CC=2 SCAR=7.1
116 1 ▶ die "A single API key argument is required" unless @_ == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 1 1
117  
118 1 my ($orig, $class, $api_key) = @_;
119  
120 1 $class->$orig(api_key => $api_key)
121 2 };
122  
123  
124 0 ▶method login(Str $login, Str $password) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=2.6
125 0 my $res = $self->_post(POST => 'http://slimtimer.com/users/token',
126 { user => { email => $login, password => $password } },
127 error => "Failed to login as \"$login\""
128 );
129  
130 0 $self->_set_user_id($res->{user_id});
131 $self->_set_access_token($res->{access_token})
132 0 }
133  
134  
135 # Helper for task-related methods: returns either the root tasks URI or the
136 # URI for the given task if the task id is specified.
137 0 ▶method _get_tasks_uri(Int $task_id?) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=2 SCAR=15.4
138 0 my $uri = "http://slimtimer.com/users/$self->{user_id}/tasks";
139 0 ▶ if ( defined $task_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $task_id) 0 0 0
140 0 $uri .= "/$task_id"
141 }
142  
143 0 return $uri
144 2 }
145  
146  
147 0 ▶method list_tasks(Bool $include_completed = 1) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=3 SCAR=22.3
148 0 ▶ my $tasks_entries = $self->_request(GET => $self->_get_tasks_uri,
Branch: 1 branch
BranchTrueFalseTotal
$include_completed ? : 0 0 0
149 params => {
150 show_completed => $include_completed ? 'yes' : 'no'
151 },
152 error => "Failed to get the tasks list"
153 );
154  
155 # The expected reply structure is an array of hashes corresponding to each
156 # task.
157 0 my @tasks;
158 0 for (@$tasks_entries) {
159 0 push @tasks, WebService::SlimTimer::Task->new(%$_);
160 }
161  
162 0 return @tasks;
163 2 }
164  
165  
166 0 ▶method create_task(Str $name) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=3.5
167 0 my $res = $self->_post(POST => $self->_get_tasks_uri,
168 { task => { name => $name } },
169 error => "Failed to create task \"$name\""
170 );
171  
172 0 return WebService::SlimTimer::Task->new($res);
173 2 }
174  
175  
176 0 ▶method delete_task(Int $task_id) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=2.6
177 0 $self->_request(DELETE => $self->_get_tasks_uri($task_id),
178 error => "Failed to delete the task $task_id"
179 );
180 2 }
181  
182  
183 0 ▶method get_task(Int $task_id) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=3.5
184 0 my $res = $self->_request(GET => $self->_get_tasks_uri($task_id),
185 error => "Failed to find the task $task_id"
186 );
187  
188 0 return WebService::SlimTimer::Task->new($res);
189 2 }
190  
191  
192 0 ▶method complete_task(Int $task_id, TimeStamp $completed_on) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=2.6
193 0 $self->_post(PUT => $self->_get_tasks_uri($task_id),
194 { task => { completed_on => $self->_format_time($completed_on) } },
195 error => "Failed to mark the task $task_id as completed"
196 );
197 2 }
198  
199  
200  
201 # Helper for time-entry-related methods: returns either the root time entries
202 # URI or the URI for the given entry if the time entry id is specified.
203 0 ▶method _get_entries_uri(Int $entry_id?) {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=2 SCAR=15.4
204 0 my $uri = "http://slimtimer.com/users/$self->{user_id}/time_entries";
205 0 ▶ if ( defined $entry_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry_id) 0 0 0
206 0 $uri .= "/$entry_id"
207 }
208  
209 0 return $uri
210 2 }
211  
212 # Common part of list_entries() and list_task_entries()
213 0 ▶method _list_entries(
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=5 SCAR=32.5
214 0 ▶ Maybe[Int] $taskId,
Branch: 1 branch
BranchTrueFalseTotal
defined $taskId ? : 0 0 0
215 OptionalTimeStamp $start,
216 OptionalTimeStamp $end) {
217 my $uri = defined $taskId
218 0 ? $self->_get_tasks_uri($taskId) . "/time_entries"
219 0 ▶ : $self->_get_entries_uri;
Branch: 1 branch
BranchTrueFalseTotal
if defined $start 0 0 0
220 0 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
if defined $end 0 0 0
221 my %params;
222 0 $params{'range_start'} = $self->_format_time($start) if defined $start;
223 $params{'range_end'} = $self->_format_time($end) if defined $end;
224  
225 my $entries = $self->_request(GET => $uri,
226 params => \%params,
227 0 error => "Failed to get the entries list"
228 0 );
229 0  
230 my @time_entries;
231 for (@$entries) {
232 0 push @time_entries, WebService::SlimTimer::TimeEntry->new($_);
233 2 }
234  
235 return @time_entries;
236 0 ▶}
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=2.6
237 0  
238 2  
239 method list_entries(TimeStamp :$start, TimeStamp :$end) {
240 return $self->_list_entries(undef, $start, $end);
241 0 ▶}
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=2.6
242 0  
243 2  
244 method list_task_entries(Int $taskId, TimeStamp :$start, TimeStamp :$end) {
245 return $self->_list_entries($taskId, $start, $end);
246 0 ▶}
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=3.5
247 0  
248  
249 method get_entry(Int $entryId) {
250 my $res = $self->_request(GET => $self->_get_entries_uri($entryId),
251 0 error => "Failed to get the entry $entryId"
252 2 );
253  
254 return WebService::SlimTimer::TimeEntry->new($res);
255 0 ▶}
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=2 SCAR=15.1
256 0 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
unless defined $end 0 0 0
257  
258 0 method create_entry(Int $taskId, TimeStamp $start, TimeStamp $end?) {
259 $end = DateTime->now if !defined $end;
260  
261 my $res = $self->_post(POST => $self->_get_entries_uri, {
262 time_entry => {
263 task_id => $taskId,
264 start_time => $self->_format_time($start),
265 end_time => $self->_format_time($end),
266 duration_in_seconds => $end->epoch() - $start->epoch(),
267 }
268 },
269 0 error => "Failed to create new entry for task $taskId"
270 2 );
271  
272 return WebService::SlimTimer::TimeEntry->new($res);
273 0 ▶}
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=2.6
274 0  
275  
276 method update_entry(
277 Int $entry_id,
278 Int $taskId,
279 TimeStamp $start,
280 TimeStamp $end) {
281 $self->_post(PUT => $self->_get_entries_uri($entry_id), {
282 time_entry => {
283 task_id => $taskId,
284 2 start_time => $self->_format_time($start),
285 end_time => $self->_format_time($end),
286 duration_in_seconds => $end->epoch() - $start->epoch(),
287 0 ▶ }
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=2.6
288 0 },
289 error => "Failed to update the entry $entry_id"
290 );
291 2 }
292  
293 2  
294 method delete_entry(Int $entry_id) {
295 $self->_request(DELETE => $self->_get_entries_uri($entry_id),
296 error => "Failed to delete the entry $entry_id"
297 );
298 }
299  
300 1;
301  
302 __END__
↑ Summary SlimTimer/TimeEntry.pm »