blib/lib/WebService/RTMAgent.pm

filter: Statementstmt 69.0% 78 / 113 Branchbran 52.1% 24 / 46 Conditioncond 44.4% 4 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 85.7% 18 / 21 Podpod 100% 11 / 11 totaltotal 66.5% 137 / 206 CC 28 SCAR 22.1
CC
28
Cov
63%
init
21.6
AUTOLOAD
19.4
rtm_request
7.2
CRAP 9.2
↑ Summary
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package WebService::RTMAgent 0.603;
4 # ABSTRACT: a user agent for the Remember The Milk API
5  
6 #pod =head1 SYNOPSIS
7 #pod
8 #pod $ua = WebService::RTMAgent->new;
9 #pod $ua->api_key($key_provided_by_rtm);
10 #pod $ua->api_secret($secret_provided_by_rtm);
11 #pod $ua->init;
12 #pod $url = $ua->get_auth_url; # then do something with the URL
13 #pod $res = $ua->tasks_getList('filter=status:incomplete');
14 #pod
15 #pod ...
16 #pod
17 #pod =head1 DESCRIPTION
18 #pod
19 #pod WebService::RTMAgent is a Perl implementation of the rememberthemilk.com API.
20 #pod
21 #pod =head2 Calling API methods
22 #pod
23 #pod All API methods documented at L<https://www.rememberthemilk.com/services/api/>
24 #pod can be called as methods, changing dots for underscores and optionnaly taking
25 #pod off the leading 'rtm': C<< $ua->auth_checkToken >>, C<< $ua->tasks_add >>, etc.
26 #pod
27 #pod Parameters should be given as a list of strings, e.g.:
28 #pod
29 #pod $ua->tasks_complete(
30 #pod "list_id=4231233",
31 #pod "taskseries_id=124233",
32 #pod "task_id=1234",
33 #pod );
34 #pod
35 #pod Refer to the API documentation for each method's parameters.
36 #pod
37 #pod Return values are the XML response, parsed through L<XML::Simple>. Please refer
38 #pod to XML::Simple for more information (and Data::Dumper, to see what the values
39 #pod look like) and the sample B<rtm> script for examples.
40 #pod
41 #pod If the method call was not successful, C<undef> is returned, and an error
42 #pod message is set which can be accessed with the B<error> method:
43 #pod
44 #pod $res = $ua->tasks_getList;
45 #pod die $ua->error unless defined $res;
46 #pod
47 #pod Please note that at this stage, I am not very sure that this is the best way to implement the API. "It works for me," but:
48 #pod
49 #pod =for :list
50 #pod * Parameters may turn to hashes at some point
51 #pod * Output values may turn to something more abstract and useful,
52 #pod as I gain experience with API usage.
53 #pod
54 #pod =head2 Authentication and authorisation
55 #pod
56 #pod Before using the API, you need to authenticate it. If you are going to be
57 #pod building a desktop application, you should get an API key and shared secret
58 #pod from the people at rememberthemilk.com (see
59 #pod L<https://groups.google.com/group/rememberthemilk-api/browse_thread/thread/dcb035f162d4dcc8>
60 #pod for rationale) and provide them to RTMAgent.pm with the C<api_key> and
61 #pod C<api_secret> methods.
62 #pod
63 #pod You then need to proceed through the authentication cycle: create a useragent,
64 #pod call the get_auth_url method and direct a Web browser to the URL it returns.
65 #pod There RememberTheMilk will present you with an authorisation page: you can
66 #pod authorise the API to access your account.
67 #pod
68 #pod At that stage, the API will get a token which identifies the API/user
69 #pod authorisation. B<RTMAgent> saves the token in a file, so you should never need
70 #pod to do the authentication again.
71 #pod
72 #pod =head2 Proxy and other strange things
73 #pod
74 #pod The object returned by B<new> is also a LWP::UserAgent. This means you can
75 #pod configure it the same way, in particular to cross proxy servers:
76 #pod
77 #pod $ua = new WebService::RTMAgent;
78 #pod $ua->api_key($key);
79 #pod $ua->api_secret($secret);
80 #pod $ua->proxy('http', 'https://proxy:8080');
81 #pod $ua->init;
82 #pod $list = $ua->tasks_getList;
83 #pod
84 #pod Incidentally, this is the reason why the C<init> method exists: C<init> needs
85 #pod to access the network, so its work cannot be done in C<new> as that would leave
86 #pod no opportunity to configure the LWP::UserAgent.
87 #pod
88 #pod =cut
89  
90 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 4 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
92 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 4 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 4 ▶use parent 'LWP::UserAgent';
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 4 my $REST_endpoint = "https://api.rememberthemilk.com/services/rest/";
98 4 my $auth_endpoint = "https://api.rememberthemilk.com/services/auth/";
99  
100 4 our $config_file = "$ENV{HOME}/.rtmagent";
101 4 our $config; # reference to config hash
102  
103 #pod =head1 PUBLIC METHODS
104 #pod
105 #pod =head2 $ua = WebService::RTMAgent->new;
106 #pod
107 #pod Creates a new agent.
108 #pod
109 #pod =cut
110  
111 7 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
112 7 my $class = shift;
113 7 ▶ Carp::confess("tried to call ->new on an instance") if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 7 7
114 7 my $self = $class->SUPER::new(@_);
115 7 $self->verbose('');
116 7 return bless $self, $class;
117 }
118  
119 #pod =head2 $ua->api_key($key);
120 #pod
121 #pod =head2 $ua->api_secret($secret);
122 #pod
123 #pod Set the API key and secret. These are obtained from the people are
124 #pod RememberTheMilk.com.
125 #pod
126 #pod =head2 $ua->verbose('netin netout');
127 #pod
128 #pod Sets what type of traces the module should print. You can use 'netout' to print
129 #pod all the outgoing messages, 'netin' to print all the incoming messages.
130 #pod
131 #pod =head2 $err = $ua->error;
132 #pod
133 #pod Get a message describing the last error that happened.
134 #pod
135 #pod =cut
136  
137 # Create accessors
138 4 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.1
139 4 my $subs;
140 4 foreach my $data ( qw/error verbose api_secret api_key/ ) {
141 16 $subs .= qq{
142 sub $data {
143 \$_[0]->{rtma_$data} =
144 defined \$_[1] ? \$_[1] : \$_[0]->{rtma_$data};
145 }
146 }
147 }
148 0 ▶ eval $subs;
Subroutine api_key: called CC=2 SCAR=7.2
Subroutine api_secret: called CC=2 SCAR=7.2
Subroutine error: not called CC=2 SCAR=7.2
Subroutine verbose: called CC=2 SCAR=7.2
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 4 branches
BranchTrueFalseTotal
defined $_[1] ? : 3 5 8
defined $_[1] ? : 3 6 9
defined $_[1] ? : 0 0 0
defined $_[1] ? : 7 5 12
149 }
150  
151 #pod =head2 $ua->init;
152 #pod
153 #pod Performs authentication with RTM and various other book-keeping
154 #pod initialisations.
155 #pod
156 #pod =cut
157  
158 6 ▶sub init {
Subroutine init: called CC=11 SCAR=30.7
Pod documented
159 6 my ($self) = @_;
160  
161 6 ▶ if (-e $config_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $WebService::RTMAgent::config_file) 4 2 6
162 4 ▶ die "$config_file: can't read or write\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -r $WebService::RTMAgent::config_file and -w $WebService::RTMAgent::config_file 2 2 4
Condition100%3/3
-r $WebService::RTMAgent::config_file and -w $WebService::RTMAgent::config_file
!ll&&!rl&&r
112
MC/DC100%2/2
-r $WebService::RTMAgent::config_file and -w $WebService::RTMAgent::config_file
-r $WebService::RTMAgent::config_file -w $WebService::RTMAgent::config_file
Truth table100%3/3
A and B
A= -r $WebService::RTMAgent::config_fileB= -w $WebService::RTMAgent::config_file
ABresult
0X0
100
111
163 unless -r $config_file and -w $config_file;
164  
165 2 my $ok = eval {
166 2 $config = XMLin($config_file, KeyAttr=>'', ForceArray => ['undo']);
167 1 1;
168 };
169 2 ▶ croak "$config_file: Invalid XML file" unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 1 2
170 }
171  
172 # Check Token
173 3 ▶ if ($config->{token}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WebService::RTMAgent::config->{"token"}) 2 1 3
174 2 my $res = $self->auth_checkToken;
175 0 ▶ if (not defined $res) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 0 0 0
176 0 delete $config->{frob};
177 0 delete $config->{token};
178 0 croak $self->error;
179 }
180 }
181  
182 # If we have a frob and no token, we're half-way through
183 # authentication -- finish it
184 1 ▶ if ($config->{frob} and not $config->{token}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WebService::RTMAgent::config->{"frob"} and not $WebService::RTMAgent::config->{"token"}) 1 0 1
Condition33%1/3
$WebService::RTMAgent::config->{"frob"} and not $WebService::RTMAgent::config->{"token"}
!ll&&!rl&&r
001
MC/DC0%0/2
$WebService::RTMAgent::config->{"frob"} and not $WebService::RTMAgent::config->{"token"}
$WebService::RTMAgent::config->{"frob"} not $WebService::RTMAgent::config->{"token"}
Truth table33%1/3
A and not(B)
A= $WebService::RTMAgent::config->{"frob"}B= not $WebService::RTMAgent::config->{"token"}
ABresult
0X0
100
111
185 1 warn "frobbed -- getting token\n";
186 1 my $res = $self->auth_getToken("frob=$config->{frob}");
187 0 ▶ die $self->error."(Maybe you need to erase $config_file)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res 0 0 0
188 unless defined $res;
189 0 $config->{token} = $res->{auth}->[0]->{token}->[0];
190 0 warn "token $config->{token}\n";
191 }
192  
193 # If we have no timeline, get one
194 0 ▶ unless ($config->{timeline}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($WebService::RTMAgent::config->{"timeline"}) 0 0 0
195 0 my $res = $self->timelines_create();
196 0 $config->{timeline} = $res->{timeline}->[0];
197 0 $config->{undo} = [];
198 }
199 }
200  
201 #pod =head2 $ua->get_auth_url;
202 #pod
203 #pod Performs the beginning of the authentication: this returns a URL to which
204 #pod the user must then go to allow RTMAgent to access his or her account.
205 #pod
206 #pod This mecanism is slightly contrieved and designed so that users do not have
207 #pod to give their username and password to third party software (like this one).
208 #pod
209 #pod =cut
210  
211 1 ▶sub get_auth_url {
Subroutine get_auth_url: called CC=1 SCAR=3.9
Pod documented
212 1 my ($self) = @_;
213  
214 1 my $res = $self->auth_getFrob();
215  
216 0 my $frob = $res->{'frob'}->[0];
217  
218 0 my @params;
219 0 push @params, "api_key=".$self->api_key, "perms=delete", "frob=$frob";
220 0 push @params, "api_sig=".($self->sign(@params));
221  
222 0 my $url = "$auth_endpoint?". (join '&', @params);
223  
224 # save frob for later
225 0 $config->{'frob'} = $frob;
226  
227 0 return $url;
228 }
229  
230 #pod =head2 @undo = $ua->get_undoable;
231 #pod
232 #pod Returns the transactions which we know how to undo (unless data has been lost,
233 #pod that's all the undo-able transaction that go with the timeline that is saved in
234 #pod the state file).
235 #pod
236 #pod The value returned is a list of { id, op, [ params ] } with id the transaction
237 #pod id, op the API method that was called, and params the API parameters that were
238 #pod called.
239 #pod
240 #pod =cut
241  
242 0 ▶sub get_undoable {
Subroutine get_undoable: not called CC=1 SCAR=6.9
Pod documented
243 0 my ($self) = @_;
244  
245 0 return $config->{undo};
246 }
247  
248 #pod =head2 $ua->clear_undo(3);
249 #pod
250 #pod Removes an undo entry.
251 #pod
252 #pod =cut
253  
254 0 ▶sub clear_undo {
Subroutine clear_undo: not called CC=1 SCAR=6.9
Pod documented
255 0 my ($self, $index) = @_;
256  
257 0 splice @{$config->{undo}}, $index, 1;
258 }
259  
260 #pod =head1 PRIVATE METHODS
261 #pod
262 #pod Don't use those and we'll stay friends.
263 #pod
264 #pod =head2 $ua->sign(@params);
265 #pod
266 #pod Returns the md5 signature for signing parameters. See RTM Web site for details.
267 #pod This should only be useful for the module, don't use it.
268 #pod
269 #pod =cut
270  
271 6 ▶sub sign {
Subroutine sign: called CC=1 SCAR=0.0
Pod documented
272 6 my ($self, @params) = @_;
273  
274 6 my $sign_str = join '', sort @params;
275 6 $sign_str =~ s/=//g;
276  
277 6 return md5_hex($self->api_secret."$sign_str");
278 }
279  
280 #pod =head2 $ua->rtm_request("rtm.tasks.getList", "list_id=234", "taskseries_id=2"..)
281 #pod
282 #pod Signs the parameters, performs the request, returns a parsed XML::Simple
283 #pod object.
284 #pod
285 #pod =cut
286  
287 5 ▶sub rtm_request {
Subroutine rtm_request: called CC=6 SCAR=19.7
Pod documented
288 5 my ($self, $request, @params) = @_;
289  
290 5 unshift @params, "method=$request";
291 5 push @params, "api_key=".$self->api_key;
292 5 ▶ push @params, "auth_token=$config->{token}" if exists $config->{token};
Branch: 1 branch
BranchTrueFalseTotal
if exists $WebService::RTMAgent::config->{"token"} 3 2 5
293 5 ▶ push @params, "timeline=$config->{timeline}" if exists $config->{timeline};
Branch: 1 branch
BranchTrueFalseTotal
if exists $WebService::RTMAgent::config->{"timeline"} 1 4 5
294 5 my $sig = $self->sign(@params);
295 5 my $param = join '&', @params;
296  
297 5 my $req = HTTP::Request->new( POST => $REST_endpoint);
298 5 $req->content_type('application/x-www-form-urlencoded');
299 5 $req->content("$param&api_sig=$sig");
300 5 ▶ warn("request:\n".$req->as_string."\n\n") if $self->verbose =~ /netout/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose =~ /netout/ 0 5 5
301  
302 5 my $res = $self->request($req);
303 0 ▶ die $res->status_line unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
304  
305 0 ▶ warn("response:\n".$res->as_string."\n\n") if $self->verbose =~ /netin/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose =~ /netin/ 0 0 0
306 0 return XMLin($res->content, KeyAttr=>'', ForceArray=>1);
307 }
308  
309 # AUTOLOAD gets calls to undefined functions
310 # we add 'rtm' and change underscores to dots, to change perl function
311 # names to RTM API: tasks_getList => rtm.tasks.getList
312 # arguments are as strings:
313 # $useragent->tasks_complete("list_id=$a", "taskseries_id=$b" ...);
314 4 our $AUTOLOAD;
315 5 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=6 SCAR=29.7
316 5 my $function = $AUTOLOAD;
317  
318 5 my $self = shift;
319  
320 5 $function =~ s/^.*:://; # Remove class name
321 5 $function =~ s/_/./g; # Change underscores to dots (auth_getFrob => auth.getFrob)
322 5 ▶ $function =~ s/^/rtm./ unless $function =~ /^rtm./; # prepends rtm if needed
Branch: 1 branch
BranchTrueFalseTotal
unless $function =~ /^rtm./ 5 0 5
323 5 my $res = $self->rtm_request($function, @_);
324  
325 # Treat errors
326 0 ▶ if (exists $res->{'err'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $res->{"err"}) 0 0 0
327 0 ▶ croak ("$function does not exist\n") if $res->{'err'}->[0]->{'code'} == 112;
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"err"}[0]{"code"} == 112 0 0 0
328 0 $self->error("$res->{'err'}->[0]->{'code'}: $res->{'err'}->[0]->{'msg'}\n");
329 0 return undef;
330 }
331  
332 # If action is undo-able, store transaction ID
333 0 ▶ if (exists $res->{transaction} and
Branch: 1 branch
BranchTrueFalseTotal
if (exists $res->{"transaction"} and exists $res->{"transaction"}[0]{"undoable"}) 0 0 0
Condition0%0/3
exists $res->{"transaction"} and exists $res->{"transaction"}[0]{"undoable"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $res->{"transaction"} and exists $res->{"transaction"}[0]{"undoable"}
exists $res->{"transaction"} exists $res->{"transaction"}[0]{"undoable"}
Truth table0%0/3
A and B
A= exists $res->{"transaction"}B= exists $res->{"transaction"}[0]{"undoable"}
ABresult
0X0
100
111
334 exists $res->{transaction}->[0]->{undoable}) {
335 0 push @{$config->{undo}}, {
336 'id' => $res->{transaction}->[0]->{id},
337 0 'op' => $function,
338 'params' => \@_,
339 };
340 }
341 0 return $res;
342 }
343  
344  
345 # When destroying the object, save the config file
346 # (careful, this all means we can only have one instance running...)
347 7 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
348 7 ▶ return unless defined $config;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $WebService::RTMAgent::config 4 3 7
349 3 open my $f, "> $config_file";
350 3 print $f XMLout($config, NoAttr=>1, RootName=>'RTMAgent');
351 }
352  
353 #pod =head1 FILES
354 #pod
355 #pod =for :list
356 #pod = F<~/.rtmagent>
357 #pod XML file containing runtime data: frob, timeline, authentication token. This
358 #pod file is overwritten on exit, which means you should only have one instance of
359 #pod RTMAgent (this should be corrected in a future version).
360 #pod
361 #pod =head1 SEE ALSO
362 #pod
363 #pod =for :list
364 #pod * C<< L<rtm|https://www.rutschle.net/rtm> >>, example command-line script.
365 #pod * L<LWP::UsrAgent>
366 #pod * L<XML::Simple>
367 #pod
368 #pod =cut
369  
370 4 1;
371  
372 __END__
↑ Summary