| 1 | package WWW::Marvel::Response; | ||
| 2 | use base qw/ Class::Accessor /; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use WWW::Marvel::Factory::Entity; | ||
| 6 | __PACKAGE__->follow_best_practice; | ||
| 7 | __PACKAGE__->mk_ro_accessors(qw/ attributionHTML attributionText code copyright etag status /); | ||
| 8 | |||
| 9 | sub get_count { $_[0]->{data}->{count} } | ||
| 10 | sub get_limit { $_[0]->{data}->{limit} } | ||
| 11 | sub get_offset { $_[0]->{data}->{offset} } | ||
| 12 | sub get_total { $_[0]->{data}->{total} } | ||
| 13 | |||
| 14 | # iterator | ||
| 15 | sub get_next_entity { | ||
| 16 | my ($self) = @_; | ||
| 17 | my $data = $self->_get_results->[ $self->_get_entity_index ]; | ||
| 18 | return if !defined $data; | ||
| 19 | my $ent = $self->_entity_factory->identify( $data ); | ||
| 20 | return $ent; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub reset_entity_iterator { | ||
| 24 | my ($self) = @_; | ||
| 25 | delete $self->{_internal}->{entity_index}; | ||
| 26 | return $self; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub _entity_factory { | ||
| 30 | $_[0]->{_internal}->{entity_factory} //= do { | ||
| 31 | WWW::Marvel::Factory::Entity->new(); | ||
| 32 | }; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub _get_entity_index { | ||
| 36 | my ($self) = @_; | ||
| 37 | my $i = $self->{_internal}->{entity_index} // 0; | ||
| 38 | if ($i < $self->get_count) { | ||
| 39 | $self->{_internal}->{entity_index} = $i +1; | ||
| 40 | } | ||
| 41 | return $i; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub _get_results { $_[0]->{data}->{results} } | ||
| 45 | |||
| 46 | 1; |