API/Base.pm

filter: Statementstmt 21.3% 40 / 187 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 42.3% 11 / 26 Podpod 100% 13 / 13 totaltotal 22.0% 64 / 290 CC 32 SCAR 47.3
CC
32
Cov
17%
construct_element
306.0
_fill_in_from_node
110.0
response_xml_to_node
20.0
CRAP 113.4
↑ Summary API/Role/CRUD.pm »
1 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Net::FreshBooks::API::Base;
5 9 $Net::FreshBooks::API::Base::VERSION = '0.24';
6 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 with 'Net::FreshBooks::API::Role::Common';
9  
10 9 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use XML::LibXML qw( XML_ELEMENT_NODE );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 ▶use Net::FreshBooks::API::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 9 my %plural_to_singular = (
21 clients => 'client',
22 contacts => 'contact',
23 invoices => 'invoice',
24 lines => 'line',
25 payments => 'payment',
26 nesteds => 'nested', # for testing
27 );
28  
29 9 has '_fb' => ( is => 'rw', required => 0 );
30 9 has '_sent_xml' => ( is => 'rw' );
31  
32 0 ▶sub new_from_node {
Subroutine new_from_node: not called CC=1 SCAR=6.9
Pod documented
33 0 my $class = shift;
34 0 my $node = shift;
35  
36 0 my $self = bless {}, $class;
37 0 $self->_fill_in_from_node( $node );
38 0 return $self;
39 }
40  
41 0 ▶sub copy {
Subroutine copy: not called CC=1 SCAR=6.9
Pod documented
42 0 my $self = shift;
43 0 my $class = ref $self;
44 0 return $class->new( _fb => $self->_fb );
45 }
46  
47 # this method is called recursively as it works its way through the XML
48 # elements
49  
50 0 ▶sub _fill_in_from_node {
Subroutine _fill_in_from_node: not called CC=10 SCAR=47.0
51 0 my $self = shift;
52 0 my $in_node = shift; # XML::LibXML::Element
53  
54 # parse it as a new node so that the matching is more reliable
55 0 my $parser = XML::LibXML->new();
56 0 my $node = $parser->parse_string( $in_node->toString );
57  
58 # clean up all the keys
59 0 delete $self->{$_} for grep { !m/^_/x } keys %$self;
60  
61 0 my $fields_config = $self->_fields;
62  
63 # copy across the new values provided
64 0 foreach my $key ( grep { !m/^_/x } keys %$fields_config ) {
65  
66 0 my $xpath .= sprintf "//%s/%s", $self->node_name, $key;
67  
68 # check that this field is not a special one
69 0 ▶ if ( my $class = $fields_config->{$key}{made_of} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = $fields_config->{$key}{'made_of'}) { } 0 0 0
70  
71 0 my ( $match ) = $node->findnodes( $xpath );
72  
73 # avoid this error: Can't call method "childNodes" on an undefined
74 # value at Net/FreshBooks/API/Base.pm
75 # line 174
76 0 ▶ next if !$match;
Branch: 1 branch
BranchTrueFalseTotal
unless $match 0 0 0
77 0 ▶ if ( $fields_config->{$key}{presented_as} eq 'array' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fields_config->{$key}{'presented_as'} eq 'array') { } 0 0 0
elsif ($fields_config->{$key}{'presented_as'} eq 'object') { } 0 0 0
78  
79 0 my @new_objects = map { $class->new_from_node( $_ ) }
80 0 grep { $_->nodeType eq XML_ELEMENT_NODE }
81 $match->childNodes();
82 0 $self->{$key} = \@new_objects;
83 }
84  
85 elsif ( $fields_config->{$key}{presented_as} eq 'object' ) {
86  
87 0 my $inflated = $class->new;
88 0 my $f = $inflated->_fields;
89  
90 # convert XML to HASHREF as it's easier to work with
91 0 my $node_as_ref = XMLin( $match->toString );
92  
93 0 foreach my $field ( keys %{$f} ) {
94  
95 0 ▶ if ( exists $f->{$field}->{made_of} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $f->{$field}{'made_of'}) { } 0 0 0
96 0 my $new_class = $f->{$field}->{made_of};
97 0 my $obj = $new_class->new_from_node( $match );
98 0 $inflated->$field( $obj );
99 }
100 else {
101 0 $inflated->$field( $node_as_ref->{$field} );
102 }
103 }
104 0 $self->{$key} = $inflated;
105 }
106 else {
107 0 ▶ $self->{$key} #
Branch: 1 branch
BranchTrueFalseTotal
$match ? : 0 0 0
108 = $match #
109 ? $class->new_from_node( $match ) #
110 : undef;
111 }
112  
113 }
114 else {
115 0 my $val = $node->findvalue( $xpath );
116 0 $self->{$key} = $val;
117 }
118 }
119  
120 0 return $self;
121  
122 }
123  
124 0 ▶sub send_request {
Subroutine send_request: not called CC=1 SCAR=6.9
Pod documented
125 0 my $self = shift;
126 0 my $args = shift;
127  
128 0 my $method = $args->{_method};
129  
130 0 my %frequency_fix = %{ $self->_frequency_cleanup };
131  
132 0 my $pattern = join "|", keys %frequency_fix;
133  
134 0 $self->_log( debug => "Sending request for $method" );
135  
136 0 my $request_xml = $self->parameters_to_request_xml( $args );
137 0 $request_xml
138 =~ s{<frequency>($pattern)</frequency>}{<frequency>$frequency_fix{$1}</frequency>}gxms;
139  
140 0 $self->_log( debug => $request_xml );
141 0 $self->_request_xml( $request_xml );
142  
143 0 my $return_xml = $self->send_xml_to_freshbooks( $request_xml );
144  
145 0 $self->_log( debug => $return_xml );
146 0 $self->_return_xml( $return_xml );
147  
148 0 my $response_node = $self->response_xml_to_node( $return_xml );
149  
150 0 $self->_log( debug => "Received response for $method" );
151  
152 0 return $response_node;
153 }
154  
155 0 ▶sub method_string {
Subroutine method_string: not called CC=1 SCAR=6.9
Pod documented
156 0 my $self = shift;
157 0 my $action = shift;
158  
159 0 return $self->api_name . '.' . $action;
160 }
161  
162 0 ▶sub api_name {
Subroutine api_name: not called CC=2 SCAR=17.9
Pod documented
163 0 my $self = shift;
164 0 ▶ my $name = ref( $self ) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
165 0 $name =~ s{^.*::}{}x;
166 0 return lc $name;
167 }
168  
169 0 ▶sub node_name {
Subroutine node_name: not called CC=1 SCAR=6.9
Pod documented
170 0 my $self = shift;
171 0 return $self->api_name;
172 }
173  
174 0 ▶sub id_field {
Subroutine id_field: not called CC=1 SCAR=6.9
Pod documented
175 0 my $self = shift;
176 0 return $self->api_name . "_id";
177 }
178  
179 0 ▶sub field_names {
Subroutine field_names: not called CC=1 SCAR=6.9
Pod documented
180 0 my $self = shift;
181 0 my @names = sort keys %{ $self->_fields };
182 0 return @names;
183 }
184  
185 0 ▶sub field_names_rw {
Subroutine field_names_rw: not called CC=1 SCAR=6.9
Pod documented
186 0 my $self = shift;
187 0 my $fields = $self->_fields;
188  
189 my @names = sort
190 0 grep { $fields->{$_}{is} eq 'rw' }
191 keys %$fields;
192  
193 0 return @names;
194 }
195  
196 0 ▶sub parameters_to_request_xml {
Subroutine parameters_to_request_xml: not called CC=1 SCAR=6.9
Pod documented
197 0 my $self = shift;
198 0 my $params = shift;
199 0 my $parameters = clone( $params );
200  
201 0 my $dom = XML::LibXML::Document->new( '1.0', 'utf-8' );
202  
203 0 my $root = XML::LibXML::Element->new( 'request' );
204 0 $dom->setDocumentElement( $root );
205  
206 0 $self->construct_element( $root, $parameters );
207  
208 0 return $dom->toString( 1 );
209 }
210  
211 0 ▶sub construct_element {
Subroutine construct_element: not called CC=17 SCAR=57.2
Pod documented
212 0 my $self = shift;
213 0 my $element = shift;
214 0 my $hashref = shift;
215  
216 0 foreach my $key ( sort keys %$hashref ) {
217 0 my $val = $hashref->{$key};
218  
219 # avoid "Unknown currency" API error
220 0 ▶ next if $key eq 'currency_code' && !$val;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "currency_code" and not $val 0 0 0
Condition0%0/3
$key eq "currency_code" and not $val
!ll&&!rl&&r
000
MC/DC0%0/2
$key eq "currency_code" and not $val
$key eq "currency_code" not $val
Truth table0%0/3
A and not(B)
A= $key eq "currency_code"B= not $val
ABresult
0X0
100
111
221  
222 # line_id is returned, but not sent
223 0 ▶ next if $key eq 'line_id';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "line_id" 0 0 0
224  
225 # keys starting with an underscore are attributes
226 0 ▶ if ( my ( $attr_key ) = $key =~ m{ \A _ (.*) \z }x ) {
Branch: 4 branches
BranchTrueFalseTotal
if (my($attr_key) = $key =~ / \A _ (.*) \z /x) { } 0 0 0
elsif (ref $val eq '') { } 0 0 0
elsif (ref $val eq 'ARRAY') { } 0 0 0
elsif (ref $val eq 'HASH' or $val and builtin::blessed($val)) { } 0 0 0
227 0 $element->setAttribute( $attr_key, $val );
228 }
229  
230 # scalar values are text nodes
231 elsif ( ref $val eq '' ) {
232 0 ▶ $element->appendTextChild( $key, $val || '' );
Condition0%0/2
$val || ''
l!l
00
MC/DC0%0/1
$val || ''
$val
Truth table0%0/2
A
A= $val
Aresult
11
00
233 }
234  
235 # arrayrefs are groups of nested values
236 elsif ( ref $val eq 'ARRAY' ) {
237  
238 0 ▶ my $singular_key = $plural_to_singular{$key}
Condition0%0/2
$plural_to_singular{$key} || croak("could not convert '${key}' to singular")
l!l
00
MC/DC0%0/1
$plural_to_singular{$key} || croak("could not convert '${key}' to singular")
$plural_to_singular{$key}
Truth table0%0/2
A
A= $plural_to_singular{$key}
Aresult
11
00
239 || croak "could not convert '$key' to singular";
240  
241 0 my $wrapper = XML::LibXML::Element->new( $key );
242 0 $element->addChild( $wrapper );
243  
244 0 foreach my $entry_val ( @$val ) {
245 0 my $entry_node = XML::LibXML::Element->new( $singular_key );
246 0 $wrapper->addChild( $entry_node );
247  
248 # LineItems were including methods from Role/Common
249 0 my $class = blessed $entry_val;
250  
251 0 ▶ if ( $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) 0 0 0
252 0 my $new_entry = {};
253 0 my $fields = $class->_fields;
254  
255 0 foreach my $key ( keys %{$fields} ) {
256 0 ▶ $new_entry->{$key} = $entry_val->$key
Branch: 1 branch
BranchTrueFalseTotal
if $entry_val->$key 0 0 0
257 if $entry_val->$key;
258 }
259  
260 0 $entry_val = $new_entry;
261 }
262  
263 0 $self->construct_element( $entry_node, $entry_val );
264 }
265 }
266 ▶ elsif ( ref $val eq 'HASH' || ( $val && blessed $val ) ) {
Condition0%0/6
ref $val eq "HASH" or $val and builtin::blessed($val)
l!l&&r!l&&!r
000
$val and builtin::blessed($val)
!ll&&!rl&&r
000
MC/DC0%0/3
ref $val eq "HASH" or $val and builtin::blessed($val)
ref $val eq "HASH" $val builtin::blessed($val)
Truth table0%0/4
A or B and C
A= ref $val eq "HASH"B= $valC= builtin::blessed($val)
ABCresult
1XX1
0111
00X0
0100
267 0 my $wrapper = XML::LibXML::Element->new( $key );
268 0 $element->addChild( $wrapper );
269 0 $self->construct_element( $wrapper, $val );
270  
271 }
272  
273 }
274  
275 0 return;
276 }
277  
278 0 ▶sub response_xml_to_node {
Subroutine response_xml_to_node: not called CC=4 SCAR=30.0
Pod documented
279 0 my $self = shift;
280 0 ▶ my $xml = shift || croak "No XML passed in";
Condition0%0/2
shift() || croak('No XML passed in')
l!l
00
MC/DC0%0/1
shift() || croak('No XML passed in')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
281  
282 # get rid of any namespaces that will prevent simple xpath expressions
283 0 $xml =~ s{ \s+ xmlns=\S+ }{}xg;
284  
285 0 my $parser = XML::LibXML->new();
286 0 my $dom = $parser->parse_string( $xml );
287  
288 0 my $response = $dom->documentElement();
289 0 my $response_status = $response->getAttribute( 'status' );
290  
291 0 ▶ if ( $response_status ne 'ok' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response_status ne 'ok') { } 0 0 0
292 0 my $msg = XMLin( $xml );
293 0 ▶ warn $self->_sent_xml if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
294 0 my $error = "FreshBooks server returned error: '$msg->{error}'";
295 0 $self->_handle_server_error( $error );
296 }
297 else {
298 0 $self->last_server_error( undef );
299 }
300  
301 0 return $response;
302 }
303  
304 0 ▶sub send_xml_to_freshbooks {
Subroutine send_xml_to_freshbooks: not called CC=3 SCAR=24.8
Pod documented
305  
306 0 my $self = shift;
307 0 my $xml_to_send = shift;
308  
309 0 my $fb = $self->_fb;
310 0 my $response = undef;
311 0 $self->_sent_xml( $xml_to_send );
312  
313 0 ▶ if ( $fb->auth_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fb->auth_token) { } 0 0 0
314  
315 0 $self->_log( debug => "Not using OAuth" );
316  
317 0 my $ua = $fb->ua;
318 0 my $request = HTTP::Request->new(
319 'POST', # method
320 $fb->service_url, # url
321 undef, # header
322 $xml_to_send # content
323 );
324 0 $response = $ua->request( $request );
325  
326 }
327 else {
328  
329 0 $self->_log( debug => "using OAuth" );
330  
331 0 $response = $fb->oauth->restricted_request( $fb->service_url,
332 $xml_to_send );
333  
334 }
335  
336 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
337 0 croak "FreshBooks request failed: " . $response->status_line;
338 0 $self->_handle_server_error(
339 "FreshBooks request failed: " . $response->status_line );
340 }
341  
342 0 return $response->content;
343 }
344  
345 # When FreshBooks returns info on recurring items, it does not return the same
346 # frequency values as the values it requests. This method provides a lookup
347 # table to fix this issue.
348  
349 0 ▶sub _frequency_cleanup {
Subroutine _frequency_cleanup: not called CC=1 SCAR=6.9
350  
351 0 my $self = shift;
352  
353 return {
354 0 'y' => 'yearly',
355 'w' => 'weekly',
356 '2w' => '2 weeks',
357 '4w' => '4 weeks',
358 'm' => 'monthly',
359 '2m' => '2 months',
360 '3m' => '3 months',
361 '6m' => '6 months',
362 };
363  
364 }
365  
366 9 __PACKAGE__->meta->make_immutable( inline_constructor => 1 );
367  
368 9 1;
369  
370 # ABSTRACT: Base class
371  
372 __END__
↑ Summary API/Role/CRUD.pm »