blib/lib/Net/Xero.pm

filter: Statementstmt 31.5% 59 / 187 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 45.1% 14 / 31 Podpod 92.8% 13 / 14 totaltotal 29.9% 86 / 287 CC 28 SCAR 36.4
CC
28
Cov
25%
_talk
132.0
get_invoices
56.0
_template
30.0
CRAP 38.0
↑ Summary
1 package Net::Xero;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strictures 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Net::OAuth;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Random qw(rand_chars);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use XML::LibXML::Simple qw(XMLin);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::ShareDir 'dist_dir';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Crypt::OpenSSL::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use IO::All;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 $Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A;
19  
20 =head1 NAME
21  
22 Net::Xero - Interface to Xero accounting
23  
24 =head1 VERSION
25  
26 Version 0.40
27  
28 =cut
29  
30 1 our $VERSION = '0.20';
31  
32 1 has 'api_url' => (
33 is => 'rw',
34 default => 'https://api.xero.com',
35 );
36  
37 has 'ua' => (
38 is => 'rw',
39 0 ▶ default => sub { LWP::UserAgent->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 1 );
41  
42 1 has 'debug' => (
43 is => 'rw',
44 default => 0,
45 );
46  
47 1 has 'error' => (
48 is => 'rw',
49 predicate => 'has_error',
50 clearer => 'clear_error',
51 );
52  
53 1 has 'key' => (is => 'rw');
54 1 has 'secret' => (is => 'rw');
55 1 has 'cert' => (is => 'rw');
56  
57 1 has 'nonce' => (
58 is => 'ro',
59 default => join('', rand_chars(size => 16, set => 'alphanumeric')),
60 );
61  
62 1 has 'login_link' => (is => 'rw');
63  
64 1 has 'callback_url' => (
65 is => 'rw',
66 default => 'http://localhost:3000/callback',
67 );
68  
69 1 has 'request_token' => (is => 'rw');
70 1 has 'request_secret' => (is => 'rw');
71 1 has 'access_token' => (is => 'rw');
72 1 has 'access_secret' => (is => 'rw');
73  
74 1 has 'template_path' => (
75 is => 'rw',
76 default => ( dist_dir('Net-Xero') ),
77 );
78  
79 #has 'template_path' => (is => 'rw', isa => 'Str');
80  
81 =head1 SYNOPSIS
82  
83 Quick summary of what the module does.
84  
85 For a private application you will receive the access_token/secret when you
86 submit your X509 to xero. You can ignore login/auth in this instance as follows:
87 use Net::Xero;
88  
89 my $foo = Net::Xero->new(
90 access_token => 'YY',
91 access_secret => 'XX',
92 );
93  
94 =head1 EXPORT
95  
96 A list of functions that can be exported. You can delete this section
97 if you don't export anything, such as for a purely object-oriented module.
98  
99 =head1 FUNCTIONS
100  
101 =cut
102  
103 =head2 login
104  
105 This sets up the initial OAuth handshake and returns the login URL. This
106 URL has to be clicked by the user and the the user then has to accept
107 the application in xero.
108  
109 Xero then redirects back to the callback URL defined with
110 C<$self-E<gt>callback_url>. If the user already accepted the application the
111 redirect may happen without the user actually clicking anywhere.
112  
113 =cut
114  
115 sub login {
116 0 ▶ my $self = shift;
Subroutine login: not called CC=4 SCAR=30.0
Pod documented
117  
118 0 my $request = Net::OAuth->request("request token")->new(
119 consumer_key => $self->key,
120 consumer_secret => $self->secret,
121 request_url => $self->api_url . '/oauth/RequestToken',
122 request_method => 'POST',
123 signature_method => 'RSA-SHA1',
124 timestamp => time,
125 nonce => $self->nonce,
126 callback => $self->callback_url,
127 );
128  
129 0 my $private_key = Crypt::OpenSSL::RSA->new_private_key($self->cert);
130 0 $request->sign($private_key);
131 0 my $res = $self->ua->request(GET $request->to_url);
132  
133 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
134 0 my $response =
135 Net::OAuth->response('request token')
136 ->from_post_body($res->content);
137 0 $self->request_token($response->token);
138 0 $self->request_secret($response->token_secret);
139 0 ▶ print STDERR "Got Request Token ", $response->token, "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
140 if $self->debug;
141 0 ▶ print STDERR "Got Request Token Secret ", $response->token_secret, "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
142 if $self->debug;
143 return
144 0 $self->api_url
145 . '/oauth/Authorize?oauth_token='
146 . $response->token
147 . '&oauth_callback='
148 . $self->callback_url;
149 }
150 else {
151 0 $self->error($res->status_line);
152 0 warn "Something went wrong: " . $res->status_line;
153 }
154 }
155  
156 =head2 auth
157  
158 The auth method changes the initial request token into access token that we need
159 for subsequent access to the API. This method only has to be called once
160 after login.
161  
162 =cut
163  
164 sub auth {
165 0 ▶ my $self = shift;
Subroutine auth: not called CC=4 SCAR=30.0
Pod documented
166  
167 0 my $request = Net::OAuth->request("access token")->new(
168 consumer_key => $self->key,
169 consumer_secret => $self->secret,
170 request_url => $self->api_url . '/oauth/AccessToken',
171 request_method => 'POST',
172 signature_method => 'RSA-SHA1',
173 timestamp => time,
174 nonce => $self->nonce,
175 callback => $self->callback_url,
176 token => $self->request_token,
177 token_secret => $self->request_secret,
178 );
179 0 my $private_key = Crypt::OpenSSL::RSA->new_private_key($self->cert);
180 0 $request->sign($private_key);
181 0 my $res = $self->ua->request(GET $request->to_url);
182  
183 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
184 0 my $response =
185 Net::OAuth->response('access token')->from_post_body($res->content);
186 0 $self->access_token($response->token);
187 0 $self->access_secret($response->token_secret);
188 0 ▶ print STDERR "Got Access Token ", $response->token, "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
189 if $self->debug;
190 0 ▶ print STDERR "Got Access Token Secret ", $response->token_secret, "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
191 if $self->debug;
192 }
193 else {
194 0 $self->error($res->status_line);
195 0 $self->error($res->status_line . "\n" . $res->content);
196 }
197 }
198  
199 =head2 set_cert
200  
201 =cut
202  
203 sub set_cert {
204 0 ▶ my ($self, $path) = @_;
Subroutine set_cert: not called CC=1 SCAR=6.9
Pod documented
205 0 my $cert = io $path;
206 0 $self->cert($cert->all);
207 }
208  
209 =head2 get_inv_by_ref
210  
211 =cut
212  
213 sub get_inv_by_ref {
214 0 ▶ my ($self, @ref) = @_;
Subroutine get_inv_by_ref: not called CC=2 SCAR=17.9
Pod documented
215  
216 0 my $path = 'Invoices?where=Reference.ToString()=="' . (shift @ref) . '"';
217 0 $path .= ' OR Reference.ToString()=="' . $_ . '"' foreach (@ref);
218  
219 0 return $self->_talk($path, 'GET');
220 }
221  
222 =head2 get_invoices
223  
224 =cut
225  
226 sub get_invoices {
227 0 ▶ my ($self, $where) = @_;
Subroutine get_invoices: not called CC=7 SCAR=40.3
Pod documented
228  
229 0 my $path = 'Invoices';
230  
231 0 ▶ return $self->_talk($path, 'GET') unless (ref $where eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $where eq "HASH" 0 0 0
232  
233 0 $path .= '?where=';
234 my $conjunction =
235 0 ▶ (exists $where->{'conjunction'}) ? uc $where->{'conjunction'} : 'OR';
Branch: 1 branch
BranchTrueFalseTotal
exists $where->{'conjunction'} ? : 0 0 0
236 0 my $first = 1;
237  
238 0 foreach my $key (%{$where}) {
239 0 ▶ $path .= " $conjunction " unless $first;
Branch: 1 branch
BranchTrueFalseTotal
unless $first 0 0 0
240  
241 0 given ($key) {
242 0 when ('reference') {
243 0 my @refs = @{ $where->{$key} };
244 0 $path .= 'Reference.ToString()=="' . (shift @refs) . '"';
245 $path .= ' OR Reference.ToString()=="' . $_ . '"'
246 0 foreach (@refs);
247 }
248 0 when ('contact') {
249 0 my @contacts = @{ $where->{$key} };
250 0 my $contact = shift @contacts;
251 $path .= join(
252 ' AND ',
253 map {
254 "Contact."
255 . ucfirst($_) . '=="'
256 0 . $contact->{$_} . '"'
257 0 } keys %{$contact});
258  
259 # finish foreach
260 }
261 0 when ('number') {
262 0 my @numbers = @{ $where->{$key} };
263 $path .= ' OR InvoiceNumber.ToString()=="' . $_ . '"'
264 0 foreach (@numbers);
265 }
266 }
267  
268 0 $first = 0;
269 }
270  
271 0 return $self->_talk($path, 'GET');
272 }
273  
274 =head2 create_invoice
275  
276 =cut
277  
278 sub create_invoice {
279 0 ▶ my ($self, $hash) = @_;
Subroutine create_invoice: not called CC=1 SCAR=6.9
Pod documented
280 0 $hash->{command} = 'create_invoice';
281 0 return $self->_talk('Invoices', 'POST', $hash);
282 }
283  
284 sub void_invoice {
285 0 ▶ my ($self, $guid) = @_;
Subroutine void_invoice: not called CC=1 SCAR=6.9
Pod undocumented
286 0 my $hash = { guid => $guid } ;
287 0 $hash->{command} = 'void_invoice';
288 0 return $self->_talk('Invoices', 'POST', $hash );
289 }
290  
291 =head2 create_payment
292  
293 =cut
294  
295 sub create_payment {
296 0 ▶ my ($self, $data) = @_;
Subroutine create_payment: not called CC=1 SCAR=6.9
Pod documented
297 0 $data->{command} = 'payments';
298 0 return $self->_talk('Payments', 'POST', $data);
299 }
300  
301 =head2 create_contact
302  
303 =cut
304  
305 sub create_contact {
306 0 ▶ my ($self, $data) = @_;
Subroutine create_contact: not called CC=1 SCAR=6.9
Pod documented
307 0 $data->{command} = 'create_contact';
308 0 $data->{Contacts}->{Contact} = $data;
309 0 return $self->_talk('Contacts', 'POST', $data);
310 }
311  
312 =head2 approve_credit_note
313  
314 =cut
315  
316 sub approve_credit_note {
317 0 ▶ my ($self, $hash) = @_;
Subroutine approve_credit_note: not called CC=1 SCAR=6.9
Pod documented
318 0 $hash->{command} = 'approve_credit_note';
319 0 return $self->_talk('CreditNotes', 'POST', $hash);
320 }
321  
322 =head2 status_invoice
323  
324 =cut
325  
326 sub status_invoice {
327 0 ▶ my ($self, $hash) = @_;
Subroutine status_invoice: not called CC=1 SCAR=6.9
Pod documented
328 0 $hash->{command} = 'status_invoice';
329 0 return $self->_talk('Invoices', 'POST', $hash);
330 }
331  
332 =head2 get
333  
334 =cut
335  
336 sub get {
337 0 ▶ my ($self, $command) = @_;
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
338 0 return $self->_talk($command, 'GET');
339 }
340  
341 =head2 post
342  
343 =cut
344  
345 sub post {
346 0 ▶ my ($self, $command, $hash) = @_;
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
347 0 return $self->_talk($command, 'POST', $hash);
348 }
349  
350 =head2 put
351  
352 =cut
353  
354 sub put {
355 0 ▶ my ($self, $command, $hash) = @_;
Subroutine put: not called CC=1 SCAR=6.9
Pod documented
356 0 return $self->_talk($command, 'PUT', $hash);
357 }
358  
359 =head1 INTERNAL API
360  
361 =head2 _talk
362  
363 _talk handles the access to the restricted resources. You should
364 normally not need to access this directly.
365  
366 =cut
367  
368 sub _talk {
369 0 ▶ my ($self, $command, $method, $hash) = @_;
Subroutine _talk: not called CC=11 SCAR=48.8
370  
371 0 $self->clear_error;
372  
373 0 my $path = join('', map(ucfirst, split(/_/, $command)));
374  
375 0 my $request_url = $self->api_url . '/api.xro/2.0/' . $path;
376 0 my %opts = (
377 consumer_key => $self->key,
378 consumer_secret => $self->secret,
379 request_url => $request_url,
380 request_method => $method,
381 signature_method => 'RSA-SHA1',
382 timestamp => time,
383 nonce => join('', rand_chars(size => 16, set => 'alphanumeric')),
384 token => $self->access_token,
385 token_secret => $self->access_secret,
386 );
387  
388 0 my $content;
389 0 ▶ if ($method =~ m/^(POST|PUT)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^(POST|PUT)$/) 0 0 0
390 0 ▶ $hash->{command} ||= $command;
Condition0%0/3
$hash->{"command"} ||= $command
l!l&&r!l&&!r
000
MC/DC0%0/2
$hash->{"command"} ||= $command
$hash->{"command"} $command
Truth table0%0/3
A ||= B
A= $hash->{"command"}B= $command
ABresult
1X1
011
000
391 0 $content = $self->_template($hash);
392 0 ▶ $opts{extra_params} = { xml => $content } if ($method eq 'POST');
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "POST" 0 0 0
393 }
394  
395 0 my $request = Net::OAuth->request("protected resource")->new(%opts);
396 0 my $private_key = Crypt::OpenSSL::RSA->new_private_key($self->cert);
397 0 $request->sign($private_key);
398 #my $req = HTTP::Request->new($method, $request->to_url);
399 0 my $req = HTTP::Request->new($method, $request_url);
400 0 ▶ if ($hash and ($method eq 'POST')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash and $method eq 'POST') { } 0 0 0
Condition0%0/3
$hash and $method eq "POST"
!ll&&!rl&&r
000
MC/DC0%0/2
$hash and $method eq "POST"
$hash $method eq "POST"
Truth table0%0/3
A and B
A= $hashB= $method eq "POST"
ABresult
0X0
100
111
401 0 $req->content($request->to_post_body);
402 0 $req->header('Content-Type' =>
403 'application/x-www-form-urlencoded; charset=utf-8');
404 }
405 else {
406 0 ▶ $req->content($content) if ($hash and ($method eq 'PUT'));
Branch: 1 branch
BranchTrueFalseTotal
if $hash and $method eq "PUT" 0 0 0
Condition0%0/3
$hash and $method eq "PUT"
!ll&&!rl&&r
000
MC/DC0%0/2
$hash and $method eq "PUT"
$hash $method eq "PUT"
Truth table0%0/3
A and B
A= $hashB= $method eq "PUT"
ABresult
0X0
100
111
407 0 $req->header(Authorization => $request->to_authorization_header);
408 }
409  
410 0 ▶ print STDERR $req->as_string if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
411  
412 0 my $res = $self->ua->request($req);
413  
414 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
415 0 ▶ print STDERR "Got Content ", $res->content, "\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
416 0 return XMLin($res->content);
417 }
418 else {
419 0 warn "Something went wrong: " . $res->content;
420 0 $self->error($res->status_line . " " . $res->content);
421 }
422  
423 0 return;
424 }
425  
426 =head2 _template
427  
428 =cut
429  
430 sub _template {
431 0 ▶ my ($self, $hash) = @_;
Subroutine _template: not called CC=5 SCAR=34.0
432  
433 0 $hash->{command} .= '.tt';
434 0 ▶ print STDERR Dumper($hash) if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
435 0 my $tt;
436 0 ▶ if ($self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) { } 0 0 0
437 0 $tt = Template->new(
438 #DEBUG => 'all',
439 INCLUDE_PATH => [ $self->template_path ],
440 );
441 }
442 else {
443 0 $tt = Template->new(INCLUDE_PATH => [ $self->template_path ]);
444 }
445  
446 0 my $template = '';
447 0 ▶ $tt->process('frame.tt', $hash, \$template)
Branch: 1 branch
BranchTrueFalseTotal
$tt->process("frame.tt", $hash, \$template) or die $tt->error 0 0 0
448 || die $tt->error;
449 0 utf8::encode($template);
450 0 ▶ print STDERR $template if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
451  
452 0 return $template;
453 }
454  
455 =head1 AUTHOR
456  
457 Lenz Gschwendtner, C<< <norbu09 at cpan.org> >>
458  
459 =head1 BUGS
460  
461 Please report any bugs or feature requests to C<bug-net-xero at rt.cpan.org>, or through
462 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Xero>. I will be notified, and then you'll
463 automatically be notified of progress on your bug as I make changes.
464  
465  
466  
467  
468 =head1 SUPPORT
469  
470 You can find documentation for this module with the perldoc command.
471  
472 perldoc Net::Xero
473  
474  
475 You can also look for information at:
476  
477 =over 4
478  
479 =item * RT: CPAN's request tracker
480  
481 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Xero>
482  
483 =item * AnnoCPAN: Annotated CPAN documentation
484  
485 L<http://annocpan.org/dist/Net-Xero>
486  
487 =item * CPAN Ratings
488  
489 L<http://cpanratings.perl.org/d/Net-Xero>
490  
491 =item * Search CPAN
492  
493 L<http://search.cpan.org/dist/Net-Xero/>
494  
495 =back
496  
497  
498 =head1 ACKNOWLEDGEMENTS
499  
500  
501 =head1 COPYRIGHT & LICENSE
502  
503 Copyright 2010 Lenz Gschwendtner.
504  
505 This program is free software; you can redistribute it and/or modify it
506 under the terms of either: the GNU General Public License as published
507 by the Free Software Foundation; or the Artistic License.
508  
509 See http://dev.perl.org/licenses/ for more information.
510  
511  
512 =cut
513  
514 1 __PACKAGE__->meta->make_immutable();
↑ Summary