| 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::Iterator; | ||||||||||
| 5 | 9 | $Net::FreshBooks::API::Iterator::VERSION = '0.24'; | |||||||||
| 6 | 9 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 9 | ▶ | use Lingua::EN::Inflect qw( PL ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 9 | ▶ | use XML::LibXML qw( XML_ELEMENT_NODE ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 9 | has 'parent_object' => ( is => 'rw' ); # The object we are iterating for | |||||||||
| 12 | 9 | has 'args' => ( is => 'rw' ); # args used in the search | |||||||||
| 13 | 9 | has 'total' => ( is => 'rw' ); # The total number of results | |||||||||
| 14 | 9 | has 'pages' => ( is => 'rw' ); # the number of result pages | |||||||||
| 15 | 9 | has 'item_nodes' => ( is => 'rw' ); # a list of all items | |||||||||
| 16 | 9 | has 'current_index' => ( is => 'rw' ); # which item we are currently on | |||||||||
| 17 | |||||||||||
| 18 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 19 | 0 | my $class = shift; | |||||||||
| 20 | 0 | my $self = bless shift, $class; | |||||||||
| 21 | |||||||||||
| 22 | my $request_args = { | ||||||||||
| 23 | _method => $self->parent_object->method_string( 'list' ), | ||||||||||
| 24 | |||||||||||
| 25 | # defaults | ||||||||||
| 26 | page => 1, | ||||||||||
| 27 | per_page => 15, | ||||||||||
| 28 | |||||||||||
| 29 | 0 | %{ $self->args }, | |||||||||
| 30 | }; | ||||||||||
| 31 | |||||||||||
| 32 | 0 | my $list_name = PL( $self->parent_object->api_name ); | |||||||||
| 33 | |||||||||||
| 34 | 0 | my $response = $self->parent_object->send_request( $request_args ); | |||||||||
| 35 | 0 | my ( $list ) = $response->findnodes( "//$list_name" ); | |||||||||
| 36 | |||||||||||
| 37 | 0 | $self->pages( $list->getAttribute( 'pages' ) ); | |||||||||
| 38 | 0 | $self->total( $list->getAttribute( 'total' ) ); | |||||||||
| 39 | |||||||||||
| 40 | 0 | my $parser = XML::LibXML->new(); | |||||||||
| 41 | |||||||||||
| 42 | my @item_nodes = # | ||||||||||
| 43 | 0 | map { $parser->parse_string( $_->toString ) } # recreate | |||||||||
| 44 | 0 | grep { $_->nodeType eq XML_ELEMENT_NODE } # filter | |||||||||
| 45 | $list->childNodes; | ||||||||||
| 46 | |||||||||||
| 47 | 0 | $self->item_nodes( \@item_nodes ); | |||||||||
| 48 | |||||||||||
| 49 | 0 | return $self; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 0 | ▶ | sub next { ## no critic | ||||||||
|
Subroutine
next: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 53 | ## use critic | ||||||||||
| 54 | 0 | my $self = shift; | |||||||||
| 55 | |||||||||||
| 56 | # work out what the current index should be | ||||||||||
| 57 | 0 | my $current_index = $self->current_index; | |||||||||
| 58 | 0 | ▶ | $current_index = defined( $current_index ) ? $current_index + 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | $self->current_index( $current_index ); | |||||||||
| 60 | |||||||||||
| 61 | # check that there is a next item | ||||||||||
| 62 | # FIXME - add fetching the next page if needed here | ||||||||||
| 63 | 0 | my $next_node = $self->item_nodes->[$current_index]; | |||||||||
| 64 | 0 | ▶ | return if !$next_node; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | |||||||||||
| 66 | # if we don't clone here, a user who iterates and pushes the returned | ||||||||||
| 67 | # objects to a list will end up with a list of copies of the last | ||||||||||
| 68 | # object to be returned | ||||||||||
| 69 | 0 | return $self->parent_object->clone->_fill_in_from_node( $next_node ); | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 9 | __PACKAGE__->meta->make_immutable( inline_constructor => 0 ); | |||||||||
| 73 | |||||||||||
| 74 | 9 | 1; | |||||||||
| 75 | |||||||||||
| 76 | # ABSTRACT: FreshBooks Iterator objects | ||||||||||
| 77 | |||||||||||
| 78 | __END__ | ||||||||||