blib/lib/Image/Randim/Source.pm untested

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