API.pm

filter: Statementstmt 69.1% 85 / 123 Branchbran 14.2% 2 / 14 Conditioncond 13.6% 3 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 65.6% 21 / 32 Podpod 100% 12 / 12 totaltotal 56.9% 123 / 216 CC 18 SCAR 23.3
CC
18
Cov
57%
oauth
42.0
delete_everything_from_this_test_account
30.0
ping
12.0
CRAP 10.3
« API/OAuth.pm ↑ Summary API/Role/Iterator.pm »
1 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Net::FreshBooks::API;
5 8 $Net::FreshBooks::API::VERSION = '0.24';
6 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 with 'Net::FreshBooks::API::Role::Common';
9  
10 #use namespace::autoclean;
11  
12 8 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 #use Devel::SimpleTrace;
16 8 ▶use Net::FreshBooks::API::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use Net::FreshBooks::API::Client::Contact;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use Net::FreshBooks::API::Estimate;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use Net::FreshBooks::API::Gateway;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use Net::FreshBooks::API::Invoice;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use Net::FreshBooks::API::OAuth;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 ▶use Net::FreshBooks::API::Language;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶use Net::FreshBooks::API::Payment;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use Net::FreshBooks::API::Recurring;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 8 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 8 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 8 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 8 has 'account_name' => ( is => 'rw' );
30 8 has 'auth_token' => ( is => 'rw' );
31 8 has 'api_version' => ( is => 'rw', default => 2.1 );
32 8 has 'auth_realm' => ( is => 'rw', default => 'FreshBooks' );
33 8 has 'ua' => ( is => 'rw', lazy_build => 1 );
34 8 has 'ua_name' => ( is => 'rw', lazy_build => 1 );
35  
36 # oauth methods
37 8 has 'access_token' => ( is => 'rw' );
38 8 has 'access_token_secret' => ( is => 'rw' );
39 8 has 'consumer_key' => ( is => 'rw' );
40 8 has 'consumer_secret' => ( is => 'rw' );
41  
42 0 ▶sub ping {
Subroutine ping: not called CC=3 SCAR=24.8
Pod documented
43 0 my $self = shift;
44 0 eval { $self->client->list() };
45  
46 0 ▶ $self->_log( debug => $@ ? "ping failed: $@" : "ping succeeded" );
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
47 0 ▶ return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
48 0 return 1;
49 }
50  
51 6 ▶sub service_url {
Subroutine service_url: called CC=3 SCAR=11.8
Pod documented
52 6 my $self = shift;
53 6 ▶ my $account_name = $self->account_name || $self->consumer_key;
Condition33%1/3
$self->account_name || $self->consumer_key
l!l&&r!l&&!r
600
MC/DC0%0/2
$self->account_name || $self->consumer_key
$self->account_name $self->consumer_key
Truth table33%1/3
A || B
A= $self->account_nameB= $self->consumer_key
ABresult
1X1
011
000
54  
55 6 ▶ croak "account_name required" if !$account_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $account_name 0 6 6
56  
57 6 my $uri
58 = URI->new( 'https://'
59 . $account_name
60 . '.freshbooks.com/api/'
61 . $self->api_version
62 . '/xml-in' );
63  
64 6 return $uri;
65 }
66  
67 0 ▶sub client {
Subroutine client: not called CC=1 SCAR=6.9
Pod documented
68 0 return shift->_create_object( 'Client', @_ );
69 }
70  
71 0 ▶sub estimate {
Subroutine estimate: not called CC=1 SCAR=6.9
Pod documented
72 0 return shift->_create_object( 'Estimate', @_ );
73 }
74  
75 0 ▶sub gateway {
Subroutine gateway: not called CC=1 SCAR=6.9
Pod documented
76 0 return shift->_create_object( 'Gateway', @_ );
77 }
78  
79 0 ▶sub invoice {
Subroutine invoice: not called CC=1 SCAR=6.9
Pod documented
80 0 return shift->_create_object( 'Invoice', @_ );
81 }
82  
83 0 ▶sub language {
Subroutine language: not called CC=1 SCAR=6.9
Pod documented
84 0 return shift->_create_object( 'Language', @_ );
85 }
86  
87 0 ▶sub payment {
Subroutine payment: not called CC=1 SCAR=6.9
Pod documented
88 0 return shift->_create_object( 'Payment', @_ );
89 }
90  
91 0 ▶sub recurring {
Subroutine recurring: not called CC=1 SCAR=6.9
Pod documented
92 0 return shift->_create_object( 'Recurring', @_ );
93 }
94  
95 0 ▶sub _create_object {
Subroutine _create_object: not called CC=2 SCAR=17.9
96  
97 0 my $self = shift;
98 0 my $class = 'Net::FreshBooks::API::' . shift;
99  
100 0 ▶ my $args = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
101 0 my $obj = $class->new( _fb => $self, %$args );
102 0 $obj->verbose( $self->verbose );
103 0 $obj->die_on_server_error( $self->die_on_server_error );
104  
105 0 return $obj;
106  
107 }
108  
109 2 ▶sub _build_ua_name {
Subroutine _build_ua_name: called CC=3 SCAR=12.0
110  
111 2 my $self = shift;
112 2 ▶ my $class = ref( $self ) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
200
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
113 2 ▶ my $version = $Net::FreshBooks::API::VERSION || '0.00';
Condition50%1/2
$Net::FreshBooks::API::VERSION || '0.00'
l!l
20
MC/DC0%0/1
$Net::FreshBooks::API::VERSION || '0.00'
$Net::FreshBooks::API::VERSION
Truth table50%1/2
A
A= $Net::FreshBooks::API::VERSION
Aresult
11
00
114  
115 2 return "$class (v$version)";
116  
117 }
118  
119 1 ▶sub _build_ua {
Subroutine _build_ua: called CC=1 SCAR=0.0
120 1 my $self = shift;
121  
122 1 my $ua = LWP::UserAgent->new(
123 agent => $self->ua_name,
124 protocols_allowed => ['https'],
125 keep_alive => 10,
126 );
127  
128 # authenticate with and without realms
129 1 $ua->credentials( #
130 $self->service_url->host_port, # net loc
131 $self->auth_realm, # realm
132 $self->auth_token, # username
133 '' # password (none - all in username)
134 );
135  
136 1 $ua->credentials( #
137 $self->service_url->host_port, # net loc
138 '', # realm (none)
139 $self->auth_token, # username
140 '' # password (none - all in username)
141 );
142  
143 1 return $ua;
144 }
145  
146 0 ▶sub delete_everything_from_this_test_account {
Subroutine delete_everything_from_this_test_account: not called CC=5 SCAR=34.0
Pod documented
147  
148 0 my $self = shift;
149  
150 0 my $name = $self->account_name;
151 0 ▶ croak( "ERROR: account_name must end with 'test' to use"
Branch: 1 branch
BranchTrueFalseTotal
if not $name =~ / test \z /x and $name ne "netfreshbooksapi" 0 0 0
Condition0%0/3
not $name =~ / test \z /x and $name ne "netfreshbooksapi"
!ll&&!rl&&r
000
MC/DC0%0/2
not $name =~ / test \z /x and $name ne "netfreshbooksapi"
not $name =~ / test \z /x $name ne "netfreshbooksapi"
Truth table0%0/3
not(A) and B
A= not $name =~ / test \z /xB= $name ne "netfreshbooksapi"
ABresult
0X0
100
111
152 . " the method delete_everything_on_this_test_account"
153 . " - your account name is '$name'" )
154 if ( $name !~ m{ test \z }x && $name ne 'netfreshbooksapi' );
155  
156 0 my $delete_count = 0;
157  
158 # note: 'payments' can't be deleted
159 0 my @names_to_delete = qw( invoice client );
160  
161 # clear out all existing clients etc on this account.
162 0 foreach my $object_name ( @names_to_delete ) {
163 0 my $objects_to_delete = $self->$object_name->list();
164 0 ▶ while ( my $obj = $objects_to_delete->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $objects_to_delete->next) 0 0 0
165 0 $obj->delete;
166 0 $delete_count++;
167 }
168 }
169  
170 0 return $delete_count;
171 }
172  
173 0 ▶sub oauth {
Subroutine oauth: not called CC=6 SCAR=37.4
Pod documented
174  
175 0 my $self = shift;
176  
177 0 ▶ my %tokens = (
Condition0%0/6
$self->consumer_key || undef
l!l
00
$self->consumer_secret || undef
l!l
00
$self->account_name || undef
l!l
00
MC/DC0%0/1
$self->consumer_key || undef
$self->consumer_key
MC/DC0%0/1
$self->consumer_secret || undef
$self->consumer_secret
MC/DC0%0/1
$self->account_name || undef
$self->account_name
Truth table0%0/2
A
A= $self->consumer_key
Aresult
11
00
Truth table0%0/2
A
A= $self->consumer_secret
Aresult
11
00
Truth table0%0/2
A
A= $self->account_name
Aresult
11
00
178 consumer_key => $self->consumer_key || undef,
179 consumer_secret => $self->consumer_secret || undef,
180 account_name => $self->account_name || undef,
181 );
182  
183 0 ▶ if ( $self->access_token && $self->access_token_secret ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->access_token and $self->access_token_secret) 0 0 0
Condition0%0/3
$self->access_token and $self->access_token_secret
!ll&&!rl&&r
000
MC/DC0%0/2
$self->access_token and $self->access_token_secret
$self->access_token $self->access_token_secret
Truth table0%0/3
A and B
A= $self->access_tokenB= $self->access_token_secret
ABresult
0X0
100
111
184 0 $tokens{'access_token'} = $self->access_token;
185 0 $tokens{'access_token_secret'} = $self->access_token_secret;
186 }
187  
188 0 my $oauth = Net::FreshBooks::API::OAuth->new( %tokens );
189  
190 0 return $oauth;
191  
192 }
193  
194 1 ▶sub account_name_ok {
Subroutine account_name_ok: called CC=2 SCAR=7.0
Pod documented
195  
196 1 my $self = shift;
197  
198 1 my $mech = WWW::Mechanize->new( autocheck => 0 );
199 1 $mech->agent( $self->ua_name );
200  
201 # FreshBooks redirects on all account names (valid or not)
202 1 $mech->requests_redirectable( [] );
203  
204 1 $mech->get( $self->service_url );
205  
206 # if your account name is valid, you'll get an "unauthorized" response
207 1 ▶ return ( $mech->status == 401 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$mech->status == 401 ? : 0 1 1
208  
209 }
210  
211 8 __PACKAGE__->meta->make_immutable();
212  
213 8 1;
214  
215 # ABSTRACT: Easy OO access to the FreshBooks.com API
216  
217 __END__
« API/OAuth.pm ↑ Summary API/Role/Iterator.pm »