lib/Image/Randim/Source.pm

filter: Statementstmt 100% 38 / 38 Branchbran 50.0% 4 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 6 / 6 totaltotal 93.5% 58 / 62 CC 5 SCAR 4.6
CC
5
Cov
91%
set_random_provider
2.0
url
2.0
name
2.0
CRAP 1.6
« lib/Image/Randim/Source/Unsplash.pm ↑ Summary lib/Image/Randim/Source/Role.pm »
1 package Image::Randim::Source;
2 1 our $VERSION = '0.01';
3 1 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Module::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 usesub Image::Randim::Source;
9  
10 1 has 'src_obj' => ( is => 'ro',
11 isa => 'Image::Randim::Source::Role',
12 );
13 1 has 'timeout' => ( is => 'rw',
14 isa => 'Int',
15 default => 20,
16 );
17  
18 sub list {
19 28 ▶ my $self = shift;
Subroutine list: called CC=1 SCAR=0.0
Pod documented
20 28 my @class = map { s/.+::(.+)$/$1/r } grep {!/::Role$/} findsubmod Image::Randim::Source;
21 28 return \@class;
22 }
23  
24 sub set_provider {
25 28 ▶ my ($self, $source_name) = @_;
Subroutine set_provider: called CC=1 SCAR=0.0
Pod documented
26 28 $self->{src_obj} = "Image::Randim::Source::$source_name"->new;
27 27 $self->{src_obj}->timeout($self->timeout);
28 27 return $self->src_obj;
29 }
30  
31 sub set_random_provider {
32 26 ▶ my ($self, $provider_names) = @_;
Subroutine set_random_provider: called CC=2 SCAR=7.1
Pod documented
33 26 ▶ my $source = $provider_names ? $provider_names : $self->list;
Branch: 1 branch
BranchTrueFalseTotal
$provider_names ? : 0 26 26
34 26 return $self->set_provider($$source[int(rand(scalar @$source))]);
35 }
36  
37 sub url {
38 1 ▶ my $self = shift;
Subroutine url: called CC=2 SCAR=7.1
Pod documented
39 1 ▶ die 'No valid provider' unless $self->src_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->src_obj 0 1 1
40 1 return $self->src_obj->url;
41 }
42  
43 sub name {
44 26 ▶ my $self = shift;
Subroutine name: called CC=2 SCAR=7.1
Pod documented
45 26 ▶ die 'No valid provider' unless $self->src_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->src_obj 0 26 26
46 26 return $self->src_obj->name;
47 }
48  
49 sub get_image {
50 1 ▶ my $self = shift;
Subroutine get_image: called CC=2 SCAR=7.1
Pod documented
51 1 ▶ die 'No valid provider' unless $self->src_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->src_obj 0 1 1
52 1 return $self->src_obj->get_image;
53 }
54  
55 1 __PACKAGE__->meta->make_immutable;
56 1 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
« lib/Image/Randim/Source/Unsplash.pm ↑ Summary lib/Image/Randim/Source/Role.pm »