File Coverage

blib/lib/WebService/Shutterstock/SearchResults.pm
Criterion Covered Total %
statement 57 59 96.6
branch 11 14 78.5
condition 3 5 60.0
subroutine 15 15 100.0
pod 6 7 85.7
total 92 100 92.0


line stmt bran cond sub pod time code
1             package WebService::Shutterstock::SearchResults;
2             {
3             $WebService::Shutterstock::SearchResults::VERSION = '0.006';
4             }
5              
6             # ABSTRACT: Class representing a single page of search results from the Shutterstock API
7              
8 9     9   51 use strict;
  9         16  
  9         521  
9 9     9   50 use warnings;
  9         19  
  9         324  
10 9     9   45 use Moo;
  9         21  
  9         68  
11 9     9   8775 use WebService::Shutterstock::SearchResult::Image;
  9         31  
  9         549  
12 9     9   15479 use WebService::Shutterstock::SearchResult::Video;
  9         40  
  9         413  
13              
14 9     9   89 use WebService::Shutterstock::HasClient;
  9         21  
  9         6514  
15             with 'WebService::Shutterstock::HasClient';
16              
17 7     7 0 248 sub BUILD { shift->_results_data } # eagar loading
18              
19              
20              
21             has type => (
22             is => 'ro',
23             required => 1,
24             isa => sub {
25             die 'invalid type (expected "image" or "video")' unless $_[0] eq 'image' or $_[0] eq 'video';
26             }
27             );
28              
29             has query => (
30             is => 'ro',
31             required => 1,
32             isa => sub { die "query must be a HashRef" unless ref $_[0] eq 'HASH' }
33             );
34             has _results_data => ( is => 'lazy' );
35              
36             sub _build__results_data {
37 7     7   668 my $self = shift;
38 7         26 my $client = $self->client;
39 7         992 $client->GET(sprintf('/%ss/search.json', $self->type), $self->query);
40 7         33 return $client->process_response;
41             }
42              
43              
44 5     5 1 4697 sub page { return shift->_results_data->{page} }
45 4     4 1 699 sub count { return shift->_results_data->{count} }
46 2     2 1 3326 sub sort_method { return shift->_results_data->{sort_method} }
47              
48              
49             sub results {
50 5     5 1 2142 my $self = shift;
51 5 100       27 my $item_class = $self->type eq 'image' ? 'WebService::Shutterstock::SearchResult::Image' : 'WebService::Shutterstock::SearchResult::Video';
52             return [
53 10         3385 map {
54 5 50       132 $self->new_with_client( $item_class, %$_ );
55             }
56 5         10 @{ $self->_results_data->{results} || [] }
57             ];
58             }
59              
60              
61             sub iterator {
62 1     1 1 2025 my $self = shift;
63 1         6 my $count = $self->count;
64 1         11 my $search_results = $self;
65 1         2 my $batch;
66 1         4 my $batch_i = my $i = my $done = 0;
67             return sub {
68 8 100   8   896 return if $i >= $count;
69 6         9 my $item;
70 6 100       25 if(!$batch){
    100          
71 1         6 $batch = $search_results->results;
72             } elsif($batch_i >= @$batch){
73 2         4 $batch_i = 0;
74             eval {
75 2         8 $search_results = $search_results->next_page;
76 2         671 $batch = $search_results->results;
77 2         66 1;
78 2 50       5 } or do {
79 0         0 warn $@;
80 0         0 $done = 1;
81             };
82             }
83 6 50 33     69 return if !$batch || $done;
84              
85 6         10 $item = $batch->[$batch_i];
86 6         7 $i++;
87 6         8 $batch_i++;
88 6         40 return $item;
89 1         9 };
90             }
91              
92              
93             sub next_page {
94 4     4 1 1057 my $self = shift;
95 4         9 my $query = { %{ $self->query } };
  4         27  
96 4   100     26 $query->{page_number} ||= 0;
97 4         459 $query->{page_number}++;
98 4         159 return WebService::Shutterstock::SearchResults->new( client => $self->client, query => $query, type => $self->type );
99             }
100              
101             1;
102              
103             __END__