File Coverage

lib/WebService/Braintree/ResourceCollection.pm
Criterion Covered Total %
statement 8 38 21.0
branch 0 10 0.0
condition n/a
subroutine 3 10 30.0
pod 0 7 0.0
total 11 65 16.9


line stmt bran cond sub pod time code
1             package WebService::Braintree::ResourceCollection;
2             $WebService::Braintree::ResourceCollection::VERSION = '0.94';
3 20     20   341 use 5.010_001;
  20         64  
4 20     20   120 use strictures 1;
  20         118  
  20         686  
5              
6 20     20   1743 use Moose;
  20         59  
  20         108  
7             extends "WebService::Braintree::ResultObject";
8              
9             has 'response' => (is => 'rw');
10             has 'ids' => (is => 'rw');
11             has 'page_size' => (is => 'rw');
12             has 'callback' => (is => 'rw');
13              
14             sub init {
15 0     0 0   my ($self, $response, $callback) = @_;
16 0           $self->response($response);
17 0           $self->ids($response->{search_results}->{ids});
18 0           $self->page_size($response->{search_results}->{page_size});
19 0           $self->callback($callback);
20 0           return $self;
21             }
22              
23             sub is_success {
24 0     0 0   my $self = shift;
25 0 0         return 1 unless $self->response->{'api_error_response'};
26 0           return 0;
27             }
28              
29             sub first {
30 0     0 0   my $self = shift;
31 0 0         return undef if $self->is_empty;
32 0           my $first_id = $self->ids->[0];
33 0           return $self->callback->([$first_id])->[0];
34             }
35              
36             sub is_empty {
37 0     0 0   my $self = shift;
38 0           $self->maximum_size == 0;
39             }
40              
41             sub maximum_size {
42 0     0 0   my $self = shift;
43 0 0         return (scalar @{$self->ids || []});
  0            
44             }
45              
46             sub each {
47 0     0 0   my ($self, $block) = @_;
48 0           my @page = ();
49              
50 0           for (my $count = 0; $count < $self->maximum_size; $count++) {
51 0           push(@page, $self->ids->[$count]);
52 0 0         if ((scalar @page) == $self->page_size) {
53 0           $self->execute_block_for_page($block, @page);
54 0           @page = ();
55             }
56             }
57              
58 0 0         $self->execute_block_for_page($block, @page) if(scalar(@page) > 0);
59             }
60              
61             sub execute_block_for_page {
62 0     0 0   my ($self, $block, @page) = @_;
63 0           my $resources = $self->callback->([@page]);
64 0           for my $resource (@$resources) {
65 0           $block->($resource);
66             }
67             }
68              
69             __PACKAGE__->meta->make_immutable;
70              
71             1;
72             __END__