File Coverage

lib/Image/Randim/Source.pm
Criterion Covered Total %
statement 34 34 100.0
branch 6 10 60.0
condition n/a
subroutine 10 10 100.0
pod 6 6 100.0
total 56 60 93.3


line stmt bran cond sub pod time code
1             our $VERSION = '0.02';
2             use v5.20;
3 1     1   986 use Moose;
  1         3  
4 1     1   4 use Module::Find;
  1         1  
  1         7  
5 1     1   5365 use namespace::autoclean;
  1         2  
  1         59  
6 1     1   4  
  1         2  
  1         6  
7             usesub Image::Randim::Source;
8              
9             has 'src_obj' => (
10             is => 'ro',
11             isa => 'Image::Randim::Source::Role',
12             );
13              
14             has 'timeout' => (
15             is => 'rw',
16             isa => 'Int',
17             default => 20,
18             );
19              
20             # This property will be removed - it's only here to fix the badness
21             # of people who don't keep their module libraries clean between
22             # released versions of this module.
23             has 'autoload_blacklist' => (
24             is => 'ro',
25             isa => 'HashRef',
26             default => sub { {'Image::Randim::Source::Role' => 1,
27             'Image::Randim::Source::Desktoppr' => 1,
28             }
29             },
30             );
31              
32             my $self = shift;
33             #my @class = map { s/.+::(.+)$/$1/r } grep {!/::Role$/ && !/::Desktoppr$/} findsubmod Image::Randim::Source;
34 28     28 1 31 my @class;
35             foreach (findsubmod Image::Randim::Source) {
36 28         19 next if defined ${$self->autoload_blacklist}{$_};
37 28         61 push @class, s/.+::(.+)$/$1/r;
38 56 100       31304 }
  56         976  
39 28         127 return \@class;
40             }
41 28         48  
42             my ($self, $source_name) = @_;
43             $self->{src_obj} = "Image::Randim::Source::$source_name"->new;
44             $self->{src_obj}->timeout($self->timeout);
45 28     28 1 76 return $self->src_obj;
46 28         541 }
47 27         381  
48 27         372 my ($self, $provider_names) = @_;
49             my $source = $provider_names ? $provider_names : $self->list;
50             return $self->set_provider($$source[int(rand(scalar @$source))]);
51             }
52 26     26 1 89  
53 26 50       40 my $self = shift;
54 26         95 die 'No valid provider' unless $self->src_obj;
55             return $self->src_obj->url;
56             }
57              
58 1     1 1 2 my $self = shift;
59 1 50       19 die 'No valid provider' unless $self->src_obj;
60 1         14 return $self->src_obj->name;
61             }
62              
63             my $self = shift;
64 26     26 1 81 die 'No valid provider' unless $self->src_obj;
65 26 50       328 return $self->src_obj->get_image;
66 26         316 }
67              
68             __PACKAGE__->meta->make_immutable;
69             1;
70 1     1 1 4  
71 1 50       14 =pod
72 1         13  
73             =head1 NAME
74              
75             Image::Randim::Source - Pull a random image from a source
76              
77             =head1 SYNOPSIS
78              
79             use Image::Randim::Source;
80            
81             $source = Image::Randim::Source->new;
82             $source->set_random_provider;
83             $image = $source->get_image;
84              
85             say $image->url;
86              
87             =head1 DESCRIPTION
88              
89             This is the main class to instantiate when wanting to pull random
90             image information from the defined sources.
91              
92             You can specifically state which source to use to pull a random image
93             from by using the set_provider() method, or you can also let the
94             provider be a random choice too by calling the method
95             set_random_provider() instead.
96              
97             Nothing much will work until you call one of those two methods, except
98             for list(), which returns a list of defined sources (providers of
99             images).
100              
101             =head1 METHODS
102              
103             =head2 C<list>
104              
105             Return a list of supported providers (ones with "plugins").
106              
107             =head2 C<set_provider($provider_name)>
108              
109             Sets the provider name to use, to grab a random image. Valid ones can
110             be found with the list() method.
111              
112             "Plugins" can be created to support more providers by implementing the
113             Image::Randim::Source::Role
114              
115             =head2 C<set_random_provider(['name1', 'name2'])>
116              
117             Without any parameters, chooses a random provider from the list() of
118             possible providers and set_provider()'s to it.
119              
120             An array ref - a list of source providers names - can be provided if
121             you want to limit the random choice to just those providers.
122              
123             =head2 C<url>
124              
125             Returns the URL from which the provider information is gathered. You
126             probably don't need to care about this.
127              
128             =head2 C<name>
129              
130             Returns the provider name (as it was set by set_provider())
131              
132             =head2 C<get_image>
133              
134             Returns an Image::Randim::Image object, populated with the necessary
135             information to download the image, and image credits for the owner of
136             the image, as well as reported dimensions of the image.
137              
138             =head1 ATTRIBUTES
139              
140             =head2 C<timeout>
141              
142             Gets or sets the timeout in seconds we will wait for a response from
143             the provider's server for the information. If you want something
144             different than the default (25 seconds) you must set this attribute
145             before calling the set_* methods.
146              
147             =head2 C<src_obj>
148              
149             Gives access to the provider "plugin" object directly. Not typically
150             needed.
151              
152             =head1 AUTHOR
153              
154             Mark Rushing <mark@orbislumen.net>
155              
156             =head1 COPYRIGHT AND LICENSE
157              
158             This software is copyright (c) 2017 by Home Grown Systems, SPC.
159              
160             This is free software; you can redistribute it and/or modify it under
161             the same terms as the Perl 5 programming language system itself.
162              
163             =cut